前提:
假设现在有一个提交表单,里面有n个查询条件,用户可以填写几个或者不填写来提交。
思想“
判断每个条件用户输入是否为空,不为空则使用sql语句拼接
这是期末项目中用来查询图片的函数的代码:
public List<Image> watchImageDao(Image img){
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
List<Image> li=null;
int index=-1;
int flag=0;
int count=0;
String table_name="t_image";
String sql=table_name;
String[] paramater= {img.getCountry(),img.getName(),img.getPosition(),
img.getResolution(),img.getLongitude(),img.getLatitude(),
img.getAcquisition_time(),img.getScale()
};
boolean[] empty=new boolean[8];
for(int i=0;i<8;i )
empty[i]=true;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/happy","root", "root");
if(img.getCountry()!="") {
sql="select* from " table_name " where country=?";
empty[0]=false;
flag=1;
count ;
}
if(img.getName()!="") {
if(flag==0)
sql="select* from " table_name " where name=?";
else
sql="select* from" "(" sql ")subset_name" " where name=?";
empty[1]=false;
flag=1;
count ;
}
if(img.getPosition()!="") {
if(flag==0)
sql="select* from " table_name " where position=?";
else
sql="select* from" "(" sql ")subset_position" " where position=?";
empty[2]=false;
flag=1;
count ;
}
if(img.getResolution()!="") {
if(flag==0)
sql="select* from " table_name " where resolution=?";
else
sql="select* from" "(" sql ")subset_resolution" " where resolution=?";
empty[3]=false;
flag=1;
count ;
}
if(img.getLongitude()!="") {
if(flag==0)
sql="select* from " table_name " where longitude=?";
else
sql="select* from" "(" sql ")subset_longitude" " where longitude=?";
empty[4]=false;
flag=1;
count ;
}
if(img.getLatitude()!="") {
if(flag==0)
sql="select* from " table_name " where latitude=?";
else
sql="select* from" "(" sql ")subset_latitude" " where latitude=?";
empty[5]=false;
flag=1;
count ;
}
if(img.getAcquisition_time()!="") {
if(flag==0)
sql="select* from " table_name " where acquisition_time=?";
else
sql="select* from" "(" sql ")subset_acquisition_time" " where acquisition_time=?";
empty[6]=false;
flag=1;
count ;
}
if(img.getScale()!="") {
if(flag==0)
sql="select* from " table_name " where scale=?";
else
sql="select* from" "(" sql ")subset_scale" " where scale=?";
empty[7]=false;
flag=1;
count ;
}
if(count==0) {
sql="SELECT* FROM " table_name;
}
ps=conn.prepareStatement(sql);
for(int i=0;i<count;i ) {
for(int j=0;j<8;j ) {
if(!empty[j]) {
ps.setString(i 1, paramater[j]);
empty[j]=true;
break;
}
}
}
rs=ps.executeQuery();
li=new ArrayList<>();
while(rs.next()) {
Image i=new Image();
i.setCountry(rs.getString("country"));
i.setName(rs.getString("name"));
i.setPosition(rs.getString("position"));
i.setResolution(rs.getString("resolution"));
i.setLongitude(rs.getString("longitude"));
i.setLatitude(rs.getString("latitude"));
i.setAcquisition_time(rs.getString("acquisition_time"));
i.setScale(rs.getString("scale"));
li.add(i);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return li;
}