【发布时间】:2014-01-21 02:51:52
【问题描述】:
我正在尝试使用 mysql j 连接器在 netbeans 中连接一个 java 程序,我收到一条异常消息,提示 com.mysql.jdbc.Driver 这是我的代码
package testdb;
import java.sql.*;
public class Testdb {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/location";
String login = "root";
String pass = "";
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url,login,pass);
Statement s = con.createStatement();
ResultSet r = s.executeQuery("select * from reservation");
while(r.next())
{
System.out.println("id reservation = "+r.getInt("id_reservation"));
}
}catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
【问题讨论】:
-
你得到什么异常???
-
打印堆栈跟踪 e.printStackTrace() 而不是消息。你会得到一个更好的主意。检查是否已将所需的 jar 文件添加到构建路径
-
不要使用 e.getMessage()。使用 e.printStackTrace() 查看异常的有用描述。
-
将此行
System.out.println(e.getMessage());更改为e.printStackTrace()并在此处发布完整的堆栈跟踪 -
删除你的 catch 块,并在你的 main 方法中添加一个 throws XxxException (对于每个检查的异常)。然后粘贴你得到的堆栈跟踪。