0

I am using the below code to make an SQL connection but am getting an error.

I am using netbeans and netbeans itself is able to connect to the database. My code for some reason not allowing me to connect and states I do not have a suitable driver. I am importing the SQL driver so I am not sure what is wrong.

Did I form something improperly?

May 20, 2013 11:06:18 AM sample_server.DatabaseManagement Connection
SEVERE: No suitable driver found for jdbc:mysql://XXXXXXX:3306/javaBBS
java.sql.SQLException: No suitable driver found for jdbc:mysql://XXXXXX:3306/javaBBS
    at java.sql.DriverManager.getConnection(DriverManager.java:604)
    at java.sql.DriverManager.getConnection(DriverManager.java:221)
    at sample_server.DatabaseManagement.Connection(DatabaseManagement.java:31)
    at sample_server.OneLiner.<init>(OneLiner.java:14)
    at sample_server.MenuSystemClass.MainMenu(MenuSystemClass.java:38)
    at sample_server.doComms.run(Sample_server.java:55)
    at java.lang.Thread.run(Thread.java:722)

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sample_server;

import com.sun.corba.se.impl.util.Version;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Aaron
 */
public class DatabaseManagement {
    DatabaseManagement(){}
    /**
     *
     */
    public void Connection() {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        String url = "jdbc:mysql://XXX.XXX.XXX.XXX:3306/javaBBS";
        String user = "java";
        String password = "notarealpassword";

        try {
            con = DriverManager.getConnection(url, user, password);
            st = con.createStatement();
            rs = st.executeQuery("SELECT VERSION()");

            if (rs.next()) {
                System.out.println(rs.getString(1));
            }

        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Version.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(Version.class.getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }
        }
    }
}


 try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch(Exception e) {
            System.out.println(e);
        }
        con = DriverManager.getConnection(url, user, password);
        st = con.createStatement();
        rs = st.executeQuery("SELECT VERSION()");

I added the above and it still throws the same error. Do I need to register Class to DriverManager some how?

2 Answers 2

5

As every JDBC Tutorial will show you, you have to call

Class.forName("com.mysql.jdbc.Driver");

to load the driver class prior to getting the Connection from the DriverManager.

Additionaly, you need to have the mysql connector in your build path.

Sign up to request clarification or add additional context in comments.

4 Comments

zetcode.com/db/mysqljava This tutorial doesn't show that. Can you show me how to load the driver class and add the connector to my build path?
@Aaron: The author of the tutorial seems to have forgotten that. You load the driver class with the piece of code I've posted, and you can follow the steps from the tutorial below Before we start to add the connector to your build path.
You need to add Class.forName("com.mysql.jdbc.Driver"); before you call con = DriverManager.getConnection(url, user, password);
1

There are a couple of things that you can try:

  1. Register the JDBC Driver using the method : Class.forName("com.mysql.jdbc.Driver"); so that the driver is loaded.

  2. In the connection url string after the name of the database: add a "?useSSL=false" such that it is jdbc:mysql://XXX.XXX.XXX.XXX:3306/javaBBS?useSSL=false

  3. If neither of it works, try re-installing the JDBC Driver

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.