The Coded One

programming, algorithms, discrete math, open-source

Setting Up MySQL/JDBC Driver on Ubuntu

with 15 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 , , , ,

15 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

  9. your code give the following errors
    Exception in thread “main” java.lang.NoClassDefFoundError: JDBSTest
    Caused by: java.lang.ClassNotFoundException: JDBSTest
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    Could not find the main class: JDBSTest. Program will exit.

    noel

    May 24, 2013 at 9:33 am

  10. This code will work

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

    class JDBCTest {

    private static String user =”root”;
    private static String password = “123456″;
    private static Connection con=null;
    private static Statement st=null;
    private static ResultSet rs=null;

    public static void main(String args[])throws Exception {
    try {
    Class.forName(“com.mysql.jdbc.Driver”);
    con = DriverManager.getConnection(“jdbc:mysql://localhost:3306/loyola”, user, password);

    }

    catch (Exception e) {
    System.out.println(“e”);
    }

    try{

    st=con.createStatement();
    rs=st.executeQuery(“select * from student”);

    while(rs.next()) {
    System.out.println(rs.getString(“Last_Name”));
    }

    }

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

    }
    catch (SQLException e) {
    System.out.println(e);
    }

    }

    }

    }

    loyola

    July 23, 2013 at 11:10 am

  11. This post saved me so much time, I was able to use your post to connect Open Office Base with Mysql database. After sudo apt-get install libmysql-java — I created the class path by going to open office writer >tools>options>advanced >class path (button ) and navigating to /usr/share/java/mysql-connector-java-5.1.16.jar — then restart Open Office as it tells you.
    assuming you have Mysql client and server already installed and your database already accessible via command line.

    Damian

    August 1, 2013 at 6:03 pm

  12. Thank you

    James

    November 22, 2013 at 4:42 pm

  13. Good day I am so thrilled I found your blog page, I really found you
    by mistake, while I was researching on Aol for something else, Anyways I am here
    now and would just like to say thank you for a marvelous post and a all round enjoyable blog (I also love the theme/design), I don’t have time to browse it all at
    the moment but I have saved it and also included your RSS
    feeds, so when I have time I will be back to read a lot more, Please do keep up the excellent job.

  14. thank you it’s so helpful….

    lp

    April 24, 2014 at 9:04 am

  15. I was looking through some of your blog posts on this internet site and I conceive this web site is rattling informative ! Keep on posting . cadekdgcgbae

    Johnb498

    May 29, 2014 at 2:17 am


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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: