日期:2014-05-20  浏览次数:21042 次

关于prepareStatement.setInt的使用
private boolean isFirstAnsw(int userphone){

int current;
db= new DBConn();
conn = db.getConnection();
try {
String sql = "select * from useransw where userphonenum = ?";
ps = conn.prepareStatement(sql);
//String sql = "select * from useransw where userphonenum = "+userphone;
ps.setInt(1, userphone);
rs = ps.executeQuery(sql);
if(rs.next()){
current = rs.getInt("CURRENT");
if(current!=0){
return false;
}
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
db.close(conn, stmt, rs);
}
return true;

}

这是判断一个用户是否是第一次登入的代码,我不明白中间为什么要使用ps.setInt(1, userphone); 是什么作用。

------解决方案--------------------
简单点说

String sql = "select * from useransw where userphonenum = ?"; 
这句话里不是有个问号么 
ps.setInt(1, userphone); 这句话的意思就是把第一个问题所在的地方换成userphone

------解决方案--------------------
String sql = "select * from useransw where userphonenum = ?";
ps.setInt(1, userphone)的意思是设置sql的第一个参数也就是第一个问号值为userphone