读书人

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

发布时间: 2012-09-12 09:21:30 作者: rapoo

关于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();                     }            }} 



原本是每当serverThread.imsgflag=1的时候就建立数据库的连接,操作数据库,然后关闭(就是注释掉的部分)
可是这样对数据库的连接断开太频繁
于是就把建立连接的步骤放在了这个类的构造函数里
在其他程序里没建立一个该类对象就自动建立数据库连接,在run()方法里也不关闭连接,而是写了一个closedb()的函数
在其他程序里调用这个函数就会调运conn.close()

可是这个时候为什么会出现空指针错误
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Database.closedb(Database.java:135)
at Server.actionPerformed(Server.java:63)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
135行就是conn.close()哪一行
server是调用它的程序

这个空指针是什么意思
是因为,在这个线程结束的时候已经自动关闭连接了吗
可是从网上找的答案好像都说得自己关闭
很迷惑,求解答

[解决办法]
虽然不懂,友情顶
[解决办法]
你全局的Connection conn=null;

而你的closedb方法没任何传递参数、
你把全局conn给close了;
而这个全局的conn一直是为null的、

当然会出空指针异常、
把你的方法适当的修改一下就可以了

public void closedb(Connection conn) {
conn.close();//自己try catch
}

用的时候把需要关闭的conn 传递里就可以了、
[解决办法]
挂个连接池就完了,为什么干这么复杂, conn.close(); 必须写在一个try里边

读书人网 >J2EE开发

热点推荐