title: JDBC connection object returns null in Servlet/Web application
body: <p>I am trying to connect my Java Web application to a mysql server. I am deploying on tomcat. I have a JDBC class which a I have created which returns the connection object in a normal java main program. However when I use the JDBC class on a deployed tomcat application, it returns null. I do not understand this. It will return connection in main but not in servlet/Web app. Thanks. Code:</p>
  
  <p>Registration.Servlet:</p>
  
  <pre><code>    Jdbc dbConn = new Jdbc();
      Connection con = dbConn.connect();
      String insert_member = "INSERT INTO Members (id, name, address, dob, "
              + "dor, status, balance)"  + " VALUES (?, ?, ?, ?, ?, ?, ?)";
      //String c = con.toString();
      //con = null for some reason
      pstmt = con.prepareStatement(insert_member);
      pstmt.setString(1, "coo");
      pstmt.setString(2, "aa");
      pstmt.setString(3, "26 road");
      pstmt.setDate(4, new java.sql.Date(new Date().getTime()));
      pstmt.setDate(5, new java.sql.Date(new Date().getTime()));
      pstmt.setString(6, "APPLIED");
      pstmt.setFloat(7, 10);
      pstmt.execute();
  </code></pre>
  
  <p>JDBC class:</p>
  
  <pre><code>public class Jdbc {
  Connection connection;
  Statement statement;
  ResultSet rs;
  
      public Jdbc() {
          String db = "xyz_assoc";
                  try {
              Class.forName("com.mysql.jdbc.Driver");
              connection = DriverManager.getConnection
                  ("jdbc:mysql://localhost:3306/"+db.trim(), "root", "");
          }
          catch(ClassNotFoundException cfe){    
  
          }
          //SQL exception
          catch (SQLException ex) {
              Logger.getLogger(Jdbc.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  
      //open connection to database
      public Connection connect(){
          return connection;
      }
  
      //execute mysql queryS
      public ResultSet executeQuery(String sql_query) throws SQLException {
          statement = null;
          statement = connection.createStatement();
          rs = statement.executeQuery(sql_query);
          return rs;
      }
  
      //close connection to database
      //destroy any remaining objects
      public void close() throws SQLException{
          statement.close();
          rs.close();
          connection.close();
      }
  
   }
  }
  </code></pre>
  
  <p>StackTrace:</p>
  
  <blockquote>
    <p>Nov 30, 2016 11:12:18 AM org.apache.catalina.core.ApplicationContext
    log INFO: SessionListener: contextDestroyed() Nov 30, 2016 11:12:18 AM
    org.apache.catalina.core.ApplicationContext log INFO: ContextListener:
    contextDestroyed() Nov 30, 2016 11:12:23 AM
    org.apache.catalina.core.ApplicationContext log INFO: ContextListener:
    contextInitialized() Nov 30, 2016 11:12:23 AM
    org.apache.catalina.core.ApplicationContext log INFO: SessionListener:
    contextInitialized() Nov 30, 2016 11:12:23 AM
    org.apache.catalina.core.ApplicationContext log INFO: ContextListener:
    attributeAdded('org.apache.jasper.compiler.TldLocationsCache',
    'org.apache.jasper.compiler.TldLocationsCache@7f4e2bf7') Nov 30, 2016
    11:12:59 AM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet [Registration] in context with
    path [/XYZGroup] threw exception [Servlet execution threw an
    exception] with root cause java.lang.ClassNotFoundException:
    model.Jdbc    at
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
      at
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
      at model.MemberManager.addMember(MemberManager.java:46)     at
    web.Registration.processRequest(Registration.java:52)     at
    web.Registration.doPost(Registration.java:101)    at
    javax.servlet.http.HttpServlet.service(HttpServlet.java:646)  at
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)  at
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
      at
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
      at
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
      at
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
      at
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
      at
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
      at
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
      at
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
      at
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
      at
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
      at
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
      at
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
      at
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
      at
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Thread.java:745)</p>
  </blockquote>