java:sql的后赋值问题

如果使用预编译的SQL语句

例如:

public void test() throws SQLException {
Connection connection = null;
PreparedStatement pstm = null;
connection = DriverManager.getConnection("jdbc:mysql://121.89.220.197:3306/smbms?useUnicode=true&characterEncoding=utf-8","root","123456");

String sql = "update smbms_user set userPassword = ? where id = ?";
Object[] params = {"123456789",1};

pstm = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pstm.setObject(i+1,params[i]);
}
int i = pstm.executeUpdate();//在这里需要注意两点:1,保证重载的函数正确,2.在函数executeUpdate()里面没有参数
System.out.println(i);


}

 

 

------------恢复内容开始------------

如果使用预编译的SQL语句

例如:

public void test() throws SQLException {
Connection connection = null;
PreparedStatement pstm = null;
connection = DriverManager.getConnection("jdbc:mysql://121.89.220.197:3306/smbms?useUnicode=true&characterEncoding=utf-8","root","123456");

String sql = "update smbms_user set userPassword = ? where id = ?";
Object[] params = {"123456789",1};

pstm = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pstm.setObject(i+1,params[i]);
}
int i = pstm.executeUpdate();//在这里需要注意两点:1,保证重载的函数正确,2.在函数executeUpdate()里面没有参数
System.out.println(i);


}

 

 

------------恢复内容结束------------

posted @ 2020-08-29 12:07  strcoy_s  阅读(220)  评论(0编辑  收藏  举报