日期:2014-05-19  浏览次数:20854 次

关于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 posi