The Coded One

programming, algorithms, discrete math, open-source

Setting Up MySQL/JDBC Driver on Ubuntu

with 25 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

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);

		} catch (Exception e) {

Written by Mark Basmayor

March 1, 2009 at 1:32 pm

Posted in Java, Ubuntu

Tagged with , , , ,

25 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

    • In the above program why u r not created driver class


      December 14, 2017 at 5:25 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 {
    Connection con = DriverManager.getConnection(url, user, password);

    } catch (Exception e) {


    June 10, 2009 at 6:46 am

  3. what exception are you getting


    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(
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(
    at com.mysql.jdbc.MysqlIO.secureAuth411(
    at com.mysql.jdbc.MysqlIO.doHandshake(
    at com.mysql.jdbc.ConnectionImpl.coreConnect(
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(
    at com.mysql.jdbc.ConnectionImpl.createNewIO(
    at com.mysql.jdbc.ConnectionImpl.(
    at com.mysql.jdbc.JDBC4Connection.(
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(
    at java.lang.reflect.Constructor.newInstance(
    at com.mysql.jdbc.Util.handleNewInstance(
    at com.mysql.jdbc.ConnectionImpl.getInstance(
    at com.mysql.jdbc.NonRegisteringDriver.connect(
    at java.sql.DriverManager.getConnection(
    at java.sql.DriverManager.getConnection(
    at JDBCTest.main(

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


    January 21, 2011 at 9:27 am

  5. thankyou its working thanks a lot


    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!


    September 3, 2012 at 12:16 am

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


    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 Method)
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
    Could not find the main class: JDBSTest. Program will exit.


    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 {
    con = DriverManager.getConnection(“jdbc:mysql://localhost:3306/loyola”, user, password);


    catch (Exception e) {


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

    while( {


    finally {
    try {
    if (rs != null) {
    if (st != null) {
    if (con != null) {

    catch (SQLException e) {





    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.


    August 1, 2013 at 6:03 pm

  12. Thank you


    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….


    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


    May 29, 2014 at 2:17 am

  16. Thanks a lot, I’ve got a question because I’m new in ubuntu. Where is the location for the jar file?; in which folder?

    Thank you.

    Cristian Camilo

    October 18, 2014 at 2:54 am

  17. You post very interesting content here. Your website deserves much more
    traffic. It can go viral if you give it initial boost,
    i know very useful tool that can help you, simply type in google:
    svetsern traffic tips


    January 1, 2015 at 11:52 pm

  18. it not working in my system


    January 8, 2015 at 7:01 am

  19. tried it but it doesn’t work on me. I’m using ubuntu 13.10


    February 4, 2015 at 9:33 am

  20. how to connect using oracle it is giving invalid url exception

    dheeraj kadam

    March 27, 2015 at 7:15 am

  21. 私は 本当にも能力 書き込み |あなた} {ブログへ|あなたの内|あなたの|あなたのための構造|形式| レイアウトでなど。それ| 変更カスタマイズ 自分 トピックまたはあなたがたの支払った| これは、これはということですか? とにかく 珍しい希少 | それはそれはだ、書き込み| 高品質品質 | | 優れた素敵まで保つ滞在 ピア·ツー 偉大 このようなブログ 今日で ..
    国内即発 特価最安値

    国内即発 特価最安値

    November 26, 2015 at 4:59 am

  22. thank worked. installing the package solved the problem

    Sanjay Suresh

    January 3, 2016 at 5:30 am

  23. Thank you for your code to setting my mysql database connectivity in eclipse on Ubuntu. And its work for me.

    Jagdeep Yadav

    December 1, 2016 at 10:51 am

  24. thanks a ton man 🙂


    February 14, 2017 at 5:06 pm

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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

%d bloggers like this: