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?