0
我想連接到本地主機上的sql數據庫。 用下面的代碼:連接到sql數據庫問題
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(ClassNotFoundException e) {
System.out.println("JdbcOdbcDriver wasn't found");
e.printStackTrace();
}
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost/testBase", "root", "");
Statement statement = connection.createStatement();
ResultSet res = statement.executeQuery("");
}
catch(SQLException e){ System.out.println(e); }
finally {
if(connection != null) {
try { connection.close(); } catch (SQLException e) { e.printStackTrace(); }
}
}
我有2個與此相關的代碼問題:
- 我使用的是在sun.jdbc.odbc.JdbcOdbcDriver驅動程序,因爲它似乎,這是唯一可用的上我的系統,但是我已經讀過它是一個Windows組件...有沒有一種方法使其與平臺無關(或已經)?
第二收集輸出
值java.sql.SQLException:發現 JDBC沒有合適的驅動程序的:mysql://本地主機/
這是很煩人的,我不似乎能夠找到一種工作方式...
感謝您的幫助!