方法:
@Override
public int update(String sql, Object[] parameters) {
Connection conn = DBConnectUtils.getConnection();
PreparedStatement pstmt = null;
if (parameters == null || parameters.length == 0) {
return 0;
}
for (int i = 0; i < parameters.length; i++) {
// pstmt.setObject(第几个参数, 替换成什么);
try {
pstmt = conn.prepareStatement(sql);
pstmt.setObject(i + 1, parameters[i]);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int j = 0;
try {
pstmt = conn.prepareStatement(sql); //这里出问题 不知道应该写什么
j = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("j: " + j);
DBCloseUtils.closeCS(conn, pstmt);
return j;
}
测试:
sql = "update user_info set password = ? where id = ?";
databaseFactory.update(sql, new Object[] { "444", 25 });
System.out.println(sql);