-3

Below is the code I used for jdbc connection

String dbUrl="jdbc:mysql://localhost:3306/mysql";
String user= "kumar";
String pwd="ratiol";

try (Connection connection = DriverManager.getConnection(dbUrl, user, pwd)) {
    System.out.println("Database connected!");
} catch (SQLException e) {
    throw new IllegalStateException("Cannot connect the database!", e);
}

but I got error as below-

Exception in thread "main" java.lang.IllegalStateException: Cannot connect the database!
    at jdbcConnection.Jdbcdemo.main(Jdbcdemo.java:22)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mysql
    at java.sql.DriverManager.getConnection(DriverManager.java:596)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at jdbcConnection.Jdbcdemo.main(Jdbcdemo.java:19)

Can you please tell me how can I get jdbc url?

I am using eclipse(mars) in ubuntu 14.04

Kumar
  • 1
  • 1
  • 1

4 Answers4

1

if you are using netbeans, right click project -->properties -->libraries-->add library and select MySQL JDBC Driver

JavaLearner
  • 272
  • 1
  • 11
0

This is what you wrote

try (Connection connection = DriverManager.getConnection(dbUrl, user, pwd)) {
    System.out.println("Database connected!");
} catch (SQLException e) {
    throw new IllegalStateException("Cannot connect the database!", e);
}

it would be like this:

Connection con = null;
try {
    //registering the jdbc driver here, your string to use 
    //here depends on what driver you are using.
    Class.forName("something.jdbc.driver.YourFubarDriver");   
    Connection connection = DriverManager.getConnection(dbUrl, user, pwd)
} catch (SQLException e) {
    throw new RuntimeException(e);
}

Please check Class.forName not more needed when using JDBC v.4

Let's take a quick look at how we can use this new feature to load a JDBC driver manager. The following listing shows the sample code that we typically use to load the JDBC driver. Let's assume that we need to connect to an Apache Derby database, since we will be using this in the sample application explained later in the article:

 Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
    Connection conn =
        DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);

But in JDBC 4.0, we don't need the Class.forName() line. We can simply call getConnection() to get the database connection.

Note that this is for getting a database connection in stand-alone mode. If you are using some type of database connection pool to manage connections, then the code would be different.

There are plenty of reason for the exception No suitable driver found for jdbc:mysql like

  1. Your JDBC URL can be wrong.
  2. ClassPath/BuildPath/Lib folder missing the connector jar.
  3. Driver Information is Wrong.
Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
  • Your "corrected" code is outdated: using `Class.forName` is no longer necessary (which you even acknowledge with the text you quote), and the OP correctly used try-with-resources (assuming they don't want to do anything else with the connection), which your code undoes by removing the try-with-resources. – Mark Rotteveel Sep 03 '15 at 08:37
0

just add the com.mysql.jdbc.Driver to the lib folder in you program files.

-3

Just load the driver first:

Class.forName("com.mysql.jdbc.driver");
spongebob
  • 8,370
  • 15
  • 50
  • 83
SaviNuclear
  • 886
  • 1
  • 7
  • 19