PreparedStatement问题
我写了一个类
public ResultSet getAtu(String type,String datetime,String datetime1)
{
ResultSet oRs=null;
Connection conn=list.getConn();
try{
PreparedStatement pstmt=null;
String sql= "select id,name from jctb where type=? and adddate between ? and ? ";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,type);
pstmt.setString(2,datetime);
pstmt.setString(3,datetime1);
oRs=pstmt.executeQuery();
}catch(
SQLException e){
System.out.println( "SQL语句出错 "+e.getMessage());
}
return oRs;
}
用这个写法oRs不能往下移动
但是如果我把那几个setString去掉 把sql语句该成String sql= "select id,name from jctb where type= ' "+type+ " ' and adddate between ' "+datetime+ " ' and ' "+datetime1+ " ' ";
这样就没问题,一直搞了好久 没搞清楚是什么回事,type传进来是中文,datetime和datetime1我在传进来之前就已经转换成字符了
请教大家
请详细说明
------解决方案--------------------pstmt.setDate(2,datetime);
pstmt.setDate(3,datetime1);
------解决方案--------------------import java.sql.Date;
Date date = Date.valueOf(datetime);
Date date1 = Date.valueOf(datetime1);
pstmt.setDate(2,date);
pstmt.setDate(3,date1);
------解决方案--------------------修改查询语句为:conn.prepareStatement(SQL,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)试试
------解决方案--------------------楼上正解
------解决方案--------------------UP