0

I downloaded mysql-connector-java-5.1.43. Put it in the build path. But it still gives me the error "java.sql.SQLException: No suitable driver found for jdbc:mysql"

enter image description here

String URL = "jdbc:mysql://ip address:3306/mydb";

        String userName = "yuh5";

        String password = ***************;

        try {

            Connection myConn = DriverManager.getConnection(URL, userName, password);
Hang
  • 189
  • 1
  • 13
  • Do you literally have "ip address" in your JDBC URL? That's not supposed to be the *words* "ip address", it's supposed to be an address like 192.168.1.42. Use the IP address of your MySQL server. – Bill Karwin Aug 03 '17 at 22:15
  • If it's not that, then it must be some mistake with your CLASSPATH. – Bill Karwin Aug 03 '17 at 22:17
  • Or you're using Java older than 1.6, and you didn't use `Class.forName()` to load the driver class. See https://stackoverflow.com/questions/8053095/what-is-the-actual-use-of-class-fornameoracle-jdbc-driver-oracledriver-while – Bill Karwin Aug 03 '17 at 22:19
  • @BillKarwin I put a real ip address, and my Java is 1.8. – Hang Aug 07 '17 at 15:40
  • @BillKarwin I created a "external_lib" folder in my Eclipse project and placed the connector's jar file in this folder, and then added it from "Properties -> Java Build Path". Is this correct? – Hang Aug 07 '17 at 16:04
  • I'm the wrong person to ask about Eclipse. – Bill Karwin Aug 08 '17 at 00:46

1 Answers1

0

I think you're close.

It looks like you're using possibly Eclipse or STS in terms of your IDE. It's hard to tell from your snippets...but see if this attached screen with more info helps.

I assume your connection try-catch block is inside of a method like main, or some other method within the class. When you run the java class, are you running from inside Eclipse or from the command line? If from the command line, make sure you reference the classpath properly with the -cp argument (see the example). If you're on Windows, you'll need to reference both the MySQL jar file AND the build directory where your application's classes are. Make sure you use the right separator (: for Windows, ; for Linux and OSX).

This silly little application does most of what I could see from your snippet...and if it loads the driver successfully, and connects to MySQL, it prints out the following message.

"Connection is read-only: false"

[Example] https://i.stack.imgur.com/TRfT0.png

  • what does reference the build directory where your application's classes are mean? How do I do that? – Hang Aug 04 '17 at 19:18
  • As you can see from the image above, I added the jar file in a lib folder, and then referenced it in Java build path. Also, I placed this snippet in doPost() method in the servlet class. – Hang Aug 04 '17 at 20:51