Вы находитесь на странице: 1из 10

The Coded One

programming, algorithms, discrete math, open-source

Setting Up MySQL/JDBC Driver on Ubuntu


with 26 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:

Select Project Properties > Java Build Path


Select Libries tab
Click Add External Jars
Choose the jar file, in this case mysql-connector-java.java
Once you’re done, you can test the connection using the following snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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();
}
}
}
Advertisements

REPORT THIS AD
Share this:
TwitterFacebookReddit

Related
Setting Up Tomcat on Ubuntu
In "Java"
Handy Shell Commands
In "Linux"
Introduction to SOAP
In "PHP"
Written by Mark Basmayor

March 1, 2009 at 1:32 pm

Posted in Java, Ubuntu

Tagged with database, Java, jdbc, Linux, Ubuntu

« Writing Better Conditional StatementsSetting Up Tomcat on Ubuntu »


26 Responses
Subscribe to comments with RSS.

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

Reply
In the above program why u r not created driver class
mastan

December 14, 2017 at 5:25 am

Reply
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

Reply
what exception are you getting

mantis

August 18, 2009 at 3:19 pm

Reply
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:237
1)
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(NativeConstructorAc
cessorImpl.java:57)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConst
ructorAccessorImpl.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

Reply
any solution i m also facing same issues

abhey

August 24, 2018 at 10:40 am

Reply
thankyou its working thanks a lot

thishanth

March 17, 2012 at 1:04 pm


Reply
Reblogged this on Srikanth's Blog.

srikanth ganta

August 17, 2012 at 2:23 am

Reply
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

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

Douglas

September 14, 2012 at 4:34 pm

Reply
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

Reply
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

Reply
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

Reply
Thank you

James

November 22, 2013 at 4:42 pm

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

how to approach a girl you just saw

December 27, 2013 at 2:43 pm

Reply
thank you it’s so helpful….

lp
April 24, 2014 at 9:04 am

Reply
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

Reply
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

Reply
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

Kazuko

January 1, 2015 at 11:52 pm

Reply
it not working in my system

chandu

January 8, 2015 at 7:01 am

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

cath

February 4, 2015 at 9:33 am

Reply
how to connect using oracle it is giving invalid url exception
dheeraj kadam

March 27, 2015 at 7:15 am

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

国内即発 特価最安値

November 26, 2015 at 4:59 am

Reply
thank you.it worked. installing the package solved the problem

Sanjay Suresh

January 3, 2016 at 5:30 am

Reply
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

Reply
thanks a ton man 🙂

vinay

February 14, 2017 at 5:06 pm

Reply

Leave a Reply

Enter your comment here...


Search for:
Recent Posts
Using Disqus Plugin in Joomla
TiddlyWiki + TiddlySnip + TiddlySpot as a Google Notebook Alternative
Enable mod rewrite in Apache2
Which Programming Language Are You?
Setting Up Tomcat on Ubuntu
Categories
apache
Best Coding Practices
Java
Javascript
Joomla
Linux
PHP
productivity
random thoughts
Ubuntu
apache apache2 bash bugs coding practices comment conditional statements
curl database data structure disqus google notebook ide introduction Java
Javascript jdbc Joomla Linux linux tutorial mod rewrite nostalgia office
overview PHP plugin programming techniques programming tips shell snippet
soap source control subversion svn terminal tiddlysnip tiddlyspot tiddlywiki
tomcat tutorial Ubuntu versioning vim web services
Blogroll
Coding (the life)
Coding Horror
Lifehacker
Ubuntu Geek
Syndication
rss_ico Subscribe to main content
rss_ico Subscribe to comments
Bookmark and Share
Advertisements

REPORT THIS AD
Create a free website or blog at WordPress.com.

Privacy & Cookies: This site uses cookies. By continuing to use this website,
you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
:)

Вам также может понравиться