Results 1 to 10 of 10
- 01-21-2010, 04:41 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
Connectivity issue-MySQL and Java
Hi
There is a problem in JDBC connectivity.I have downloaded the JDBC
Connector/J for MySQL connectivity with java. I've changed the
CLASSPATH environment variable, made changes in the code and tried
and tested with several modifications. But it still doesn't work.
Here is the source code.
import java.sql.*;
public class Check
{
public static void main(String args[])
{
Connection conn=null;
try
{
String url="jdbc:mysql://localhost/example";
Class.forName("com.jdbc.mysql.Driver").newInstance ();
conn=DriverManager.getConnection(url);
System.out.println("Database connection established");
}
catch(Exception e)
{
System.out.println("Cannot connect to server");
}
finally
{
if(conn!=null)
{
try
{
conn.close();
System.out.println("Database connection terminated");
}
catch(Exception e)
{ }
}
}
}
}
It compiles and displays "Cannot connect to database" when run on the
command prompt.
Here are some specifications:
(1) My JDK is in C:\ drive
(2) MySQL is in Program Files.
(3) The Connector/J is in C:\jdk1.6.0_16\jre\lib\ext
The ext folder has the mysql-connector-java-5.1.10-bin.jar file.
Hence the CLASSPATH environment variable includes :
C:\jdk1.6.0_16\jre\lib\ext\mysql-connector-java-5.1.10-bin.jar
Please help me with this so that I can move on...
- 01-21-2010, 05:47 PM #2
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
Add this line to your code
and post all Exception here please:
catch(Exception e)
{
e.printStackTrace();
System.out.println("Cannot connect to server");
}
Maybe you must specify port like 3306
jdbc:mysql://localhost:3306/mydb
- 01-21-2010, 07:16 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
Thanks
In which Exception block do I make the changes?
Making the changes you suggested within the paranthesis of the first exception and adding :3306 after local host didn't help. It still gives the same exception.
- 01-21-2010, 08:03 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
In all of them.
- 01-22-2010, 08:40 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Yes. Always print a stack trace, otherwise you're simply losing all the important exception information.
- 01-22-2010, 09:21 AM #6
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
Here's what comes on printing the stack trace :
java.lang.ClassNotFoundException : com.jdbc.mysql.Driver
at java.net.URLCClassLoader$1.run(URLClassLoader.java :200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net,URLClassLoader.findClass(URLClassLoader.j ava:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 52)
at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:320)
at java.lang.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at Check.main(Check.java:12)
Cannot connect to database
How do I verify whether SQL is running or not?
- 01-22-2010, 09:31 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
The connector/J jar file isn't on your classpath.
- 01-22-2010, 09:36 AM #8
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
What should the classpath be so that Connector/J is included?
- 01-22-2010, 10:12 AM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 01-27-2010, 05:28 PM #10
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
MySQL connectivity (hours wasted)
By ribbs2521 in forum New To JavaReplies: 13Last Post: 10-21-2009, 07:18 AM -
mysql query performance issue
By gilbertsavier in forum JDBCReplies: 0Last Post: 08-05-2009, 10:34 AM -
mysql connectionpropertiestransform issue - help needed
By samatha in forum Advanced JavaReplies: 0Last Post: 10-22-2008, 08:50 PM -
MySQL + Java - auto incrementation issue
By int80 in forum Advanced JavaReplies: 6Last Post: 07-29-2008, 06:04 PM -
MySQL issue: Exception while getting MetaDataInfo
By sandeepspatil in forum JDBCReplies: 2Last Post: 07-27-2007, 06:54 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks