0

I'm trying to establish a connection to my mySQL database through a jdbc connection. I'm using a localhost connection at port 3006.

try{
   Connection myConn = DriverManager.getConnection("JDBC:mysql://localhost:3306/cupcakes", "kristoffer", "******");
   Statement myStmt = myConn.createStatement();

   ResultSet myRs = myStmt.executeQuery("Select * from cupcakes");
   while(myRs.next()){
       System.out.println("Cupcakes: " + myRs.getString("name"));

i get this error: java.sql.SQLException: No suitable driver found for JDBC:mysql://localhost:3306/cupcakes at java.sql.DriverManager.getConnection(DriverManager.java:689) at java.sql.DriverManager.getConnection(DriverManager.java:247) at driverappformysql.DriverAppforMYSQL.main(DriverAppforMYSQL.java:21)

How can i assure that i'm connecting to the correct database, and how can i get the right url?

  • Possible duplicate of [No suitable driver found for 'jdbc:mysql://localhost:3306/mysql](https://stackoverflow.com/questions/8146793/no-suitable-driver-found-for-jdbcmysql-localhost3306-mysql) or https://stackoverflow.com/questions/1911253/the-infamous-java-sql-sqlexception-no-suitable-driver-found – Lukasz Szozda Feb 25 '18 at 16:24
  • Try lowercase: `jdbc:mysql://localhost:3306/cupcakes`. I've never seen it using uppercase `JDBC:` prefix, and it's likely case-sensitive. – Andreas Feb 25 '18 at 16:47

2 Answers2

2

Specify driver class..add this line before opening connection to database

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

Try this

try{
   Class.forName("com.mysql.jdbc.Driver");
   Connection myConn = DriverManager.getConnection("JDBC:mysql://localhost:3306/cupcakes", "kristoffer", "******");
   Statement myStmt = myConn.createStatement();

   ResultSet myRs = myStmt.executeQuery("Select * from cupcakes");
   while(myRs.next()){
       System.out.println("Cupcakes: " + myRs.getString("name"));

Further after successful connection..execute this query to get selected database

SELECT DATABASE() FROM DUAL;
Sodium
  • 1,016
  • 1
  • 9
  • 22
0

Add

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

Before opening connection.

Make sure you have added MySql to jdbc connector jar file which you can download on mysql's website.

pritam parab
  • 85
  • 2
  • 10