我想從使用JDBC的表中獲取所有列值。在Java中連接到數據庫的異常:JDBC
這是我在我的主程序中編寫的代碼。
Class.forName("com.mysql.jdbc.driver");
String url = "jdbc:jtds:sqlserver://test.com/abc_1_20121225;instance=abcd";
Connection conn = DriverManager.getConnection(url,"uname","pwd");
Statement stmt = conn.createStatement();
ResultSet rs;
rs = stmt.executeQuery("SELECT table_name, column_name, data_type, data_length FROM USER_TAB_COLUMNS WHERE table_name = 'MyTable'");
while (rs.next()) {
String colName = rs.getString("column_name");
System.out.println(colName);
}
conn.close();
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
我在我的課程路徑中添加了jtds.jar。我收到此消息
Got an exception!
com.mysql.jdbc.driver
有人能告訴我問題的確切原因嗎?
編輯:
添加stactrace;
java.lang.ClassNotFoundException: com.mysql.jdbc.driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at schwab.TestDB.main(TestDB.java:15)
而不是e.getMessage(),你可以嘗試與e.printStackTrace()並提供給我們stacktrace? –
具有Driver類而非驅動程序類的MySQL jar。試試看Class.forName(「com.mysql.jdbc.Driver」);請給堆棧跟蹤進一步調查。 –
另外什麼是確切的例外 - 因爲這給了更多的信息,只是有一個例外 – Mark