LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

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


Hello,

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;
Code:
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
95se
Member
 
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.
Code:
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
fhleung
Member
 
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: java.io.CharConversionException message given: null

Nested Stack Trace:


** BEGIN NESTED EXCEPTION **

java.io.CharConversionException

STACKTRACE:

java.io.CharConversionException
at gnu.gcj.convert.Input_iconv.read(libgcj.so.8rh)
at java.lang.String.init(libgcj.so.8rh)
at java.lang.String.<init>(libgcj.so.8rh)
at com.mysql.jdbc.SingleByteCharsetConverter.<init>(SingleByteCharsetConverter.java:153)
at com.mysql.jdbc.SingleByteCharsetConverter.initCharset(SingleByteCharsetConverter.java:108)
at com.mysql.jdbc.SingleByteCharsetConverter.getInstance(SingleByteCharsetConverter.java:86)
at com.mysql.jdbc.Connection.getCharsetConverter(Connection.java:2658)
at com.mysql.jdbc.StringUtils.getBytes(StringUtils.java:615)
at com.mysql.jdbc.Buffer.writeStringNoNull(Buffer.java:655)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1675)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2430)
at com.mysql.jdbc.Connection.configureClientCharacterSet(Connection.java:1707)
at com.mysql.jdbc.Connection.initializePropsFromServer(Connection.java:3294)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1943)
at com.mysql.jdbc.Connection.<init>(Connection.java:716)
at com.mysql.jdbc.Connection.getInstance(Connection.java:291)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(libgcj.so.8rh)
at java.sql.DriverManager.getConnection(libgcj.so.8rh)
at program.main(program.java) <- this is line of DriverManager.getConnection( url, "user", "password" );


** END NESTED EXCEPTION **

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
fhleung
Member
 
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
paulsm4
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 java.sun.com (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:

http://forums.mysql.com/read.php?39,...410#msg-150410

'Hope that helps .. PSM
 
  


Reply


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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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