Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbname

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbname

    This program i stored as MySQLConnectExample.java
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;

    public class MySQLConnectExample {
    public static void main(String[] args) {

    // creates three different Connection objects
    Connection conn1 = null;
    Connection conn2 = null;
    Connection conn3 = null;

    try {
    // connect way #1
    String url1 = "jdbc:mysql://localhost:3306/aavikme";
    String user = "root";
    String password = "aa";

    conn1 = DriverManager.getConnection(url1, user, password);
    if (conn1 != null) {
    System.out.println("Connected to the database test1");
    }

    // connect way #2
    String url2 = "jdbc:mysql://localhost:3306/aavikme?user=root&password=aa";
    conn2 = DriverManager.getConnection(url2);
    if (conn2 != null) {
    System.out.println("Connected to the database test2");
    }

    // connect way #3
    String url3 = "jdbc:mysql://localhost:3306/aavikme";
    Properties info = new Properties();
    info.put("user", "root");
    info.put("password", "aa");

    conn3 = DriverManager.getConnection(url3, info);
    if (conn3 != null) {
    System.out.println("Connected to the database test3");
    }
    } catch (SQLException ex) {
    System.out.println("An error occurred. Maybe user/password is invalid");
    ex.printStackTrace();
    }
    }
    }

    i run the program through cmd
    like this
    E:\java mysql code driver>javac MySQLConnectExample.java

    E:\java mysql code driver>java -cp mysql-connector-java-3.0.11-stable-bin.jar;.
    MySQLConnectExample
    An error occurred. Maybe user/password is invalid
    java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/
    aavikme
    at java.sql.DriverManager.getConnection(DriverManager .java:596)
    at java.sql.DriverManager.getConnection(DriverManager .java:215)
    at MySQLConnectExample.main(MySQLConnectExample.java: 20)
    this is how i stored my file
    java+mysql.jpg


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbname

    Quote Originally Posted by aavik View Post
    java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/
    aavikme
    You miss one thing: the registration of the driver. Please, see here MySQL :: MySQL Connector/J Developer Guide :: 6.1 Connecting to MySQL Using the JDBC DriverManager Interface
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbname

    this is error while compiling
    --E:\java mysql code driver>javac MySQLConnectExample.java MySQLConnectExample.java:16: error: unreported exception ClassNotFoundException; must be caught or declared to be thrown Class.forName("com.mysql.jdbc.Driver"); ^ 1 error –

    --- Update ---

    import java.sql.*;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;

    public class MySQLConnectExample {
    static
    {
    try
    {
    // loads com.mysql.jdbc.Driver into memory
    Class.forName("com.mysql.jdbc.Driver");
    }
    catch (ClassNotFoundException cnf)
    {
    System.out.println("Driver could not be loaded: " + cnf);
    }
    }

    public static void main(String[] args) {

    // creates three different Connection objects
    Connection conn1 = null;
    Connection conn2 = null;
    Connection conn3 = null;

    try {

    AS IT IS ABOVE
    DONE IT

    --- Update ---

    import java.sql.*;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;

    public class MySQLConnectExample {
    static
    {
    try
    {
    // loads com.mysql.jdbc.Driver into memory
    Class.forName("com.mysql.jdbc.Driver");
    }
    catch (ClassNotFoundException cnf)
    {
    System.out.println("Driver could not be loaded: " + cnf);
    }
    }

    public static void main(String[] args) {

    // creates three different Connection objects
    Connection conn1 = null;
    Connection conn2 = null;
    Connection conn3 = null;
    try {
    Class.forName("com.mysql.jdbc.Driver");

    // connect way #1
    String url1 = "jdbc:mysql://localhost:3306/aavikme";
    String user = "root";
    String password = "aa";

    conn1 = DriverManager.getConnection(url1, user, password);
    if (conn1 != null) {
    System.out.println("Connected to the database test1");
    }

    // connect way #2
    String url2 = "jdbc:mysql://localhost:3306/aavikme?user=root&password=aa";
    conn2 = DriverManager.getConnection(url2);
    if (conn2 != null) {
    System.out.println("Connected to the database test2");
    }

    // connect way #3
    String url3 = "jdbc:mysql://localhost:3306/aavikme";
    Properties info = new Properties();
    info.put("user", "root");
    info.put("password", "aa");

    conn3 = DriverManager.getConnection(url3, info);
    if (conn3 != null) {
    System.out.println("Connected to the database test3");
    }
    } catch (SQLException ex) {
    System.out.println("An error occurred. Maybe user/password is invalid");
    ex.printStackTrace();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    DONE IT

    run as
    E:\java mysql code driver>javac MySQLConnectExample .java

    E:\java mysql code driver>java -cp . MySQLConnectExample

Similar Threads

  1. No suitable driver found for jdbc:mysql://localhost/books
    By Moses76 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 30th, 2012, 12:17 PM
  2. Replies: 2
    Last Post: January 9th, 2012, 10:03 AM
  3. java.sql.SQLException: No data found
    By Virender in forum JDBC & Databases
    Replies: 2
    Last Post: December 7th, 2011, 01:17 PM
  4. Replies: 2
    Last Post: November 17th, 2011, 08:03 AM
  5. No suitable driver found
    By Josheh in forum JDBC & Databases
    Replies: 2
    Last Post: October 16th, 2011, 01:12 AM

Tags for this Thread