2011-03-22 127 views
0
try 
     { 
      String userName = "root"; 
      String password = ""; 
      //<facility> is my database 
      String url = "jdbc:mysql://localhost/facility"; 
      Class.forName ("com.mysql.jdbc.Driver").newInstance(); 
      conn = DriverManager.getConnection (url, userName, password); 
      System.out.println ("Database connection established"); 
     } 
     catch (Exception e) 
     { 
      System.out.println ("Cannot connect to database server " +e.getClass().getName()); 

     } 

输出>无法连接到数据库服务器抛出java.lang.ClassNotFoundException找不到JDBC驱动程序

+1

你能确保你的jdbc驱动jar文件在classpath中吗?通常它的mysql-connector-java。 .jar – Nishan 2011-03-22 11:41:52

+1

这意味着您没有将JDBC jar放入您的类路径中。你可以从这里得到它:http://dev.mysql.com/downloads/connector/j/5.0.html – Alexandre 2011-03-22 11:44:01

+0

而不是只打印出例外的名称打印出消息或整个堆栈跟踪。 – jzd 2011-03-22 11:50:33

回答

2

看来,你应该把你的MySQL驱动jar到您的类路径

+0

我已经包括建立路径连接器/ J,我在这里找到:http://dev.mysql.com/downloads/connector/j/5.0.html – 2011-03-22 11:45:25

1

下载jdbc-mysql驱动程序here

将它包含在你的类路径中。

2

要使用MYSQL驱动程序(连接器/ J),您需要从MySQL Connectors下载并将其放入您的类路径中。

相关问题