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 05-09-2003, 02:59 PM   #1
gballsch
LQ Newbie
 
Registered: Mar 2003
Location: Northern Virginia
Distribution: Red Hat 8
Posts: 18

Rep: Reputation: 0
mysql and jdbc


Hi - am having troulble connecting to mysql (on linux) from java. I am running a simple jdbc program to test the connection and get the error - "Error : Driver not found for URL: jdbc:mysql://localhost/test" . I downloaded mysql-connector-java-3.0.7-stable-bin.jar from mysql.com, installed in $J2EE_HOME/lib, $JAVA_HOME/lib and added to my CLASSPATH. What am I missing ?
ALso, I have multiple java packages installed in /usr/java -

j2sdk1.4.1_02 j2sdkee1.3.1 jdk1.3.1_07 jre1.3.1_07 jswdk-1.0.1

Is there a possible confilct wtih this? I think that I only need J2EE and runtime to do everything I need (applets, servlets, swing, jdbc).

import java.sql.*;

public class Connect
{
public static void main (String[] args)
{
Connection conn = null;

try
{
String userName = " ";
String password = " ";
String url = "jdbc:mysql://localhost/test";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
//Class.forName ("org.gjt.mm.mysql.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
}
catch (Exception e)
{
System.err.println ("Cannot connect to database server");
System.err.println ("Error : " + e.getMessage());
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
}
 
Old 05-09-2003, 05:07 PM   #2
Hangdog42
LQ Veteran
 
Registered: Feb 2003
Location: Maryland
Distribution: Slackware
Posts: 7,803
Blog Entries: 1

Rep: Reputation: 420Reputation: 420Reputation: 420Reputation: 420Reputation: 420
I'm using code essentially identical to yours and I can connect. The only thing I can think is that it isn't finding the mysql-connector jar file. I've got mine at $JAVA_HOME/jre/lib/ext. The other thing to try is explicitly reference the jar file at run time with the classpath flag.

As for the multiple java installs, it should be fine as long as each is in its own directory under /usr/java. However, with that many installations, I'd be REAL tempted to set everything with a -cp flag at run time.
 
Old 05-10-2003, 03:32 PM   #3
gballsch
LQ Newbie
 
Registered: Mar 2003
Location: Northern Virginia
Distribution: Red Hat 8
Posts: 18

Original Poster
Rep: Reputation: 0
HangDog thanks - do you see anything amiss. Also tried w/ java -cp $CLASSPATH Connect.

[gballsch@barney ext]$ cd $JAVA_HOME/jre/lib/ext
[gballsch@barney ext]$ pwd
/usr/java/j2sdk1.4.1_02/jre/lib/ext
[gballsch@barney ext]$ ls
dnsns.jar localedata.jar
jdbc2_0-stdext.jar mysql-connector-java-2.0.14-bin.jar
jta-spec1_0_1.jar mysql-connector-java-3.0.7-stable-bin.jar
ldapsec.jar sunjce_provider.jar
[gballsch@barney ext]$ echo $J2EE_HOME
/usr/java/j2sdkee1.3.1
[gballsch@barney ext]$ echo $CLASSPATH
/usr/java/j2sdkee1.3.1/lib/j2ee.jar:/usr/java/j2sdk1.4.1_02/jre/lib/ext/mysql-connector-java-3.0.7-stable-bin.jar:/usr/java/j2sdkee1.3.1/lib/mysql-connector-java-3.0.7-stable-bin.jar
 
Old 05-10-2003, 06:01 PM   #4
Hangdog42
LQ Veteran
 
Registered: Feb 2003
Location: Maryland
Distribution: Slackware
Posts: 7,803
Blog Entries: 1

Rep: Reputation: 420Reputation: 420Reputation: 420Reputation: 420Reputation: 420
Hmm. I've only got J2SDK 1.4.1_02 on my box so everything points there. I wonder if your $CLASSPATH is causing trouble since it is pointing to two jar files in two different Java versions. The other potential problem is that you have two different versions of the mysql-connector jar in your 1.4.1 install. That might be confusing the compiler (although I would have expected it to complain). Is there a reason why you have both installs on your CLASSPATH? I know that multiple versions of Java can co-exist on a single machine, but when coding you might want to have only one on the CLASSPATH. However, I'm a little out of my depth here, so maybe one of the Java gurus here could comment.

When you tried java -cp, did you explicity point to the jar (/usr/java/j2sdk1.4.1_02/jre/lib/ext/mysql-connector-java-3.0.7-stable-bin.jar) or did you use $CLASSPATH? Just for giggles, it might be worth compiling and running using explicit jar files

Finally, here is some code that absolutely works on my computer. It looks an awful lot like yours:

Quote:
import java.sql.*;

public class MySqlUsers {

public static void main(String[] args) {
String url ="jdbc:mysql://localhost/mysql";
String sqlQuery="SELECT Host,User,Password FROM user";
Connection con;
try{
//Class.forName("com.mysql.jdbc.Driver").newInstance();
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection(url,"hangdog","pswd");
Statement smnt=con.createStatement();
ResultSet result =smnt.executeQuery(sqlQuery);
while(result.next()){
System.out.println(result.getRow());
System.out.println(result.getString("Host")+"\t\t"+result.getString("User")+"\t"+result.getString("" +
"Password"));
}
smnt.close();
con.close();
}
catch(Exception e){
System.err.println("Exception: "+e.getMessage());
}
}
}
 
Old 05-12-2003, 01:07 PM   #5
gballsch
LQ Newbie
 
Registered: Mar 2003
Location: Northern Virginia
Distribution: Red Hat 8
Posts: 18

Original Poster
Rep: Reputation: 0
Again thanks. The only reason that I have both in the CLASSPATH variable was for a test. I just tried explicitly defining both versions of mysql connect (from /tmp) (java -cp /tmp/mysql-connector-java-3.0.7-stable/mysql-connector-java-3.0.7-stable-bin.jar Connect, and java -cp /tmp/mysql-connector-java-2.0.14/mysql-connector-java-2.0.14-bin.jar Connect) with a run and get same error. BTW, I get the same error w/ your code. I'll keep looking ....................
 
Old 05-14-2003, 04:31 AM   #6
juby
Member
 
Registered: Dec 2002
Location: Bangalore, India
Distribution: Ubuntu 5.*
Posts: 241

Rep: Reputation: 30
I guess the prob is with ur connection string , have u gave permissions to any user using the grant command.. ?
theres a simple example in the unzipped dir of the jdbc driver ..read that..& try it will work
 
Old 05-14-2003, 01:30 PM   #7
gballsch
LQ Newbie
 
Registered: Mar 2003
Location: Northern Virginia
Distribution: Red Hat 8
Posts: 18

Original Poster
Rep: Reputation: 0
Talking

Folks - thank you again for your help - I found my difficulty. The reason that java did not find the correct jar was due to the different environments that I have installed on my machine. Java was originally installed as part of the RH 8 distribution (gcj) and when I updated/added for tomcat I also installed more later versions of java. This is where all my java env. variables pointed to. But when I comiled it, by default, used the original java environment from RH, which I guess knew nothing about the new tomcat installs. By compiling explicitly with the $JAVA_HOME all worked as expected. Does anyone know how/if I should uninstall the original gcj javac so that by default is uses my later installs?
Thanks again - Gregg
 
Old 05-14-2003, 04:04 PM   #8
Hangdog42
LQ Veteran
 
Registered: Feb 2003
Location: Maryland
Distribution: Slackware
Posts: 7,803
Blog Entries: 1

Rep: Reputation: 420Reputation: 420Reputation: 420Reputation: 420Reputation: 420
Cool. Thanks for posting your solution!

You probably don't have to delete the gcj version but you do probably need to go through your $PATH and remove that directory. You can always use the which command to figure out which javac the system is using (which javac).
 
  


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
JDBC and MySQL Driver White R4bbit Debian 2 08-13-2004 04:55 PM
JDBC mysql url parameter for tomcat's server.xml gschrade Linux - Software 1 03-01-2004 05:07 PM
JDBC mysql url parameter for tomcat's server.xml gschrade Linux - Software 4 02-29-2004 12:23 PM
jdbc mysql Redhat 7.2 problem johnlee Linux - General 1 04-23-2002 06:34 PM
MySQL and JDBC oulevon Programming 1 07-16-2001 06:18 PM

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

All times are GMT -5. The time now is 02:35 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
Open Source Consulting | Domain Registration