当前位置: 代码迷 >> Java相关 >> JAVA数据库编程中java.lang.NullPointerException异常
  详细解决方案

JAVA数据库编程中java.lang.NullPointerException异常

热度:221   发布时间:2012-03-11 16:32:47.0
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);
            con.commit();
            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  

----------------解决方案--------------------------------------------------------
有谁熟悉数据库编程,希望指点
----------------解决方案--------------------------------------------------------
  相关解决方案