2

I just purchased a hosting service and added my J2EE web project via cpanel and successfully created a database in MySQL. But I am facing a problem in creating a JDBC connection with cpanel. It says "No suitable driver found for localhost". I guess cpanel is not able to detect the MySQL J connector in web-inf/lib - but why??? Is their something else to be done to run it? I am new to cpanel. Here is my connection class

public class DbConnection {


    private static Connection connection;
    public static Connection getConnection()
    {
        return connection;
    }
    static
    {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        try {
            connection = DriverManager.getConnection("localhost","abcas","*******");
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                    e.printStackTrace();
            }

    }

}
Tersosauros
  • 883
  • 1
  • 12
  • 22
Mayank Vaid
  • 330
  • 1
  • 7
  • 18
  • Is there a .JAR in your `classpath` that **has** `com.mysql.jdbc.Driver` in it? – Tersosauros Mar 06 '16 at 15:54
  • yes i have 'mysql-connector-java-5.1.38-bin.jar' in web-inf/lib/ folder – Mayank Vaid Mar 06 '16 at 15:57
  • One more thing this application is working fine on my eclipse with mysql @Tersosauros – Mayank Vaid Mar 06 '16 at 15:58
  • And that library version matches your MySQL version? and that path is on the `classpath` being used by your J2EE environment? ...*and* MySQL is definitely running on localhost on the default port? – Tersosauros Mar 06 '16 at 15:59
  • yess because there is no issue with Class.forname(**) like it doesn't says classnotfoundexception or something like that. Issue is with DriverManager.getConnection(**) – Mayank Vaid Mar 06 '16 at 16:10
  • Are you specifying a conection string anywhere? – Tersosauros Mar 06 '16 at 16:40
  • Possible duplicate of [How to fix: "No suitable driver found for jdbc:mysql://localhost/dbname" error when using pools?](http://stackoverflow.com/questions/5556664/how-to-fix-no-suitable-driver-found-for-jdbcmysql-localhost-dbname-error-w) – Tersosauros Mar 06 '16 at 16:40
  • no I have hardcoded the hostname username and password in the code itself. You can see from the code @Tersosauros – Mayank Vaid Mar 06 '16 at 18:13
  • 1
    Lot of 3rd party hosting disallow this construct (supplying JDBC driver via webapp instead of via server) because it's subject to memory leaks. If the MySQL database is also hosted by the same hosting, then usually they already offer a server-configured connection pooled `DataSource` which you should grab from JNDI via e.g. `@Resource`. Consult their documentation/help for detail as to the JNDI name. By the way, "J2EE" died a decade ago and is since renamed to Java EE. I'm not sure which resources you're using to learn Java EE, but you need to make absolutely sure they are up to date. – BalusC Mar 06 '16 at 18:37

0 Answers0