标题:JAVA数据库编程中java.lang.NullPointerException异常
只看楼主
liuyujieny
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:33
专家分:111
注 册:2012-2-20
结帖率:100%
 问题点数:0 回复次数:1 
JAVA数据库编程中java.lang.NullPointerException异常
JAVA中是不是没有全局变量的概念,但定义在所有方法外的变量是什么变量 ?
import java.sql.*;
public class DataBaseManager {
    Connection con=null;
    ResultSet rs;
    Statement stmt;
    public DataBaseManager(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/liu","root","yes");
            stmt=con.createStatement();
            con.setAutoCommit(false);
        }
        catch(ClassNotFoundException cnfe){
            System.out.println("Failed to load JDBC/ODBC driver.");
            cnfe.printStackTrace();
            System.exit(1);
        }
        catch(SQLException sqle){
            System.out.println(sqle.toString());
        }
    }
   
    public ResultSet getResult(String strSQL){
        try{
            rs=stmt.executeQuery(strSQL);
            return rs;
        }
        catch(SQLException sqle){
            System.out.println(sqle.toString());
            return null;
        }
    }
    public boolean updateSql(String strSQL){
        try{
            stmt.executeUpdate(strSQL);
            ();
            return true;
        }
        catch(SQLException sqle){
            System.out.println(sqle.toString());
            return false;
        }
    }
    public void closeConnection(){
        try{
            rs.close();
            stmt.close();
            con.close();
        }
        catch(SQLException sqle){
            System.out.println(sqle.toString());
        }
    }
}




借阅类:
import java.sql.*;
public class BorrowBook {
    DataBaseManager db=new DataBaseManager();
    void borrow(String sno,String bno,String bdate,String rdate){
            String strSQL="insert into BookBrowse(studentname,bookname,borrowdate,returndate) values('"+sno+"','"+bno+"','"+bdate+"','"+rdate+"')";
            if(db.updateSql(strSQL)){
                System.out.println("借阅完成!");
                db.closeConnection();
    }
}
}



import java.sql.*;
public class Test {
    public static void main(String[]args) throws InstantiationException, IllegalAccessException{
        DataBaseManager db=new DataBaseManager();
        ResultSet rs;
        BorrowBook bb=new BorrowBook();
        bb.borrow("some", "ghost", "2011-12-5", "2012-3-12");
        rs=db.getResult("select* from bookbrowse");
        try {
            if(!rs.first()){
                System.out.println("学生没有借阅过此书");
            }
            else{
                System.out.println(rs.getString("studentName"));
                System.out.println(rs.getString("bookName"));
                System.out.println(rs.getString("borrowDate"));
                while(rs.next()){
                    System.out.println(rs.getString("studentName"));
                    System.out.println(rs.getString("bookName"));
                    System.out.println(rs.getString("borrowDate"));
                }
            }
            db.closeConnection();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
这是数据库编程中的出现的一个问题。
抛出异常:java.lang.NullPointerException
可能是由于关闭数据库的链接对象时,将所有方法共享的conn关掉,出现异常。以后方法使用时找不到conn,请问此处问题是什么?
conn是否是在所有DataBaseManager类中共享???
搜索更多相关主题的帖子: 数据库编程 class localhost public import 
2012-03-11 16:32
liuyujieny
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:33
专家分:111
注 册:2012-2-20
得分:0 
有谁熟悉数据库编程,希望指点
2012-03-12 22:47



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-362834-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.187784 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved