Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

eclipse can not connect to mysql!!!

 
Jeffrey Ye
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeffrey Ye
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now this is definitely an Eclipse question. Moving to our IDEs forum.
 
Tim Holloway
Saloon Keeper
Posts: 18304
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic