我的数据库连接不会关闭的原因是什么?错误说当我执行多个查询时连接太多
在此先感谢,这是代码:
try {
st = DBConnector.getConnection().prepareStatement(sqlQuery);
st.setString(1, userID);
result = st.executeQuery();
checker = !result.first();
}catch(Exception e){
e.printStackTrace();
}
finally
{
try {
DBConnector.getConnection().close();
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
顺便说一下,这是我的 DBConnector 类
public class DBConnector {
private static Connection;
private static String host;
private static String username;
private static String password ;
public static void setUsername(String newusername){
username = newusername;
}
public static void setPassword(String newpassword){
password = newpassword;
}
public static void setHost(String newHost){
host = newHost;
}
public static void setConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection(host, username , password);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getUsername(){
return username;
}
public static String getPassword(){
return password;
}
public static String getHost(){
return host;
}
public static Connection getConnection(){
setConnection();
return conn;
}
public static void disconnect(){
conn = null;
}
}