This mistake comes when y'all are trying to connect to MySQL database from Java programme using JDBC but either the JDBC driver for MySQL is non available inward the classpath or it is non registered prior to calling the DriverManager.getConnection() method. In social club to acquire the connecter to the database, y'all must start register the driver using Class.forName() method. You should telephone yell upwards this method amongst the right the advert of the JDBC driver "com.mysql.jdbc.Driver" and this volition both charge together with register the driver amongst JDBC. The type four JDBC driver for MySQL is bundled into MySQL connector JAR e.g. mysql-connector-java-5.1.18-bin.jar depending upon which version of MySQL database y'all are connecting. Make certain this JAR is available inward classpath before running your Java program, otherwise Class.forName() volition non last able to notice together with charge the bird together with throw java.lang.ClassNotFoundException: com.mysql.jdbc.Driver, approximately other dreaded JDBC error, which nosotros accept seen inward the before post.
Recently I accept seen a mutual blueprint of this mistake where a Java developer running his programme Java SE half dozen expects that JDBC driver's JAR volition last automatically loaded past times JVM because of autoloading of JDBC driver characteristic of JDBC 4.0 released inward JDK half dozen but misses the play a joke on that the JDBC driver should likewise last JDBC 4.0 compliant e.g. mysql-connector-java-5.1.18-bin.jar volition last automatically loaded but older version may not, fifty-fifty if y'all run on Java 6. See Core Java Volume two - Advanced Features to acquire to a greater extent than virtually JDBC 4.0 features.
So, brand certain y'all accept both JDK half dozen together with a JDBC 4.0 compliant driver to leverage the auto-loading characteristic of JDBC 4.0 specification.
You tin give the axe come across that nosotros got the "No suitable driver found" error inward JDBC. The argue was that JDBC API couldn't notice whatever Driver corresponding to "jdbc:mysql://" URL because nosotros accept non added the MySQL connector JAR which contains the JDBC driver required to connect to MySQL database.
I prefer -cp pick because it's uncomplicated together with slow together with y'all tin give the axe come across what is included inward classpath right inward the ascendence occupation itself, no quest to worry virtually whether JAR is included inward CLASSPATH environs variable or not.
The mistake should acquire away only past times adding JDBC driver inward the classpath if y'all are running on Java 6, which supports JDBC 4.0 together with the driver is likewise JDBC 4.0 compliant e.g. mysql-connector-java-5.1.36-bin.jar. From JDBC 4.0, Java has introduced auto loading of JDBC driver, therefore y'all don't quest to charge or register it manually using Class.forName() method.
If y'all are non running on Java SE half dozen or your JDBC driver version doesn't back upwards JDBC 4.0 together with so only add together the next occupation before calling DriverManager.getConnection() to charge together with register the MySQL JDBC driver. This volition solve the work (uncomment the occupation inward higher upwards program):
This throws checked java.lang.ClassNotFoundException so makes certain y'all pick out handgrip of it. I accept non caught it to croak along the code clutter costless past times introducing endeavour together with pick out handgrip of statement.
So, inward short:
1) Just add together the MySQL JDBC JAR into classpath if y'all are running on Java SE half dozen together with driver is JDBC 4.0 compliant e.g. mysql-connector-java-5.1.36-bin.jar.
2) Alternatively, add together the MySQL JDBC driver to classpath e.g. mysql-connector-java-5.1.18-bin.jar together with telephone yell upwards the Class.forName("com.mysql.jdbc.Driver"); to charge together with register the driver before calling DriverManager.getConnection() method.
You tin give the axe likewise banking venture check out JDBC API Tutorial together with Reference (3rd Edition) to acquire to a greater extent than virtually novel features introduced inward JDBC 3.0 together with JDBC 4.0 specification together with it is likewise i of the best books to acquire JDBC API inward Java.
That's all virtually how to laid "No suitable driver establish for jdbc:mysql://localhost:3306/mysql" mistake inward Java. You tin give the axe acquire this mistake from Eclipse or NetBeans IDE acre connecting to local MySQL event listening on default port 3306, don't afraid, only follow the same approach. Drop the MySQL JDBC driver together with telephone yell upwards the Class.forName() method amongst the advert of the bird amongst implements Driver interface from JDBC API.
Further Learning
JSP, Servlets together with JDBC for Beginners: Build a Database App
Complete JDBC Programming Part 1 together with 2
Guide)How to connect to Oracle database from Java (Guide) How to connect to Microsoft SQL Server from Java (Guide) How to setup JDBC connecter Pool inward Spring + Tomcat (Guide) 10 JDBC Best Practices Java Programmer Should Follow (see here) 6 JDBC Performance Tips for Java Applications (see here)
Recently I accept seen a mutual blueprint of this mistake where a Java developer running his programme Java SE half dozen expects that JDBC driver's JAR volition last automatically loaded past times JVM because of autoloading of JDBC driver characteristic of JDBC 4.0 released inward JDK half dozen but misses the play a joke on that the JDBC driver should likewise last JDBC 4.0 compliant e.g. mysql-connector-java-5.1.18-bin.jar volition last automatically loaded but older version may not, fifty-fifty if y'all run on Java 6. See Core Java Volume two - Advanced Features to acquire to a greater extent than virtually JDBC 4.0 features.
So, brand certain y'all accept both JDK half dozen together with a JDBC 4.0 compliant driver to leverage the auto-loading characteristic of JDBC 4.0 specification.
How to reproduce "No suitable driver establish for 'jdbc:mysql://localhost:3306/"
In social club to improve sympathise this error, let's start reproduce this mistake past times executing next Java program. I await this programme to throw the "No suitable driver establish for 'jdbc:mysql://localhost:3306/" mistake because I don't accept JDBC driver inward the classpath.import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /* * Java Program to to reproduce * java.sql.SQLException: No suitable driver establish for jdbc:mysql://localhost:3306 * mistake which occurs if MySQL JDBC Driver JAR is missing * or y'all non registering the JDBC driver before calling * DriverManager.getConnection() method inward JDBC. */ public class MySQLTest { public static void main(String[] args) throws ClassNotFoundException { Connection con = null; try { String url = "jdbc:mysql://localhost:3306/mysql"; String username = "root"; String password = "root"; // Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(url, username, password); if (con != null) { System.out .println("Successfully connected to MySQL database test"); } } catch (SQLException ex) { System.out .println("An mistake occurred acre connecting MySQL databse"); ex.printStackTrace(); } } } Output An mistake occurred while connecting MySQL databse java.sql.SQLException: No suitable driver establish for jdbc:mysql://localhost:3306/mysql at java.sql.DriverManager.getConnection(DriverManager.java:596) at java.sql.DriverManager.getConnection(DriverManager.java:215)
You tin give the axe come across that nosotros got the "No suitable driver found" error inward JDBC. The argue was that JDBC API couldn't notice whatever Driver corresponding to "jdbc:mysql://" URL because nosotros accept non added the MySQL connector JAR which contains the JDBC driver required to connect to MySQL database.
How to solve "No suitable driver establish for jdbc:mysql://localhost:3306/mysql"
You tin give the axe solve this work start past times adding MySQL connector JAR, which includes JDBC driver for MySQL into classpath e.g. mysql-connector-java-5.1.18-bin.jar. It's rattling easy, only download the JAR from MySQL website together with drib it into your classpath. For Example, if y'all are running inward Eclipse together with so y'all tin give the axe drib it on the source of your projection folder. Same is truthful for Netbeans, but if y'all are running your Java programme from the ascendence prompt together with so either role -cp pick or laid the CLASSPATH equally described here.I prefer -cp pick because it's uncomplicated together with slow together with y'all tin give the axe come across what is included inward classpath right inward the ascendence occupation itself, no quest to worry virtually whether JAR is included inward CLASSPATH environs variable or not.
java -cp mysql-connector-java-5.1.18-bin.jar:. MySQLTest
The mistake should acquire away only past times adding JDBC driver inward the classpath if y'all are running on Java 6, which supports JDBC 4.0 together with the driver is likewise JDBC 4.0 compliant e.g. mysql-connector-java-5.1.36-bin.jar. From JDBC 4.0, Java has introduced auto loading of JDBC driver, therefore y'all don't quest to charge or register it manually using Class.forName() method.
If y'all are non running on Java SE half dozen or your JDBC driver version doesn't back upwards JDBC 4.0 together with so only add together the next occupation before calling DriverManager.getConnection() to charge together with register the MySQL JDBC driver. This volition solve the work (uncomment the occupation inward higher upwards program):
Class.forName("com.mysql.jdbc.Driver");
This throws checked java.lang.ClassNotFoundException so makes certain y'all pick out handgrip of it. I accept non caught it to croak along the code clutter costless past times introducing endeavour together with pick out handgrip of statement.
So, inward short:
1) Just add together the MySQL JDBC JAR into classpath if y'all are running on Java SE half dozen together with driver is JDBC 4.0 compliant e.g. mysql-connector-java-5.1.36-bin.jar.
2) Alternatively, add together the MySQL JDBC driver to classpath e.g. mysql-connector-java-5.1.18-bin.jar together with telephone yell upwards the Class.forName("com.mysql.jdbc.Driver"); to charge together with register the driver before calling DriverManager.getConnection() method.
You tin give the axe likewise banking venture check out JDBC API Tutorial together with Reference (3rd Edition) to acquire to a greater extent than virtually novel features introduced inward JDBC 3.0 together with JDBC 4.0 specification together with it is likewise i of the best books to acquire JDBC API inward Java.
That's all virtually how to laid "No suitable driver establish for jdbc:mysql://localhost:3306/mysql" mistake inward Java. You tin give the axe acquire this mistake from Eclipse or NetBeans IDE acre connecting to local MySQL event listening on default port 3306, don't afraid, only follow the same approach. Drop the MySQL JDBC driver together with telephone yell upwards the Class.forName() method amongst the advert of the bird amongst implements Driver interface from JDBC API.
Further Learning
JSP, Servlets together with JDBC for Beginners: Build a Database App
Complete JDBC Programming Part 1 together with 2
Guide)
No comments:
Post a Comment