【Java开源代码栏目提醒】:网学会员,鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“ComplexQuery.java”一文,供大家参考学习!
package com.dao;
import com.actionForm.IfForm;
import com.core.ChStr;
import com.core.ConnDB;
import java.util.List;
import java.util.LinkedList;
import java.sql.*;
import com.actionForm.BorrowForm;
public class ComplexQuery {
public List query(String str){
String sql="";
if(str==null || str.equals("")){
sql = "select * from tb_material material inner join tb_borrow borr on material.ID=borr.materialID";
}else{
sql = "select * from tb_material material inner join tb_borrow borr on material.ID=borr.materialID where " +
str;
}
System.out.println("SQL:"+sql);
ConnDB conn=new ConnDB();
ResultSet rs=conn.executeQuery(sql);
List list=new LinkedList();
BorrowForm borrow=null;
try {
while (rs.next()) {
borrow=new BorrowForm();
borrow.setId(rs.getInt("id"));
borrow.setMaterialID(rs.getInt("materialID"));
borrow.setName(rs.getString("name"));
borrow.setSpec(rs.getString("spec"));
borrow.setUnit(rs.getString("unit"));
borrow.setProducer(rs.getString("producer"));
borrow.setBorrowperson(rs.getString("borrowperson"));
borrow.setBorrowtime(rs.getDate("borrowtime"));
borrow.setIfback(rs.getByte("ifback"));
list.add(borrow);
}
} catch (SQLException ex) {
System.out.println("查询数据时的错误信息:"+ex.getMessage());
}
return list;
}
//组合查询条件
public String ifCompose(IfForm ifForm){
ChStr chStr=new ChStr();
String str=null;
String flag[]=ifForm.getFlag(); //定义一个字符型数组,获取表单中复选框提交的数据
if (flag!=null){
String flagv = flag[0];
if ("a".equals(flagv)) { //按指定字段查询
if (ifForm.getF() != null) {
str = ifForm.getF() + " like '%" +
chStr.toChinese(ifForm.getKey())+ "%'"; //此处需要进行转码
}
System.out.println("按指定字段
查询:"+str);
}
if ("b".equals(flagv)) { //按时间段查询
String sdate = ifForm.getSdate();
String edate = ifForm.getEdate();
if (sdate != null && edate != null) {
str = "borr.borrowTime between '" + sdate + "' and '" + edate +
"'";
}
System.out.println("日期" + str);
}
//同时选择日期和条件进行查询
if (flag.length == 2) {
if (ifForm.getF() != null) {
str =ifForm.getF() + " like '%" +
chStr.toChinese(ifForm.getKey()) + "%'"; //此处需要进行转码
}
System.out.println("日期和条件");
String sdate = ifForm.getSdate();
String edate = ifForm.getEdate();
String str1 = null;
if (sdate != null && edate != null) {
str1 = "borrowtime between '" + sdate + "' and '" + edate +
"'";
}
str = str + " and borr." + str1;
System.out.println("条件和日期:" + str);
}
}
return str;
}
}