开发者

Couldn't make mysql connector j work

开发者 https://www.devze.com 2023-02-22 17:23 出处:网络
I\'ve set the classpath envoirenment but still get an error \"Exception:com.mysql.jdbc.Driver\" Do you have any idea what might be wrong?

I've set the classpath envoirenment but still get an error "Exception:com.mysql.jdbc.Driver"

Do you have any idea what might be wrong?

Here is m开发者_开发技巧y test code:

import java.sql.*;

public class JdbcExample1 {

public static void main(String args[]) {
  Connection con = null;

  try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    con = DriverManager.getConnection("jdbc:mysql:///test", "root", "secret");

    if(!con.isClosed())
      System.out.println("Successfully connected to MySQL server...");

  } catch(Exception e) {
    System.err.println("Exception: " + e.getMessage());
  } finally {
    try {
      if(con != null)
        con.close();
      } catch(SQLException e) {}
    }
  }
}


Exception:com.mysql.jdbc.Driver

Is most probably not the full error message. I guess it's a ClassNotFoundException and you simply do not have the MySQL JDBC driver as part of your classpath.

When running your program, you need to list the driver as well

java -cp .;mysql-connector-java-5.1.7-bin.jar JdbcExample1
(This assumes JdbcExample1.class and the .jar file are in the current directory)

I've set the classpath envoirenment

Setting the CLASSPATH environment variable is not necessary anymore (actually it never has been necessary). As a matter of fact it creates more problems than it solves.

Use the above syntax to supply the path to your driver and run your program


As horse says, I'm pretty sure it's a 'ClassNotFoundException'. To be sure add "e.printStackTrace();" in your catch-block.

Always best to get a stack trace.

0

精彩评论

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