开发者

Problem to connect mysql and Java

开发者 https://www.devze.com 2023-01-30 05:13 出处:网络
I am trying to connect Java code to mySQL. Here is the error that I got. I dont understand why no driver is found since I have put the connector jar at the classpath.

I am trying to connect Java code to mySQL. Here is the error that I got. I dont understand why no driver is found since I have put the connector jar at the classpath.

Class Not Found Exception:
No suitable driver found for jdbc:mysql://localhost/hpdata?user=root&password=12
3456
Exception in thread "main" java.lang.NullPointerException
    at edu.indiana.iucbrf.feature.featurespec.FeatureSpecRDB.open(FeatureSpe
cRDB.java:122)
    at edu.indiana.iucbrf.feature.featurespec.FeatureSpecRDB.<init>(FeatureS
pecRDB.java:66)
    at edu.indiana.iucbrf.domain.componentfactory.RDBComponentFactory.constr
uctProble开发者_StackOverflowmFeatureSpecCollection(RDBComponentFactory.java:112)
    at edu.indiana.iucbrf.domain.Domain.<init>(Domain.java:239)
    at edu.indiana.iucbrf.domain.Domain.<init>(Domain.java:197)
    at edu.indiana.iucbrf.examples.honeypotRDBTemplate.HDomainRDB.<in
it>(HDomainRDB.java:56)
    at edu.indiana.iucbrf.examples.hRDBTemplate.HSystemRDB.set
 upDomain(HSystemRDB.java:198)
    at edu.indiana.iucbrf.examples.hRDBTemplate.HSystemRDB.<in
it>(HSystemRDB.java:131)
    at edu.indiana.iucbrf.examples.hRDBTemplate.HTestClassRDB.
main(HTestClassRDB.java:65)

Here is my code :

   private static void flush() {

  Class.forName("com.mysql.jdbc.Driver").newInstance();

    try {

   conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hpdata?user=root&password=123456");  

      } catch (Exception e) {
          System.out.println("Class Not Found Exception:");
          System.out.println(e.getMessage());       
      }


try {
 String driverName = "com.mysql.jdbc.Driver";
 Class.forName(driverName);

 String serverName = "localhost";
 String mydatabase = "hpdata";
 String url = "jdbc:mysql :// " + serverName + "/" + mydatabase; 

 String username = "root";
 String password = "123456";
 connection = DriverManager.getConnection(url, username, password);
} catch(Exception e) {
 // appropriate action
}


Try loading the driver before using it by incorporating this line.

Class.forName("com.mysql.jdbc.Driver")


Make sure mysql-connector-java-<version-number>-bin.jar is in your classpath. If you don't have it, you can download it here.


You're assuming that you've set CLASSPATH properly. Please explain how you're doing that so we can tell you whether it's correct or not.

If you've set an environment variable named CLASSPATH, that's almost certainly wrong.

It makes a difference if your app is for the web or desktop. Please tell us which is true for you.

UPDATE:

The right way to set CLASSPATH for a desktop app is to use the -classpath option on the JVM when you run:

java -classpath .;<paths-to-your-JARs-separated-by-semi-colons>;<paths-to-the-root-of-package-trees> foo.YourCode


After setting CLASSPATH please copy mysql-connector-java-5.1.16-bin into these folders:

C:\Program Files\Java\jdk1.6.0_18\jre\lib\ext

and

C:\Program Files\Java\jre6\lib\ext
0

精彩评论

暂无评论...
验证码 换一张
取 消