尝试从 servlet 设置数据库连接时遇到一些问题。
我有以下类处理 servlet 中的数据库连接,但我不断收到 NullPointerException Class.forName()
。
数据库实用程序类:
public class Database {
private static Connection connection = null;
/**
*
* @return
*/
public static Connection getConnection(){
if(connection!=null){
return connection;
} else {
try {
Properties db_properties = new Properties();
InputStream inStream = Database.class.getClassLoader().getResourceAsStream("/db.properties");
db_properties.load(inStream);
//String dbDriver = db_properties.getProperty("driver").trim();
String url = db_properties.getProperty("url").trim();
String dbUser = db_properties.getProperty("user").trim();
String dbPass = db_properties.getProperty("pass").trim();
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url,dbUser,dbPass);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
return connection;
}
}
使用以下属性文件(我尝试对驱动程序进行硬编码,但仍然出现相同的错误!):
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:8889/gymbuddy
user=Mathan
pass=PYCCmcPDAFSj7N9B
我得到以下堆栈跟踪:
SEVERE: Allocate exception for servlet BuddyController
java.lang.NullPointerException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:171)
at com.mathanv.gb.util.Database.getConnection(Database.java:35)
at com.mathanv.gb.dao.BuddyDao.<init>(BuddyDao.java:35)
at com.mathanv.gb.controller.BuddyController.<init>(BuddyController.java:29)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:357)
at java.lang.Class.newInstance(Class.java:310)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:138)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1137)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:858)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:680)
我正在尝试从 Android 客户端发送发布请求。HTTPpost 请求似乎有效!但是获得连接给了我上述异常!帮助!
以下是我的 Servlet 布局:
如果您需要更多信息,请告诉我!