public static ResultSet executeQuery(String sql, Object[] params) {
Connection conn = getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i, params[i]);
}
rs = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("尼玛");
}
return rs;
}
使用PreparedStatement 出错,检查后发现setObject以1开始而不是0,将初始值改从1开始即可。
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i+1, params[i]);
}