我在玩这种数据库,我用过后尝试关闭HSQLDB连接,但最后还是打开了。
代码:
//----This methods are in a specific connection class file
public static Connection conn = null;
public static Connection getConnection(){
try {
input = new FileInputStream("PathToMyPropertiesFile");
prop.load(input);
//The properties constants are correctly checked
Class.forName(prop.getProperty("DRIVER_HSQLDB"));
conn = DriverManager.getConnection(prop.getProperty("CONN_HSQLDB"));
}
catch(ClassNotFoundException | SQLException e) {
LOG.log(null,"Error: "+e);
}
catch (IOException ex) {
LOG.log(null,"FILE ERROR: "+ex);
}
finally {
if (input != null) {
try {
input.close();
} catch (Exception e) {
LOG.log(null,"CLOSE ERROR: "+e);
}
}
}
return conn;
}
public static boolean stopConn() {
try {
if(conn != null) {
conn.close();
System.err.println("\nCLOSE CONN\n"+conn);
return true;
}
}
catch (SQLException e) {
e.printStackTrace();
return false;
}
return false;
}
//========= the other class file with the methods to use the conneciton
public static boolean insertUser(String uName, String uEmail){
Connection con;
con = ConnectionDB.getConnection();
PreparedStatement ps = null;
try {
String consulta = "insert into USERS (\"NICK\",\"EMAIL\") VALUES(?,?);";
ps = con.prepareStatement(consulta);
System.err.println(ps);
ps.setString(1,uName);
ps.setString(2,uEmail);
System.err.println("\nASSIGNATION\n"+ps);
if(ps.executeUpdate() == 1) {
System.err.println("\nTRUE\n");
return true;
}
}
catch(SQLException e) {
e.printStackTrace();
}
finally {
try {
System.err.println("\nFINALLY\n"+ps);
if(ps != null) {
ps.close();
System.err.println("\nCLOSE PS\n"+ps);
}
if(con != null) {
con.close();
System.err.println("\nCLOSE CON\n"+con);
if(ConnectionDB.stopConn()) {
System.err.println("\nALL IS OK\n"+ConnectionDB.conn);
}
else {
System.err.println("\nMEEEEKKKK!!!\n"+ConnectionDB.conn);
}
}
}
}
return false;
}
控制台给了我这个结果,我不知道为什么连接从来没有关闭,因为我试图关闭它两次。如果有人有想法请告诉我。
org.hsqldb.jdbc.JDBCPreparedStatement@4501280b[sql=[插入 USERS ("NICK","EMAIL") VALUES(?,?);], parameters=[[null], [null]]]
分配 org.hsq这是我的 cldb.jdbc.JDBCPreparedStatement@4501280b[sql=[插入用户 ("NICK","EMAIL") VALUES(?,?);], parameters=[[extra], [extra@mail. com]]]
真的
最后 org.hsqldb.jdbc.JDBCPreparedStatement@4501280b[sql=[插入用户(“NICK”,“EMAIL”)值(?,?);],参数=[[额外],[额外@mail.com]] ]
关闭 PS org.hsqldb.jdbc.JDBCPreparedStatement@4501280b[关闭]
CLOSE CON org.hsqldb.jdbc.JDBCConnection@3e5b87f5
关闭连接 org.hsqldb.jdbc.JDBCConnection@3e5b87f5
一切正常 org.hsqldb.jdbc.JDBCConnection@3e5b87f5