墨西哥男人:JSP问题,帮我解释一下

来源:百度文库 编辑:中科新闻网 时间:2024/05/13 13:54:40
public void changePassword(String password) {
DBConnect dbc = null;
try{
dbc = new DBConnect();
dbc.prepareStatement("UPDATE user SET UserPassword = ? WHERE UserName = ?");
dbc.setBytes(1,password.getBytes("GB2312"));
dbc.setBytes(2,userName.getBytes("GB2312"));
dbc.executeUpdate();
}catch(Exception e){
System.err.println(e);
}finally{
try{
dbc.close();
}catch(Exception e){
e.printStackTrace();
}
}

}

这个方法中哪个?是代表什么?????啊啊????
问号到底是什么啊?

?是个占位符这个地方是可以被替换的。
dbc.setBytes(1,password.getBytes("GB2312")); 替换第一个问号
dbc.setBytes(2,userName.getBytes("GB2312")); 替换第二个问号
sql语句也可以这样写,如下:
String strSQL ="UPDATE user SET UserPassword = '" + password + "'" + " WHERE UserName = '" + "userName" + "'";

?是preparedStatement中sql的占位符,表示这个地方需要填写参数,这个单数就是后面的语句:dbc.setBytes(1,password.getBytes("GB2312"));
设置的,这样的好处是,sql不需要每次都进行编译,只需要把参数传进去就行了

简而言之:动态替换参数。
dbc.setBytes(1,password.getBytes("GB2312"))中的"1"是指第一个问号;
dbc.setBytes(2,userName.getBytes("GB2312"))中的"2"是指第两个问号;

占位符