• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

eclipse can not connect to mysql!!!

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now this is definitely an Eclipse question. Moving to our IDEs forum.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic