The Coded One

programming, algorithms, discrete math, open-source

Setting Up MySQL/JDBC Driver on Ubuntu

with 8 comments

Assuming that you already have MySQL installed, the next step is to install the connector driver. You can do this easily on the CLI by using the following command:

sudo apt-get install libmysql-java

The next step is to make sure that the classpath is set. You can have this set automatically by adding this command to you bashrc file.

export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar

If you want to set this for all users, you should modify the /etc/environment instead.

For those using Eclipse, you can also do this by going through the following steps:

  1. Select Project Properties > Java Build Path
  2. Select Libries tab
  3. Click Add External Jars
  4. Choose the jar file, in this case mysql-connector-java.java

Once you’re done, you can test the connection using the following snippet:

import java.sql.Connection;
import java.sql.DriverManager;

class JDBCTest {

	private static final String url = "jdbc:mysql://localhost";

	private static final String user = "username";

	private static final String password = "password";

	public static void main(String args[]) {
		try {
			Connection con = DriverManager.getConnection(url, user, password);
			System.out.println("Success");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
About these ads

Written by Mark Basmayor

March 1, 2009 at 1:32 pm

Posted in Java, Ubuntu

Tagged with , , , ,

8 Responses

Subscribe to comments with RSS.

  1. could you show me the way to set this class path in steps:
    export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar

    jean paul

    June 7, 2009 at 5:46 am

  2. Your code was giving me an SQL exception.
    This is what worked for me..

    import java.sql.Connection;
    import java.sql.DriverManager;

    class JDBCTest {

    private static final String url = “jdbc:mysql://localhost”;

    private static final String user = “root”;

    private static final String password = “vision$23″;

    public static void main(String args[]) {
    try {
    Class.forName(“com.mysql.jdbc.Driver”);
    Connection con = DriverManager.getConnection(url, user, password);
    System.out.println(“Success”);

    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    monzta

    June 10, 2009 at 6:46 am

  3. what exception are you getting

    mantis

    August 18, 2009 at 3:19 pm

  4. im also getting d following exception after trying the code :

    java.sql.SQLException: Access denied for user ‘root’@'localhost’ (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:931)
    at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4031)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1296)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2338)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2371)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2163)
    at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:794)
    at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:378)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
    at java.sql.DriverManager.getConnection(DriverManager.java:620)
    at java.sql.DriverManager.getConnection(DriverManager.java:200)
    at JDBCTest.main(JDBCTest.java:14)

    i changed d username and password.. it didnt work!

    x2am

    January 21, 2011 at 9:27 am

  5. thankyou its working thanks a lot

    thishanth

    March 17, 2012 at 1:04 pm

  6. Reblogged this on Srikanth's Blog.

    srikanth ganta

    August 17, 2012 at 2:23 am

  7. Pretty great post. I just stumbled upon your weblog and wished to say that I’ve truly loved browsing your blog posts. After all I will be subscribing for your rss feed and I’m hoping you write once more soon!

    MARLA

    September 3, 2012 at 12:16 am

  8. That was the most briliant idea i’ve ever come across. Thanks alot man.

    Douglas

    September 14, 2012 at 4:34 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: