aspose file tools*
The moose likes IDEs, Version Control and other tools and the fly likes eclipse can not connect to mysql!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "eclipse can not connect to mysql!!!" Watch "eclipse can not connect to mysql!!!" New topic
Author

eclipse can not connect to mysql!!!

Jeffrey Ye
Greenhorn

Joined: Feb 18, 2006
Posts: 24
i'v installed the jdbc driver to the following:
D:\mysql-connector-java-3.0.17-ga

and i'v set the classpath in property in computer

i'v alse started the mysql server;


the code :
import java.sql.*;



public class Dbconnection {
Connection connector;
public Dbconnect(){
try{
Class.forName("org.gjt.mm.mysql.Driver");

}catch(ClassNotFoundException e){
System.out.println(e.getMessage());

}
try{
connector=DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
}catch(SQLException c){
System.out.println(c.getMessage());
}
}
public static void main(String[]args){
Dbconnection con=new Dbconnection();
}
}
Michael Duffy
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Originally posted by Jeffrey Ye:
i'v installed the jdbc driver to the following:
D:\mysql-connector-java-3.0.17-ga

and i'v set the classpath in property in computer


This is incorrect. Eclipse (and every other IDE) and app servers like Tomcat ignore your system CLASSPATH.

Eclipse requires that you add the MySQL connector JAR to your project CLASSPATH in the way it needs to see it. Right click on the project, select "Properties", and find the spot where you add 3rd party JARs.


i'v alse started the mysql server;


the code :
import java.sql.*;



public class Dbconnection {
Connection connector;
public Dbconnect(){
try{
Class.forName("org.gjt.mm.mysql.Driver");

}catch(ClassNotFoundException e){
System.out.println(e.getMessage());

}
try{
connector=DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
}catch(SQLException c){
System.out.println(c.getMessage());
}
}
public static void main(String[]args){
Dbconnection con=new Dbconnection();
}
}


Throw this class away. It's not useful at all. You've hard-wired the driver name and connection URL. You'll have to rebuild the code if you change either. You don't close connections anywhere. It's far better to keep the scope of connections as narrow as possible and close them when you're done. Go do some reading about connection pools, too. You will not want to deal with connections in the way your class is doing it except in the simplest, most throw-away, quick and dirty situations.


%
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31062
    
232

Jeffrey,
Welcome to JavaRanch!

Eclipse doesn't use the system classpath. You need to add the mySql jar to your java project's build path.

Also, you'll want to merge those two try/catch blocks. If the class isn't found, there's no point to getting a connection. And it makes for some confusing error messages!

Note that this is really an Eclipse question. Normally, I'd move it our IDEs forum, but I'm leaving it here in case anyone wants to discuss the JDBC aspects further.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jeffrey Ye
Greenhorn

Joined: Feb 18, 2006
Posts: 24
i'v added the path in eclipse ,and the problem message "No Suitable Driver"
did't show;

but another problem message as follow:


#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
# EXCEPTION_FLT_STACK_CHECK (0xc0000092) at pc=0x00dd0618, pid=2132, tid=2524
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0_06-b05 mixed mode, sharing)
# Problematic frame:
# v ~RuntimeStub::init_check_patching Runtime1 stub
#
# An error report file with more information is saved as hs_err_pid2132.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
#
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31062
    
232

Now this is definitely an Eclipse question. Moving to our IDEs forum.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

Actually, it's a bug in the JVM.

However, regarding CLASSPATHs, Eclipse supports all sorts of classpaths, each of which may be independent of the other. Most of them are in custom build (e.g. Ant) and test/run profiles, but the model for them comes from your project's classpath.

On the Java Perspective:

Right-click on the project, select Properties and in the Project Properties dialog, select "Java Build Path" and the "Libraries" tab. Click on the "Add External JAR" button and aim it at your mysql driver jar.

If you do that, when you select Run or Debug, the model project will include the database driver when it builds a default run profile. Or you can skip all that and simply create a new run/debug profile and do the equivalent of the above directly to the new profile.

You would only actually need to include the mysql driver jar in the actual project itself if you were casting to MySQL-specific objects and the live code compiler needed those class definitions.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: eclipse can not connect to mysql!!!