-1

i am trying to create connection to my musql in java. when i do it with Main it works with no problem, but when i use apache its return the error:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/database_name..

i read the look alike questions and none of them helps me.

i use JDK 8 and JDBC 5. the JDBC driver locate in the project lib and i included it in the build path and this is my code:

 // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost:3306/program1";

   //  Database credentials
   static final String USER = "user";
   static final String PASS = "pass";
   
   private static Connection conn = null;
   
   
   /*
    * Create connection to the DB in singletone
    * */
   protected static Connection getConnection() throws ClassNotFoundException, SQLException
   {
           if(conn==null)
           {
               try 
               {
                   // Register JDBC driver
                  //Class.forName(JDBC_DRIVER);
                   
                   // Open connection
                   conn = DriverManager.getConnection(DB_URL,USER,PASS);
               } 
               catch ( SQLException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
                   if(conn != null)
                        conn.close();
               }
           }
       
       return conn;
   }

in the moment the debug run the line " conn = DriverManager.getConnection(DB_URL,USER,PASS);" i get the exception.

What is causing this error? and why when i run it from main it works?

Community
  • 1
  • 1
o k
  • 103
  • 1
  • 3
  • 11
  • 1
    classpath typically – Drew Nov 02 '16 at 20:42
  • It totally depends on what you are doing. Servlet engine, eclipse, debug, release, `web-inf/lib` ... – Drew Nov 02 '16 at 20:45
  • i use servlet engine, but in the debug i can see that the servlet make the calls, the problem is in the DriverManager.getConnection – o k Nov 02 '16 at 20:50
  • and the classpath variable is "C:\Program Files\Java\jdk1.8.0_40\jre\lib " – o k Nov 02 '16 at 20:52
  • did you try the deprecated `Class.forName` ? Also specify the servlet engine type in particular, and show it's lib info (in other words narrow it down from [dubiosity](http://www.servlets.com/engines/) ) – Drew Nov 02 '16 at 20:52
  • Class.forName() is not needed since JDBC 4.0. http://stackoverflow.com/questions/33391496/is-class-forname-mechanism-needed – o k Nov 02 '16 at 20:55
  • , http://stackoverflow.com/a/5829491 , http://stackoverflow.com/a/13175611 – Drew Nov 02 '16 at 21:15
  • Is the driver installed in Tomcat, or are you deploying the driver with your application. Automatic driver loading only works if the driver is included in the main Tomcat classpath (lib folder of Tomcat), not when it is on the webapp classpath. Sidenote: There is no such thing as JDBC 5, the latest JDBC specification is 4.2, with 4.3 in the works for Java 9. You are probably referring to the driver version. – Mark Rotteveel Nov 03 '16 at 10:17
  • you probably right, i download the driver from here: dev.mysql.com/downloads/connector/j and i copied it to the lib folder of Tomcat, and also include a copy in my project (for debug uses) and it doesn't work – o k Nov 03 '16 at 11:43

1 Answers1

1

You need to download mysqlConnector, and add is as an lib to your project.

https://dev.mysql.com/downloads/connector/j/

Christian Moen
  • 1,253
  • 2
  • 17
  • 31