Results 1 to 6 of 6
Thread: connection my mysql
- 04-06-2010, 08:13 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 13
- Rep Power
- 0
connection my mysql
i am try to connect to mysql in myeclipse by using mysql-connector-java-5.1.12.tar
this is the code:
import java.sql.*;
public class Connect
{
public static void main (String[] args)
{
Connection conn = null;
try
{
String userName = "root";
String password = "";
String url = "jdbc:mysql://localhost/java";
Class.forName ("com.mysql.jdbc.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");
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
}
But each time i find error "Cannot connect to database server"
- 04-06-2010, 08:16 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Why not print the stack trace of the exception getting caught to see what is really going wrong?
- 04-06-2010, 08:21 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 13
- Rep Power
- 0
ok i am trying...
- 04-06-2010, 08:23 PM #4
Member
- Join Date
- Apr 2010
- Posts
- 13
- Rep Power
- 0
i am really new to this...if you please tell the code of it ....your help will be highly appreciated
- 04-06-2010, 08:26 PM #5
Member
- Join Date
- Apr 2010
- Posts
- 13
- Rep Power
- 0
using this code:
import java.sql.*;
public class Connect
{
public static void main (String[] args)
{
Connection conn = null;
try
{
String userName = "root";
String password = "";
String url = "jdbc:mysql://localhost/java";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
}
got this error:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Connect.main(Connect.java:15)
- 04-06-2010, 08:26 PM #6
Member
- Join Date
- Apr 2010
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
TWO IPs FOR MY MYSQL CONNECTION
By tugalsan in forum JDBCReplies: 2Last Post: 03-21-2010, 08:46 PM -
Java-mysql connection
By Kligham in forum New To JavaReplies: 16Last Post: 11-25-2009, 01:28 PM -
java mysql connection
By sysout in forum New To JavaReplies: 5Last Post: 10-31-2009, 10:48 AM -
[SOLVED] applet connection to mysql db on home server
By shwein in forum Java AppletsReplies: 1Last Post: 10-29-2008, 05:38 PM -
no mysql connection
By scchia in forum New To JavaReplies: 12Last Post: 07-19-2008, 07:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks