Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unable to connect to MySQL (running under Ubuntu) 1

Status
Not open for further replies.

towerbase

Programmer
Jul 31, 2002
1,053
0
0
GB
I am new to Linux and Java but have a few years MySQL experience; my ambition is migrate from Windows and Delphi to Linux and Java.

However, I seem unable to get Connector/J working with Java and Linux. (I have got it to work with Windows and Delphi).

I have saved mysql-connector-java-5.1.7-bin-jar in a directory and added a "export set ..." statement to etc/profile to set CLASSPATH.

My java program checks the existence of the -bin-jar file with the following code:
Code:
boolean exists = (new File(jarname)).exists();
if (exists)
  System.out.println("jar found");
else
  System.out.println("No jar file found");
I also check that the CLASSPATH environment variable has been set up with
Code:
String classp = System.getenv("CLASSPATH");
if (classp.equals(jarname))
  System.out.println("Jar names match");
else
  System.out.println("Jar names do NOT match");
System.out.println("CLASSPATH="+classp);
and this correctly displays the environment variable that I set up.

Now the bit that doesn't work:
Code:
private static final String dbClassName = "com.mysql.jdbc.Driver";
...
try
{
  Class.forName(dbClassName);
  System.out.println("Class.forName worked");
  ...
}
catch (Exception e)
{
  System.out.println("Unable to connect to database server");
}
I always get the "Unable to connect..." message. No doubt I'm doing something really silly. I've learnt quite a bit to get this far but can anyone push me in the right direction, please?

I am using NetBeans 6.0.1, Java 1.6.0_0, Ubuntu 8.04 in case that is relevant.

Andrew
Hampshire, UK
 
1) I use eclipse but I guess you have to add the JConnect jar
library into your project in NetBeans to run your Java program and connect to mysql.
2) For pure linux and running your Java programm in linux console, you have to configure the classpath in Linux. You may
use a bash file to compile your program and another bash file to run the compiled Java class of your program.

 
Thank you and a star for that helpful post. I had not added the jar file to the Netbeans library path window.

You've probably guessed that I am new to Netbeans too.

My program does NOT display "Class.forName worked".

I have now added the jar file to the netbeans library path. It still does not display "Class.forName worked" but I shall spend some more time on this problem before coming back here.

Andrew
Hampshire, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top