ED209
ED209

Reputation: 638

Java Connecting to a MySQL Database using JDBC

I am trying to create a program that connects to a MySQL database using JDBC. But when I try to run it I get the following errors:-

java.sql.SQLException: No suitable driver found for a9442ca6-992c-411b-8bda-a42f00a0ab2e.mysql.sequelizer.com
    at java.sql.DriverManager.getConnection(DriverManager.java:596)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at DVDLibrary.MySQLDBConnection.testConnect(MySQLDBConnection.java:24)
    at DVDLibrary.MainClass.main(MainClass.java:12)

I have installed Maven and have added the MySQL dependency to the POM file (see below). But still cant get my program to work. Please could somebody help?

<dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.17</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

package DVDLibrary;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MySQLDBConnection {

        public void testConnect(){

        String dbUrl = "xxx";
        String username  = "xxx";
        String password  = "xxx";
        String dbClass = "com.mysql.jdbc.Driver";

        String query = "SELECT * FROM DVD Info Table";

        try {

            Class.forName(dbClass);
            Connection connection = DriverManager.getConnection(dbUrl, username, password);

            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(query);

            while (resultSet.next()) {
                String tableName = resultSet.getString(1);
                System.out.println(tableName);
            }
            connection.close();

        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

Other class:-

package DVDLibrary;

import oracle.jrockit.jfr.tools.ConCatRepository;


public class MainClass {

     public static void main(String []args)
    {
    MySQLDBConnection con = new MySQLDBConnection();

    con.testConnect();

        if(con != null)
        {
            System.out.println("Succes"); 

        }
        else
        {
            System.out.println("Fail"); 
        }

    }

}

Upvotes: 0

Views: 1342

Answers (2)

Chatz
Chatz

Reputation: 348

You have to add mysql-connector-java-5.1.18-bin jar to your libraries.

This is a sample class with methods to open and close database connections.

public class Database {
    public static Connection con() throws ClassNotFoundException, SQLException{
        Class.forName("com.mysql.jdbc.Driver");
        Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/DatabaseName", "User", "Password");

        return c ;
    }

    public static void con_close(Connection c) {
        try {
            if(c!=null)
            c.close();
        } catch (SQLException sQLException) {
            System.out.println(sQLException + "Database connection closing failure");
        }
    }

        public static void stmt_close(Statement s) {
        try {
            if(s!=null)
            s.close();
        } catch (SQLException sQLException) {
            System.out.println(sQLException + "Statement closing failure");
        }
    }
    public static void rs_close(ResultSet r) {
        try {
            if(r!=null)
            r.close();
        } catch (SQLException sQLException) {
            System.out.println(sQLException + "ResultSet closing failure");
        }
    }


}

Upvotes: 1

Zuko
Zuko

Reputation: 2914

Please add the mysql JDBC Driver to your classpath and try again. You can download it fromhere or here if you don't have it and load it. It should work out just fine. Here is a sample code:

 Connection getConnection() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql:localhost:3306/Database", "username",
                "password");
        if (!conn.isClosed())
            return conn;
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    }
    return null;
}

Upvotes: 1

Related Questions