How To Solve Java.Sql.Sqlexception: No Suitable Driver Establish For Jdbc:Mysql://Localhost:3306/Test

The mistake "java.sql.SQLException: No suitable driver works life for jdbc:mysql://localhost:3306/test" occurs when you lot endeavour to connect MySQL database running on your localhost, listening on port 3306 port from Java computer program but either you lot don't bring MySQL JDBC driver inwards your classpath or driver is non registered earlier calling the getConnection() method. Since JDBC API is purpose of JDK itself, when you lot write a Java computer program to connect whatsoever database similar MySQL, SQL Server or Oracle, everything compiles fine, equally you lot alone utilization classes from JDK but at runtime, when the JDBC driver which is required to connect to database is non available, JDBC API either throws this mistake or "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver".

Most mutual argue of this mistake is missing MySQL JDBC Driver JAR e.g. mysql-connector-java-5.0.8.jar non available inwards classpath. Another mutual argue is you lot are non registering the driver earlier calling the  getConnection() as well as you lot are running on Java version lower than vi as well as non using a JDBC 4.0 compliant driver. We'll meet these reasons inwards to a greater extent than exceptional inwards this article.


JAR non available inwards Classpath

If mysql-connector-java-5.0.8.jar is non available inwards classpath as well as then you lot cannot connect to MySQL database from Java. Your computer program similar below volition compile fine but equally presently equally you lot volition run it you lot volition acquire the mistake "java.sql.SQLException: No suitable driver works life for jdbc:mysql://localhost:3306/test" because of the JDBC URL format "jdbc:mysql" is non matching alongside whatsoever registered JDBC driver.


Here is our Java computer program to demonstrate this error. This computer program reproduce this mistake past times start leaving out the required JDBC JAR from classpath as well as besides non explicitly registering the driver earlier utilization past times non calling Class.forName() method.

import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties;  /*  * Java Program to to connect to MySQL database as well as  * ready java.sql.SQLException: No suitable driver works life for jdbc:mysql://localhost:3306  * mistake which tumble out if JAR is missing or you lot neglect to register driver.  */  public class Main {    public static void main(String[] args) {      Connection dbConnection = null;      try {       String url = "jdbc:mysql://localhost:3306/test";       Properties information = new Properties();       info.put("user", "root");       info.put("password", "test");        dbConnection = DriverManager.getConnection(url, info);        if (dbConnection != null) {         System.out.println("Successfully connected to MySQL database test");       }      } catch (SQLException ex) {       System.out.println("An mistake occurred piece connecting MySQL databse");       ex.printStackTrace();     }    }  }  Output An mistake occurred while connecting MySQL databse java.sql.SQLException: No suitable driver works life for jdbc:mysql://localhost:3306/test at java.sql.DriverManager.getConnection(DriverManager.java:596) at java.sql.DriverManager.getConnection(DriverManager.java:187) 
at Main.main(Main.java:24))

 port from Java computer program but either you lot don How to solve java.sql.SQLException: No suitable driver works life for jdbc:mysql://localhost:3306/test



You request to make ii things inwards lodge to solve this problem:

1) Add mysql-connector-java-5.0.8.jar or whatsoever other MySQL JAR corresponding to the MySQL database you lot are connecting. If you lot don't bring MySQL JDBC driver, you lot tin forcefulness out download from hither http://dev.mysql.com/downloads/connector/j/3.1.html

2) Add next business of code but earlier the telephone band to Connection.getConnection(url, props) method

// charge and register JDBC driver for MySQL Class.forName("com.mysql.jdbc.Driver"); 

This volition charge the class, the JDBC driver to connect MySQL, com.mysql.jdbc.Driver from mysql-connector-java-5.0.8.jar as well as register it alongside JDBC API. Once you lot make that "java.sql.SQLException: No suitable driver works life for jdbc:mysql://localhost:3306/test" volition acquire away.


Also, it's worth noting that JDBC 4.0 released alongside Java SE vi has straight off introduced auto-loading of JDBC driver class, which way you lot don't request Class.forName("com.mysql.jdbc.Driver"); whatsoever more, but alone when you lot are running on at to the lowest degree Java vi as well as your driver JAR is besides JDBC 4.0 compliant

For example, the driver used inwards this computer program "mysql-connector-java-5.0.8.jar" is non JDBC 4.0 compliant, thus fifty-fifty if you lot run this computer program inwards Java 6, seven or Java 8, it volition non work, but if you lot utilization mysql-connector-java-5.1.36.jar as well as then fifty-fifty without adding "Class.forName("com.mysql.jdbc.Driver");", your computer program volition run fine. Why? because JDBC volition automatically charge as well as register the driver, provided you lot bring mysql-connector-java-5.1.36.jar file inwards your classpath. . See Core Java Volume 2 - Advanced features to larn to a greater extent than virtually novel features introduces inwards JDBC 3.0 as well as JDBC 4.0 releases.

 port from Java computer program but either you lot don How to solve java.sql.SQLException: No suitable driver works life for jdbc:mysql://localhost:3306/test


That's all virtually how to solve "java.sql.SQLException: No suitable driver works life for jdbc:mysql://localhost:3306/test" mistake inwards Java program. This mistake occurs if JDBC is non able to respect a suitable driver for the URL format passed to getConnection() method e.g. "jdbc:mysql://" inwards our case. In lodge to solve this error, you lot request the MySQL JDBC driver e.g. mysql-connector-java-5.1.36.jar inwards your classpath. If you lot utilization a driver which is non JDBC 4.0 compliant as well as then you lot besides request to telephone band the Class.forName("com.mysql.jdbc.Driver") method to charge as well as register the driver.


Other JDBC troubleshooting guides for Java Programmers
  • How to connect to MySQL database from Java Program [steps]
  • How to connect Microsoft SQL Server from Java Program [guide]
  • How to solve java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver inwards Java? [solution]
  • How to connect Oracle database from Java as well as Eclipse? [guide]
  • How to ready java.lang.ClassNotFoundException: org.postgresql.Driver mistake inwards Java? [solution]
  • SQLServerException: The index 58 is out of hit - JDBC [solution]
  • How to fix java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver inwards Java? [solution]
  • java.sql.SQLException: No suitable driver works life for 'jdbc:mysql://localhost:3306/mysql [Solution]
  • java.lang.ClassNotFoundException: com.mysql.jdbc.Driver [Solution]


Further Learning
JSP, Servlets as well as JDBC for Beginners: Build a Database App
Complete JDBC Programming Part 1 as well as 2
Java Platform: Working alongside Databases Using JDBC

0 Response to "How To Solve Java.Sql.Sqlexception: No Suitable Driver Establish For Jdbc:Mysql://Localhost:3306/Test"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel