Home > Back-end >  Database connection occasionally submitted to the null pointer
Database connection occasionally submitted to the null pointer

Time:11-01

Occasionally into the cover letter submitted to the null pointer errors, checked the online although know is database connection problem, but don't know how to solve, home page requires several different query the database and table of data to the page rendering, other single query page will not quote this fault, some guidance about
?


The servlet:


I put a dao (other) :


Database connection tools:
Public class DBUtil {
Private static Connection conn.
Private static Statement STMT.
Private static PreparedStatement PSTMT;
Private static DBUtil instance=new DBUtil ();
Private DBUtil () {}

The static {
Try {
Class.forname (". Com. Mysql. JDBC Driver ");
} the catch (a ClassNotFoundException e) {
//TODO Auto - generated the catch block,
e.printStackTrace();
}
}

Public static DBUtil getInstance () {
Return the instance.
}

Public static Connection getConnection () {
Try {
Conn=DriverManager. GetConnection (" JDBC: mysql://localhost: 3306/XCHCXXGL ", "root", "a + 123456");
} the catch (SQLException e) {
//TODO Auto - generated the catch block,
e.printStackTrace();
}
Return the conn.
}

Public Statement getStatement () {
The Connection conn=getConnection ();
Try {
If (conn!=null) {
STMT=conn. CreateStatement ();
}

} the catch (SQLException e) {
//TODO Auto - generated the catch block,
e.printStackTrace();
}
Return STMT.
}

Public static PreparedStatement getPreparedStatement (String SQL) {
The Connection conn=getConnection ();
Try {
If (conn!=null) {
PSTMT=conn. PrepareStatement (SQL);
}
} the catch (SQLException e) {
e.printStackTrace();
}
Return PSTMT;
}

Public static void closeDBResources () {
Try {
If (PSTMT!=null & amp; & ! PSTMT. IsClosed ()) {
PSTMT. Close ();
}
If (STMT!=null & amp; & ! STMT. IsClosed ()) {
STMT. Close ();
}
If (conn!=null & amp; & ! Conn. IsClosed ()) {
Conn. Close ();
}
} the catch (SQLException e) {
e.printStackTrace();
}
}
}

CodePudding user response:

Mistake is that get int (" field ") come out of an error,

This, if this field does not exist, or the value is not a number, what will return? Can't return 0,

Suggestion: will this get int into get string, you take a closer look at what is right,

CodePudding user response:

This is just what you said the rs. Get int () error, but the next is likely to be another rs. Get string error, location is not fixed, an error is not have every time
  • Related