title: mysql connection working fine locally but failed when calling in a rest function call [java]
body: <p>I am making a small project with REST + Mysql + Java.
  Rest calls are working fine. But when I make any database transaction in rest functions like I want to fetch some value from database on rest call then it gives <code>[java.lang.ClassNotFoundException: com.mysql.jdbc.Driver]</code>. When I call the same function by obj of class then it works fine but fails if called by rest api call. When I make a rest call to function , data is also coming via parameters and arguments and I am able to use it in function body but unable to perform database transaction.</p>
  
  <p>I found one same <a href="https://stackoverflow.com/questions/36739695/database-connection-works-locally-but-fails-when-published-to-server"> question</a> but its on ASP.net</p>
  
  <p>I have added mysql jar to classpath as well as in WEB-INF/lib/ folder</p>
  
  <p>my connection class:</p>
  
  <pre><code>import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.SQLException;
  
  public class conectionclass {
  
      public static Connection getConnection(){
  
          Connection conn = null ;
          try {
              Class.forName("com.mysql.jdbc.Driver");
              conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/emps","root", "12345" );
              System.out.println("-------------  Connection Succesful ----------------"); 
          } catch (SQLException | ClassNotFoundException e) {
              // TODO Auto-generated catch block
              System.out.println("-------------  Connection Fail ----------------"); 
  
              e.printStackTrace();
          }
          return conn;
      }
  
  }
  </code></pre>
  
  <p>my Rest Function:</p>
  
  <pre><code>@POST
  @Path("/postjson")
  @Produces("application/json")
  public Employee  getJSONfromApp(HashMap p) {
  
      return ( new empdao()).getEmployee("test", "1234") ; 
  
  }
  </code></pre>
  
  <p>Employee Dao Class function :</p>
  
  <pre><code>    @Override
      public  Employee getEmployee(String employee, String pass) {
          int count = 0 ;
           Connection conn = Connectionclass.getConnection();
  //       System.out.println("Employee : "+employee+"  Connection : "+conn);
              if(employee!=null || employee != "" )
              {
                  String query =   "select * from emps where username = '"+employee+"' &amp;&amp; pass = '"+pass+"'  ";
                  try {
                      Statement stmt  = conn.createStatement() ;
                      System.out.println("Statement created.."+ stmt);
                       ResultSet a =  stmt.executeQuery(query);
  
                       while(a.next()){
                           return (new Employee(a.getString(2) , a.getString(3) ));
                       } 
  
                  } catch (Exception e) {
                      // TODO Auto-generated catch block
                      System.out.println("Error in  connection.....");
  //                  e.printStackTrace();
                  } finally{
                      try {
                          conn.close();
                      } catch ( Exception e) {
                          System.out.println("Error in closing connection.....");
  //                      e.printStackTrace();
                      }
  
                  }
  
              }
  
          return null;
      }
  </code></pre>
  
  <p>output when call via RestService :</p>
  
  <pre><code>-------------  Connection Fail ----------------
  java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
      at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
      at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1157)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:259)
      at com.app.App3.Connectionclass.getConnection(Connectionclass.java:13)
      at com.app.App3.empdao.getEmployee(empdao.java:47)
      at com.mkyong.rest.JSONService.getJSONfromApp(JSONService.java:90)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:140)
      at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:255)
      at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:220)
      at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:209)
      at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:519)
      at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:496)
      at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:119)
      at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208)
      at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55)
      at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
      at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
      at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1517)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1474)
      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:744)
  Error in  connection.....
  </code></pre>