简体   繁体   中英

Problems with JDBC Driver & Tomcat

Having some problems trying to setup a DB connection from a servlet.

I have the following class that handles the DB connection in the servlet but I keep getting an NullPointerException for Class.forName() .

DB Util Class:

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;
    }
}

With the following properties file (I tried hardcoding the driver in and still get the same error!):

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:8889/gymbuddy
user=Mathan
pass=PYCCmcPDAFSj7N9B

And I get the following Stack trace:

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)

I'm trying to send a post request from a Android client. The HTTPpost request seems to work! But getting the connection gives me the above exception! Help!

The following is my Servlet layout:

Servlet布局

Let me know if you need any more information!

The only way you can get that exception is by passing null to Class.forName() or maybe by having a corrupt JVM. The code you've shown can't throw that exception. Double check your results. At worst, you should get a ClassNotFoundException . Maybe you've updated your code but haven't compiled and redeployed it yet, so you're seeing the results of old code running in the server still.

Are you sure when you hard coded the driver name in the code then also you are getting same error. Class.forName() throws null pointer exception when you pass null as parameter. i think in the first case property file is not loaded and driver name returns null.

This happens if you have a mysql driver in more locations.

Try to look for other locations.Or remove i from the lib folder. If it is as I say the problem should be solved.

Delete your code immediately and use a DataSource right now in Tomcat. Your code is far away from being high quality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM