Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-13-2007, 09:33 PM   #1
Registered: Aug 2004
Distribution: Lubuntu Live OS
Posts: 432

Rep: Reputation: 30
JDBC connection, mysql-connector-java


I am trying to connect a java program to MySQL.

I have downloaded the following package:
mysql-connector-java-nnn , and installed it in my /usr/local/bin/mysql-connector-java-nnn/ directory. According to a number of posts, all that I really need from this is the mysql-connector-java.jar package.

I then run the following Java script to see if it is working;
import java.sql.*;

public class TestMysql
public static void main(String args[]) {
try {
/* Test loading driver */

String driver = "com.mysql.jdbc.Driver";

System.out.println( "\n=> loading driver:" );
Class.forName( driver ).newInstance();
System.out.println( "OK" );

/* Test the connection */

String url = "jdbc:mysql://localhost:3306/<dir>";

System.out.println( "\n=> connecting:" );
DriverManager.getConnection( url, "user", "password" );

System.out.println( "OK" );
catch( Exception x ) {
System.err.println( x );
and I get the following error:

Any help would be greatly appreciated

Last edited by fhleung; 08-14-2007 at 05:31 AM.
Old 08-13-2007, 10:53 PM   #2
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Sorry for the obvious question, but is com.mysql.jdbc.Driver in your classpath? If not, make sure it is.
export CLASSPATH=$CLASSPATH:/usr/local/bin/mysql-connector-java-3.1.12/mysql-connector-java-3.1.12-bin.jar
You may have to change this, of course, but I'm guessing that is the path.
Old 08-14-2007, 05:24 AM   #3
Registered: Aug 2004
Distribution: Lubuntu Live OS
Posts: 432

Original Poster
Rep: Reputation: 30
The program can be compiled OK. When run the program

The error should be:

Error during query: Unexpected Exception: message given: null

Nested Stack Trace:


at java.lang.String.init(
at java.lang.String.<init>(
at com.mysql.jdbc.SingleByteCharsetConverter.<init>(
at com.mysql.jdbc.SingleByteCharsetConverter.initCharset(
at com.mysql.jdbc.SingleByteCharsetConverter.getInstance(
at com.mysql.jdbc.Connection.getCharsetConverter(
at com.mysql.jdbc.StringUtils.getBytes(
at com.mysql.jdbc.Buffer.writeStringNoNull(
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(
at com.mysql.jdbc.Connection.execSQL(
at com.mysql.jdbc.Connection.configureClientCharacterSet(
at com.mysql.jdbc.Connection.initializePropsFromServer(
at com.mysql.jdbc.Connection.createNewIO(
at com.mysql.jdbc.Connection.<init>(
at com.mysql.jdbc.Connection.getInstance(
at com.mysql.jdbc.NonRegisteringDriver.connect(
at java.sql.DriverManager.getConnection(
at java.sql.DriverManager.getConnection(
at program.main( <- this is line of DriverManager.getConnection( url, "user", "password" );


Only the last line from the program and all from the connector.
Any thing wrong with the String url = "jdbc:mysql://localhost/database or the SQL query statement?
Old 08-15-2007, 07:40 PM   #4
Registered: Aug 2004
Distribution: Lubuntu Live OS
Posts: 432

Original Poster
Rep: Reputation: 30
JDBC connection, mysql-connector-java

Did anyone ever connect to MySQL with Java? The config file my.cnf need to be modified?
Old 08-16-2007, 12:49 AM   #5
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
One additional suggestion -

Are you using GNU Java (GJC)? If so, please consider downloading the Sun JDK from (forget about GJC).

Folks in this link experienced the same problem, and fixed it by changing JREs (e.g. to Sun Java 1.5). I would encourage you to try the same:,...410#msg-150410

'Hope that helps .. PSM


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mysql and mysql java connector, connection refused arubin Slackware 3 03-29-2008 01:41 AM
jdbc mysql java - please help me j0hnd0e Linux - Software 2 07-22-2006 05:23 AM
Where to put mysql-connector-java folder dukehazord Linux - General 1 09-10-2004 11:30 AM
jdbc connection in java balanagireddy Programming 2 07-01-2004 06:42 AM
JDBC connection in java balanagireddy Programming 1 06-30-2004 07:34 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:24 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration