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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connecting to database 1

Status
Not open for further replies.

JTan

Technical User
Oct 9, 2001
73
SG
Hi all,

I have tried to use the following codes to connect to my mySQL database and the results given is always "Cannot connect to server". May I know what is wrong? Or is there anything which I shld add before I compile n run the codes below?

I can confirm the password and username are correct.

TIA....


import java.sql.*;

public class Connect
{
public static void main (String []args)
{
Connection conn = null;
String url = "jdbc:mysql://localhost/vpg";
String userName = "root";
String password = "root";

try
{
Class.forName("com.mysql,jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, userName, password);
System.out.println("Connected");
}
catch (Exception e)
{
System.err.println ("Cannot connect to server.");
}
finally
{
if (conn!=null)
{
try {
conn.close();
System.out.println ("Disconnected.");
}
catch (Exception e) {}
}
}
}
}
 
Try changing your url to this:

String url = "jdbc:mysql://localhost:3306/vpg";

I have never tried connecting without specifying the port before. If your mysql server is not running on 3306, then you will have to change this to the correct port.

Paul
 
Also, if you copy/pasted the code in, then I've spotted a typo in your driver loading line:

Code:
Class.forName("com.mysql,jdbc.Driver").newInstance();

The comma should be a full stop.

(If this is the case it's always a good idea to put in a
Code:
e.printStackTrace();
in your catch block when trying these things out. Your current exception handling doesn't give you any info on what went wrong.)

Tim
 
Hi all,

thanks for your replies. I did some amendments to my codes and I think the problem lies in my driver.

I have some queries in regards to the installation of mySQL connector/J....I tried to set my classpath as stated in mySQL official site....

The official codes are:

C:\> set CLASSPATH=\path\to\mysql-connector-java-[version]-bin.jar;%CLASSPATH%

Just to ensure that the correct thing has been done, I tried to change the version to the version that I have now, as below:

C:\> set CLASSPATH=\path\to\mysql-connector-java-[3-0-16-ga]-bin.jar;%CLASSPATH%

But I kept getting errors when I tried to run the program now...

Exception in thread "main" java.lang.NoClassDefFoundError: Connect

Could someone kindly enlighten me what should be the correct commands that I should enter to install the connector, or at least how to set the classptah correctly?

TIA!
 
I tend not to bother with the CLASSPATH environment variable. I prefer to specify an explicit classpath when I invoke the java command using the -cp <Classpath> form. This prevents any problems with the classpath needed for one app clashing with that for another.

That said, I would think that you definitely have a problem with your classpath. Maybe you are running the thing using the -cp with some other classpath? This would force the JVM to ignore the environment CLASSPATH.

Can you post the exact classpath. Might be able to spot something.

(There's a lot of 'classpath's in this reply [bigsmile])


Tim
 
The error : Exception in thread "main" java.lang.NoClassDefFoundError: Connect

- the poster's class is named "Connect" so you should add the directory where your Connect.class file is to your CLASSPATH.

Click here to learn Ways to help with Tsunami Relief
--------------------------------------------------
Free Database Connection Pooling Software
 
Also, my MySQL jdbc connector jar doesn't even have a Connect class (it does have a Connection class). So it may not even be a problem with the MySQL connector on the classpath.


Tim
 
..ah, I should have had a quick look again at the top post before I replied.. The main class is called Connect.. doh! Think I need another coffee.


Tim
 
Okie, the following is what I have done:

1) change the classpath back to where the COnnect file lies.
2) run the program using the -cp command (don't know if I state the command correctly...but it manages to get past compile error).

Results:

java.sql.SQLException: No suitable driver

Just a question, does it matter where I unzip the connector archive?

 
Sorry if I'm stating the obvious here; you do need both the jdbc connector jar AND your Connect class on the classpath. It doesn't matter where the mysql-connector-java-version-stable-bin.jar is as long as its location is in the classpath.


Tim
 
the jar file is placed in the stated classpath now.

However, when I tried to execute the following codes, I got the ClassNotFoundException error:com.mysql.jdbc.Driver

import java.sql.*;



public class Connect
{
public static void main (String []args)
{
Connection conn = null;
//String url = "jdbc:mysql://localhost:3306/vpg";
//String userName = "root";
//String password = "root";

try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/vpg?user=root&password=root");
//conn = DriverManager.getConnection(url, userName, password);
System.out.println("Connected");
}
catch (Exception e)
{
e.printStackTrace();
System.err.println ("Cannot connect to server.");
}
finally
{
if (conn!=null)
{
try {
conn.close();
System.out.println ("Disconnected.");
}
catch (Exception e) {}
}
}
}
}

 
The code is fine, it runs okay for me. Can you please post your exact classpath as you have it, warts-and-all?
 
Hi all,

thanks for your help.
I manage to resolve the problem. :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top