当前位置: 代码迷 >> J2EE >> 关于java连接数据库关闭连接的有关问题
  详细解决方案

关于java连接数据库关闭连接的有关问题

热度:48   发布时间:2016-04-22 00:50:17.0
关于java连接数据库关闭连接的问题
在一个项目里写了一个对数据库操作的线程

Java code
import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;public class Database extends Thread {    // 声明服务、客户端线程    //ClientThread clientThread;    ServerThread serverThread;    // 字符串    private String str=null;    private String[] str_str=null;    private String longitude=null;    private String latitude=null;    String driver = "com.mysql.jdbc.Driver";    // URL指向要访问的数据库名scutcs    String url = "jdbc:mysql://127.0.0.1:3306/TEST1";    // MySQL配置时的用户名    String user = "root";    // Java连接MySQL配置时的密码    String password = "root";    Connection conn=null;    Statement statement;    // 构造函数    public Database(ServerThread serverThread) {        this.serverThread = serverThread;        //驱动程序名                                try {                // 加载驱动程序                Class.forName(driver);                // 连续数据库                Connection conn = DriverManager.getConnection(url, user, password);                if(!conn.isClosed()){                    System.out.println("Succeeded connecting to the Database!");                                    }                statement = conn.createStatement();                                }catch(ClassNotFoundException e) {                       System.out.println("Sorry,can`t find the Driver!");                       e.printStackTrace();                       } catch(SQLException e) {                       e.printStackTrace();                       } catch(Exception e) {                       e.printStackTrace();                       }                                               }    // 运行    @Override    public void run() {        // TODO Auto-generated method stub        super.run();        while (true) {                                    synchronized (serverThread.messages) {                synchronized (serverThread.imsgflag) {                    if (serverThread.imsgflag==1)                    {                        str = (String) this.serverThread.messages.firstElement();                        str_str=str.split("\\|");                        if(str_str.length==5){                            System.out.println("来自数据库的资源");                                                            longitude=str_str[3];                            latitude=str_str[4];                                                                                    try {                                /*// 加载驱动程序                                Class.forName(driver);                                // 连续数据库                                conn = DriverManager.getConnection(url, user, password);                                if(!conn.isClosed()){                                    System.out.println("Succeeded connecting to the Database!");                                                                    }                                                            Statement statement=conn.createStatement();*/                                String sql2 = "INSERT INTO posi (longitude,latitude) VALUES ('"+longitude+"','"+latitude+"')";                                //('"+p_id+"','"+parent+"','"+child+"','"+c_id+"')";                                //statement.executeUpdate(sql2);                                //String sql2 = "INSERT INTO position (X,Y) VALUES ('"+"23"+"','"+"43"+"')";                                                                statement.executeUpdate(sql2);                                                                //conn.close();                                } catch(SQLException e) {                                       e.printStackTrace();                                       } catch(Exception e) {                                       e.printStackTrace();                                     System.out.println("数据库被关闭");                                    }                                                     }                        serverThread.imsgflag=0;                        }                        }                         }                                    }    }    public void closedb()     {        try{                        conn.close();            System.out.println("成功关闭");        }catch(SQLException e){            e.printStackTrace();                     }            }}
  相关解决方案