11684
11684

Reputation: 7507

How to import com.mysql.jdbc.Driver

I've been trying for a while to connect to my database, and I am pretty close now I think.
But com.mysql.jdbc.Driver refuses to load.
Here's my code:

import java.sql.*;
import com.apple.eawt.*;
import com.mysql.jdbc.Driver;

public class MySQL {
    public MySQL() {
        Connection conn = null;

        try
        {
            String userName = "my_username";
            String password = "*******";
            String url = "jdbc:mysql://korilu.nl/phpMyAdmin/";
            Class.forName ("com.mysql.jdbc.Driver").newInstance ();
            conn = DriverManager.getConnection (url, userName, password);
            System.out.println ("Database connection established");
        }
        catch (SQLException ex)
        {
            System.err.println ("Cannot connect to database server (SQLException)");
            System.out.println(ex.getMessage());
        }
        catch (ClassNotFoundException ex) {
            System.err.println ("Cannot connect to database server (ClassNotFoundException)");
            System.out.println(ex.getMessage());
        }
        catch (InstantiationException ex) {
            System.err.println ("Cannot connect to database server (InstantiationException)");
            System.out.println(ex.getMessage());
        }
        catch (IllegalAccessException ex) {
            System.err.println ("Cannot connect to database server (IllegalAccesException)");
            System.out.println(ex.getMessage());
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close ();
                    System.out.println ("Database connection terminated");
                }
                catch (Exception e) { /* ignore close errors */ }
            }
        }
    }

    public static void main(String args[]) {
        new MySQL();
    }

}

Currently it says: package com.mysql.jdbc does not exist, although XCode autocompletes the import line above.
I think I messed up with adding the package to my project, because I did not know how.
I just ctrl+clicked my target and selected Add to target... and added the com folder with the .java files I downloaded.
I think that the compiler doesn't recognize it as a package but I don't know how to make it a package.

P.S.
I'm not quite sure about that URL, but that's not the problem for now (but I won't mind someone telling me where antagonist stores their databases).

Upvotes: 1

Views: 14896

Answers (2)

Vishal Sharma
Vishal Sharma

Reputation: 112

Just add this library file in your libraries if you haven't yet.

mysql connector 5.1

Upvotes: 1

rooftop
rooftop

Reputation: 3027

I am not sure how Xcode handles this as I've not used it for Java development, however the usual cause of this error is the MySQL jdbc (connector) jar file not being on the classpath. Can you show us the command Xcode is using to launch the jvm?

Upvotes: 1

Related Questions