java如何关mysql_Java MySQL Connection没有关闭

我正在使用Java应用程序前端来连接MySQL 5.6服务器上的数据库并与之交互 . 使用MySQL的JDBC连接器我遇到了在调用con.close()时服务器连接没有关闭的问题 . 应用程序关闭时,连接都会被删除 . 以下是用于进行查询的dao类 .

package binaparts.dao;

import java.sql.*;

import org.json.JSONArray;

import org.json.JSONObject;

import binaparts.properties.*;

import binaparts.util.ToJSON;

public class DBConnect {

protected Statement st = null;

protected ResultSet rs = null;

protected Connection con = null;

protected PreparedStatement pst = null;

private String configFilePath = "config.properties";

public DBConnect(){

try {

Class.forName("com.mysql.jdbc.Driver");

} catch (ClassNotFoundException ex) {

ex.printStackTrace();

}

}

private Connection getDBConnection() throws Exception{

ConfigurationManager configProps = new ConfigurationManager(configFilePath);

String host = configProps.getProperty("host");

String port = configProps.getProperty("port");

String database = configProps.getProperty("database");

String user = configProps.getProperty("user");

String password = configProps.getProperty("password");

try{

con = DriverManager.getConnection("jdbc:mysql" + "://" + host + ":" + port + "/" + database, user, password);

}catch(SQLException SQLex){

con = null;

}catch(Exception ex){

ex.printStackTrace();

con = null;

}finally{

try{rs.close();} catch(Exception ex) { /*ignore*/}

try{st.close();} catch(Exception ex) { /*ignore*/}

try{pst.close();} catch(Exception ex) { /*ignore*/}

}

return con;

}

下一部分代码是用数据库验证应用程序用户的方法 . 有一些System.out.println()语句通过该进程跟踪con变量 .

public boolean verifyUser() throws Exception {

ConfigurationManager config = new ConfigurationManager(configFilePath);

String username = config.getProperty("appUser");

String password = config.getProperty("appPassword");

boolean userCheck = false;

try{

if(con == null){

System.out.println("there is not a connection");

}

if(con != null){

System.out.println("there is a connection");

}

con = getDBConnection();

if(con == null){

System.out.println("get connection did not work");

}

if(con != null){

System.out.println("get connection did work");

}

JSONObject temp = queryReturnUser(username).getJSONObject(0);

con.close();

String un = null;

try{

un = temp.get("username").toString();

}catch(Exception ex){un = " ";}

String pw = null;

try{

pw = temp.get("password").toString();

}catch(Exception ex){pw = " ";}

if(username.equals(un)){

if(password.equals(pw)){

userCheck = true;

}

}

}catch(Exception ex){/*ignore*/}

finally{

try{

if(con == null){

System.out.println("connection was terminated before finally");

}

if(con != null){

System.out.println("connection was not terminated before finally");

System.out.println("trying again...");

con.close();

}

if(con != null){

System.out.println("there is a connection still");

}

}catch(Exception ex){ex.printStackTrace();}

}

return userCheck;

}

运行该方法的输出是:

没有连接获取连接确实工作连接没有终止,最后再次尝试...还有一个连接仍然如果我改变了con.close(); con = null;然后我得到输出:没有连接获得连接确实工作连接终止之前终止

所以连接在那时成功终止,但我觉得这不是正确的方法 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值