以下java代码在运行时抛出此错误:ResultSet关闭后不允许操作。令人好奇的是,代码的预期功能正在完成。密码确实发生了变化,表明该语句确实已执行。最后的“失败”输出似乎表明代码在某处有异常。有什么想法吗?
private void ChangeSQLPassword(String userName,char[] pass) {
Connection conn = null;
String hashed = "";
String salt = "";
String concat = "";
try {
String url = "jdbc:mysql://localhost:3306/PM";
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, "Tempuser", "temppass");
System.out.println("Database connection established");
} catch (Exception e) {
e.printStackTrace();
System.err.println("Cannot connect to database server");
} finally {
if (conn != null) {
try {
System.err.println("Connection not null...");
salt = getSalt();
hashed = sha256(pass,salt);
Statement sta3 = conn.createStatement();
ResultSet rs = sta3.executeQuery("SELECT * FROM pmusers"); //Select users
System.out.println(userName + " " + new String(pass));
while (rs.next() == true) { //Loop through results
if (rs.getString("username").equals(userName)) {
concat = concat.concat("UPDATE pmusers SET Hashed = '" + hashed + "', Salt = '" + salt + "' WHERE Username = '" + userName +"';");
sta3.executeUpdate(concat);
}
}
} catch (Exception e) { /* ignore close errors */
e.printStackTrace();
System.err.println("Failed.");
}
}
}
}
static String sha256(char[] input, String Salt) throws NoSuchAlgorithmException {
MessageDigest mDigest = MessageDigest.getInstance("SHA-256");
byte[] result;
StringBuffer sb = new StringBuffer();
try {
mDigest.update(Salt.getBytes("UTF-8"));
result = mDigest.digest(String.valueOf(input).getBytes("UTF-8"));
for (int i = 0; i < result.length; i++) {
sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
}
} catch (UnsupportedEncodingException e) {
System.out.println("Encoding failed.");
}
String hashed = org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(String.valueOf(sb).getBytes());
return hashed;
}
private static String getSalt() throws NoSuchAlgorithmException, NoSuchProviderException {
//Always use a SecureRandom generator
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "SUN");
//Create array for salt
byte[] salt = new byte[16];
//Get a random salt
sr.nextBytes(salt);
//return salt
return salt.toString();
}
堆栈跟踪: java.sql.SQLException:ResultSet关闭后不允许操作 在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:996) 在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935) 在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:924) 在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:870) 在com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:734) 在com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:6313) 在ServerInterface.ChangeSQLPassword(ServerInterface.java:585) 在ServerInterface.access $ 300(ServerInterface.java:68) 在ServerInterface $ 2.actionPerformed(ServerInterface.java:277) 在javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) 在javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2341) 在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 在javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) 在java.awt.Component.processMouseEvent(Component.java:6505) 在javax.swing.JComponent.processMouseEvent(JComponent.java:3320) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) at java.awt.EventQueue.access $ 200(EventQueue.java:103) at java.awt.EventQueue $ 3.run(EventQueue.java:694) at java.awt.EventQueue $ 3.run(EventQueue.java:692) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue $ 4.run(EventQueue.java:708) at java.awt.EventQueue $ 4.run(EventQueue.java:706) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
失败。
非常感谢!
答案 0 :(得分:1)
您正在重复使用sta3
:尝试
concat = concat.concat(...
Statement sta4 = conn.createStatement();
sta4.executeUpdate(concat);