Home > Back-end >  Javaweb connect the mysql database to Java. Lang. NullPointerException
Javaweb connect the mysql database to Java. Lang. NullPointerException

Time:01-04

Help: in other files created dealDatabase used to handle database, then packaged into a jar into the idea of create javaweb project, but the idea has been an error in the tomcat runtime
According to Java. Lang. NullPointerException: always invoke a "Java. SQL. Connection. PrepareStatement (String)" because "conn is null
"But in the other documents in the original has been able to run
In the tomcat lib joined the mysql - connector. The jar
Using tomcat version 8.5.61 jdk15, mysql5.7
Import the Java. SQL. Connection;
Import the Java. SQL. DriverManager;
Import of Java, SQL PreparedStatement;
Import the Java. SQL. The ResultSet;
Import the Java. SQL. SQLException;
Import Java, SQL Statement;
Import the Java. Util. StringTokenizer;

Public class dealDatabase {
The static final String JDBC_Driver=". Com. Mysql. JDBC Driver ".
The static final String DB_URL="JDBC: mysql://localhost: 3306/webdata? ServerTimezone=UTC& UseSSL=false ";
The static final String USER="root";
The static final String PASS="39393939";

Public dealDatabase () {
}
Public static void main (String [] args) {
DealDatabase HHH=new dealDatabase ();
System. The out. Println (HHH. GetMaxFrame (1));
}
//to the database to add content
Public void addOnestep (int step, int [] matrix, int [] numberOfNote, int remark, double [] degree, double avR, double cc, int noteNum, int sideNum) {
The Connection conn=null;
String query="insert into graph (id, matrix, note that few, degree, avR, cc, noteNum, sideNum) values (?,?,?,?,? ,? ,? ,? ,? ,? ,? ,? ,?) ";

Try {
Conn=DriverManager. GetConnection (" JDBC: mysql://localhost: 3306/webdata? ServerTimezone=UTC& UseSSL=false ", "root", "39393939");
{} the catch (SQLException var17)
Var17. PrintStackTrace ();
}

Try {
PreparedStatement preparedstmt=conn. PrepareStatement (query);
Preparedstmt. SetInt (1, step);
Preparedstmt. SetString (2, enclosing toStringMethod (matrix));
Preparedstmt. SetString (3, enclosing toStringMethod (numberOfNote));
Preparedstmt. SetInt (4, remark);
Preparedstmt. SetString (5, enclosing toStringMethod (degree));
Preparedstmt. SetDouble (6, avR);
Preparedstmt. SetDouble (7, cc);
Preparedstmt. SetInt (8, noteNum);
Preparedstmt. SetInt (9, sideNum);
Preparedstmt. ExecuteUpdate ();
Preparedstmt. Close ();
{} the catch (Exception var16)
Var16. PrintStackTrace ();
}

Try {
Conn. Close ();
{} the catch (SQLException var15)
Var15. PrintStackTrace ();
}

}
  • Related