-1

hello i have mysql running on my mac and i am trying to write a java script to connect to it:

private static final String USERNAME = "root";
private static final String PASSWORD = "password";
private static final String CONN_STRING = "jdbc:mysql://local:3306/test";

public static void main(String[] args) {
    Connection conn = null;

    try {
        conn = DriverManager.getConnection(CONN_STRING,USERNAME,PASSWORD);
        System.out.println("Connected");
    }catch (SQLException e){
    System.err.println(e);
    }
}

however i keep getting this error message

java.sql.SQLException: No suitable driver found for jdbc:mysql://local:3306/test

what am i doing wrong?

sauumum
  • 1,638
  • 1
  • 19
  • 36

2 Answers2

2

First of all you have to add the Mysql jdbc driver as a external library. You can find the jar file from here. This link guides how to add the library in netbeans. Other thing is local must be localhost

private static final String USERNAME = "root";
private static final String PASSWORD = "password";
private static final String CONN_STRING = "jdbc:mysql://localhost:3306/test";

public static void main(String[] args) {
    Connection conn = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(CONN_STRING,USERNAME,PASSWORD);
        System.out.println("Connected");
    }catch (SQLException e){
    System.err.println(e);
    }
}
Sajith Herath
  • 1,002
  • 2
  • 11
  • 24
  • I have done all of that the mysql-connector-java-8.0.11 is added as a library and i import it at the beginning of the code however when the class.forName i always get an error – Olivier Maxwell May 30 '18 at 23:36
  • java.lang.RuntimeException: Uncompilable source code - unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown at testforairbus.TestFortest.main(TestForTest.java:22) this one to be precise – Olivier Maxwell May 30 '18 at 23:36
  • I think you have added a wrong library. Did you refer the which link i have given in the answer for the mysql connector jar file. latest version of original mysql connector is _5.1.46_ – Sajith Herath May 30 '18 at 23:49
  • wow it worked with your version is this an issue ?WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. – Olivier Maxwell May 31 '18 at 00:05
  • it works with the older version thank you very much for your time – Olivier Maxwell May 31 '18 at 00:08
  • @OlivierMaxwell actually its not the older version.its the latest.You were using mysql connector for .net that is totally different from java.You need java mysql connector for java projects – Sajith Herath May 31 '18 at 03:01
0

After having the driver in your classpath you need to add the below line before getting connection and change the local to localhost in CONN_STRING:

DriverManager.registerDriver(new com.mysql.jdbc.Driver ());

Something like this:

try {
        DriverManager.registerDriver(new com.mysql.jdbc.Driver ());
        conn = DriverManager.getConnection(CONN_STRING,USERNAME,PASSWORD);
        System.out.println("Connected");
    }catch (SQLException e){
    System.err.println(e);
    }
  • trying that just results in more errorsException in thread "main" java.lang.UnsupportedClassVersionError: com/mysql/jdbc/Driver : Unsupported major.minor version 52.0 at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:800) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) ......... – Olivier Maxwell May 30 '18 at 23:39
  • What is your Java version? – Bhumika Mishra May 30 '18 at 23:42
  • im using jdk 8, netbeans 8.0.2 and the mysql connector 8.0.11 – Olivier Maxwell May 30 '18 at 23:46
  • Just double check that your JDK and JRE are of the same version and then try running the code with a version 5 series of MySQL connector. – Bhumika Mishra May 30 '18 at 23:58
  • its works with an older version i still dont understand why but thank you very much – Olivier Maxwell May 31 '18 at 00:08