日期:2014-05-18 浏览次数:21258 次
ps = con.prepareStatement(sql);这种方法 好像不行
ps.setString(1, name);
import oracle.jdbc.OraclePreparedStatement; // 这个是Oracle数据库的驱动才提供的
String sql = "Select * From employees Where 1=1 And (:pName IS null OR first_name = :pName) And (:pSalary IS null OR salary = :pSalary)"; // 注意这句YD的SQL
Connection con = openConnection(); // 数据库连接获取略
OraclePreparedStatement stat = (OraclePreparedStatement) con.prepareStatement(sql);
stat.setObjectAtName("pName", null);
stat.setObjectAtName("pSalary", 2500);
ResultSet rs = stat.executeQuery();
int cols = rs.getMetaData().getColumnCount();
while (rs.next()) {
for (int i = 1; i <= cols; i++) {
System.out.print(rs.getString(i) + "\t");
}
System.out.println();
}