Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbname [duplicate]

I have this Java program: MySQLConnectExample.java

import java.sql.*; import java.util.Properties;  public class MySQLConnectExample {     public static void main(String[] args) {         Connection conn1 = null;         Connection conn2 = null;         Connection conn3 = null;          try {             String url1 = "jdbc:mysql://localhost:3306/aavikme";             String user = "root";             String password = "aa";              conn1 = DriverManager.getConnection(url1, user, password);             if (conn1 != null)                 System.out.println("Connected to the database test1");              String url2 = "jdbc:mysql://localhost:3306/aavikme?user=root&password=aa";             conn2 = DriverManager.getConnection(url2);             if (conn2 != null) {                 System.out.println("Connected to the database test2");             }              String url3 = "jdbc:mysql://localhost:3306/aavikme";             Properties info = new Properties();             info.put("user", "root");             info.put("password", "aa");              conn3 = DriverManager.getConnection(url3, info);             if (conn3 != null) {                 System.out.println("Connected to the database test3");             }         } catch (SQLException ex) {             System.out.println("An error occurred. Maybe user/password is invalid");             ex.printStackTrace();         }     } } 

I compile it like this:

E:\java mysql code driver>javac MySQLConnectExample.java  E:\java mysql code driver>java -cp mysql-connector-java-3.0.11-stable-bin.jar;. MySQLConnectExample 

I get this error:

An error occurred. Maybe user/password is invalid java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/ aavikme         at java.sql.DriverManager.getConnection(DriverManager.java:596)         at java.sql.DriverManager.getConnection(DriverManager.java:215)         at MySQLConnectExample.main(MySQLConnectExample.java:20) 

What am I doing wrong?

like image 615
user3416261 Avatar asked Mar 13 '14 16:03

user3416261


People also ask

How do I fix Java SQL Sqlexception no suitable driver found for JDBC?

This error occurs if JDBC is not able to find a suitable driver for the URL format passed to the getConnection() method e.g. "jdbc:mysql://" in our case. In order to solve this error, you need the MySQL JDBC driver like mysql-connector-java-5.1. 36. jar in your classpath.

How do I fix Java Sqlexception no suitable driver found for JDBC mysql localhost 3306?

How to solve "No suitable driver found for jdbc:mysql://localhost:3306/mysql" You can solve this problem first by adding MySQL connector JAR, which includes JDBC driver for MySQL into classpath e.g. mysql-connector-java-5.1. 18-bin.

How do you fix DriverManager error?

If you are not import mysql connector jar file these type of error can occur. So you can download jar file related to the your version and you can paste it in your own project /WEB-INF/lib folder.

How add JDBC jar to Intellij?

In the file browser, navigate to the JAR file of the JDBC driver, select it, and click OK. In the Class field, specify the value that you want to use for the driver . Click Apply.


1 Answers

Make sure you run this first:

Class.forName("com.mysql.jdbc.Driver"); 

This forces the driver to register itself, so that Java knows how to handle those database connection strings.

For more information, see the MySQL Connector reference.

like image 141
Adam Batkin Avatar answered Sep 17 '22 10:09

Adam Batkin