1

I'm trying to connect my Java program to a database, but for some reason I'm getting an exception telling me there's No suitable driver found.

This is the code I use to connect:

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

java.sql.Connection connect = DriverManager.getConnection("jdbc:mysql//localhost:3307/miku");

PreparedStatement statement = connect.prepareStatement("SELECT * FROM users");

ResultSet result = statement.executeQuery();

Does anyone see what the problem is?

Ambrish
  • 3,627
  • 2
  • 27
  • 42
user3231227
  • 67
  • 1
  • 8

2 Answers2

0

You need to add MySQL connector driver jar. Download it from here and add it to your classpath.

PS : If you are using Java 7 you don't even have to write Class.forName("com.mysql.jdbc.Driver");. it will automatically be loaded from the classpath. So just add your driver jar file to the classpath.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • I've already added the driver to the classpath but I'm still getting the error. Is there something wrong in my url? – user3231227 Jun 25 '14 at 10:44
  • If your URL is not correct you will get a different exception. This is because it is not able to load the driver class. – Aniket Thakur Jun 25 '14 at 10:59
0

Just add mysql connector jar. If you using Netbeans right click on the Libraries folder on project and then add library. There is library called MySql JDBC Driver and add it.

Harsha
  • 377
  • 1
  • 8
  • 21