嘗試從servlet建立數據庫連接時遇到一些問題。JDBC驅動程序和Tomcat的問題
我有以下類處理servlet中的數據庫連接,但我不斷收到Class.forName()
的NullPointerException。
DB的Util類:
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的佈局:
讓我知道如果你需要更多的信息!
問題是服務器沒有更新我的更改。必須確保通過清理項目並終止服務器並重新啓動以及重新啓動android模擬器。非常感謝! – MathanMV 2013-03-05 00:31:55
是的,在我的情況下,我需要做一個maven項目更新,在某些地方配置了borked – chrismarx 2015-04-17 18:44:47