Home > Back-end >  With the druid to connect to the database connection pool why null pointer exception
With the druid to connect to the database connection pool why null pointer exception

Time:05-21

 package com. Zyj. School. Util. 

The import com. Alibaba. Druid. Pool. DruidDataSource;
The import com. Alibaba. Druid. Pool. DruidDataSourceFactory;

The import javax.mail. SQL. DataSource.
Import the Java. IO. IOException;
Import the Java. IO. InputStream;
Import the Java. SQL. Connection;
Import the Java. SQL. The ResultSet;
Import the Java. SQL. SQLException;
Import Java, SQL Statement;
Import the Java. Util. Properties;

Public class DruidUtil {
/* *
* database connection pool has multiple connections
* so will con change for the DataSource
*/
//get a data connection source con
Private static DruidDataSource datasource.
Public static DataSource getDatasource () {
Return the datasource;
}
//static block decrease code optimization code
The static {
Try {
The Properties of pro=readproperties ();
System. The out. Println (" Druid + pro ");
The datasource=(DruidDataSource) DruidDataSourceFactory. CreateDataSource (pro);
System. Out.println (" database connection success!" );
} the catch (Exception e) {
e.printStackTrace();
System. Out.println (" database connection failed!" );
}
}
//read method reads ojdbc of hard disk. The properties file information
Public static Properties readproperties () {
//by class loaders
InputStream InputStream=DruidUtil. Class. GetClassLoader () getResourceAsStream (" DruidPool. Properties ");
//data type
The Properties of pro=new Properties ();
Try {
Pro. The load (inputstream);
} the catch (IOException e) {
e.printStackTrace();
}
Return pro;
}
//for connecting the datasource connection
Public static Connection getCon () {
The Connection Connection=null;
Try {
The connection=datasource. GetConnection ();
} the catch (SQLException e) {
e.printStackTrace();
System. The out. Println (e + "datasource get connection problems");
return null;
}
Return the connection;
}
//close the database related links
Public static void closeConnection (ResultSet rs, Connection conn, the Statement state) throws the Exception {
if(rs!=null) {
rs.close();
//System. Out.println (" database rs close success!" );
}

If (the state!=null) {
State. The close ();
//System. Out.println (" database pstate close success!" );
}

If (conn!=null) {
Conn. Close ();
//System. Out.println (" database con close success!" );
}
Shutdown System. Out.println (" success!" );
}
}

Java. Lang. NullPointerException
At Java. Util. The Properties of $LineReader. ReadLine (434). The Properties of Java:
The at Java. Util. Properties. Load0 (353). The Properties of Java:
The at Java. Util. Properties. The load (341). The Properties of Java:
At com. Zyj. School. Util. DruidUtil. Readproperties (DruidUtil. Java: 44)
At com. Zyj. School. Util. DruidUtil. & lt; Clinit> (DruidUtil. Java: 28)
At com. Zyj. School. Dao. LoginDaoImpl. & lt; init> (LoginDaoImpl. Java: 19)
At com. Zyj. School. Service. LoginServiceImpl. & lt; init> (LoginServiceImpl Java: 19)
At com. Zyj. School. Controller. LoginServlet. DoPost (LoginServlet. Java: 36)
At com. Zyj. School. Controller. LoginServlet. DoGet (LoginServlet. Java: 29)
The at Jakarta. Servlet. HTTP. HttpServlet. Service (HttpServlet. Java: 663)
The at Jakarta. Servlet. HTTP. HttpServlet. Service (HttpServlet. Java: 770)
The at org. Apache. Catalina. Core. ApplicationFilterChain. InternalDoFilter (ApplicationFilterChain. Java: 223)
The at org. Apache. Catalina. Core. ApplicationFilterChain. DoFilter (ApplicationFilterChain. Java: 158)
At org, apache tomcat, websocket server WsFilter. DoFilter (53) WsFilter. Java:
The at org. Apache. Catalina. Core. ApplicationFilterChain. InternalDoFilter (ApplicationFilterChain. Java: 185)
The at org. Apache. Catalina. Core. ApplicationFilterChain. DoFilter (ApplicationFilterChain. Java: 158)
At com. Zyj. School. Filter. SchoolFilter. DoFilter (SchoolFilter. Java: 24)
The at org. Apache. Catalina. Core. ApplicationFilterChain. InternalDoFilter (ApplicationFilterChain. Java: 185)
The at org. Apache. Catalina. Core. ApplicationFilterChain. DoFilter (ApplicationFilterChain. Java: 158)
The at org. Apache. Catalina. Core. StandardWrapperValve. Invoke (StandardWrapperValve. Java: 202)
The at org. Apache. Catalina. Core. StandardContextValve. Invoke (StandardContextValve. Java: 97)
The at org. Apache. Catalina. The authenticator. AuthenticatorBase. Invoke (AuthenticatorBase. Java: 543)
The at org. Apache. Catalina. Core. StandardHostValve. Invoke (StandardHostValve. Java: 127)
The at org. Apache. Catalina. Valves. ErrorReportValve. Invoke (ErrorReportValve. Java: 92)
The at org. Apache. Catalina. Valves. AbstractAccessLogValve. Invoke (AbstractAccessLogValve. Java: 690)
The at org. Apache. Catalina. Core. StandardEngineValve. Invoke (StandardEngineValve. Java: 78)
At org. Apache. Catalina. Connector. CoyoteAdapter. Service (353) CoyoteAdapter. Java:
At org. Apache. Coyote. Http11. Http11Processor. Service (374) Http11Processor. Java:
The at org. Apache. Coyote. AbstractProcessorLight. Process (AbstractProcessorLight. Java: 65)
The at org. Apache. Coyote. AbstractProtocol $ConnectionHandler. Process (AbstractProtocol. Java: 870)
At org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun NioEndpoint. Java: (1696)
The at org.apache.tomcat.util.net.SocketProcessorBase.run (SocketProcessorBase. Java: 49)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related