我已经在这里查看了它为什么会发生的原因,我唯一能看到的就是因为打开了连接。在我的代码中,我关闭一个连接并注册一个新连接以重新打开。
try{
//Registration of driver, connector
String myDriver = "com.mysql.jdbc.Driver";
String myURL = "jdbc:mysql://localhost:3306/maintenance";
Class.forName(myDriver);
Connection connection = DriverManager.getConnection(myURL, "admin", "");
//Call MySQL for table names of users
String query = "SELECT users FROM goods";
Statement st = connection.createStatement();
ResultSet res = st.executeQuery(query);
while (res.next()) {
String name = res.getString("users");
if(name.equals(username)){
connection.close();
res.close();
try{
Connection connection2;
connection2 = DriverManager.getConnection("jdbc:mysql://localhost:3306/maintenance", "admin", "");
query = "SELECT idkeys FROM goods";
Statement st2;
st2 = connection2.createStatement();
ResultSet res2;
res2 = st2.executeQuery(query);
String myDriver2 = "com.mysql.jdbc.Driver";
Class.forName(myDriver2);
connection2 = DriverManager.getConnection("jdbc:mysql://localhost:3306/maintenance", "admin", "");
while (res2.next()) {
keyed = res2.getObject("idkeys");
if (keyed.equals(sanitizedPass)){
checked = true;
connection2.close();
res2.close();
}
}
} catch (Exception e2){
System.err.println(e2.getMessage());
}
}
}
//Close connections
if (!checked){
res.close();
connection.close();
}
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
return checked;
在我的代码中,至少据我所知,没有这样的事情发生。为什么还会被抛出?
答案 0 :(得分:0)
try {
String myURL = "jdbc:mysql://localhost:3306/maintenance";
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(myURL, "admin", "");
String query = "SELECT users, idkeys FROM goods";
Statement st = connection.createStatement();
ResultSet res = st.executeQuery(query);
while (res.next()) {
String name = res.getString("users");
if (name.equals(username)) {
Object keyed = res.getObject("idkeys");
if (keyed.equals(sanitizedPass)) {
checked = true;
break;
}
}
}
return checked;
} catch (Exception e) {
e.printStackTrace(); // or do some reasonable error logging
} finally {
// close the ResultSet, Statement and Connection here
}