SQLException: No suitable driver found for jdbc:mysql://localhost:3306/xxxx

剛剛學習jsp,對着視頻敲代碼,報這個錯誤。

SQLException: No suitable driver found for jdbc:mysql://localhost:3306/shop

SQLState: 08001

VendorError: 0

問題,因爲沒有引入Driver類。

final String DBDRIVER = "org.gjt.mm.mysql.Driver";
String username = "root";
String password = "root";
String url = "jdbc:mysql://localhost:3306/shop";
Connection con = null;
try {
    Class.forName(DBDRIVER);
    con = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
    System.out.println("SQLException: " + e.getMessage());
    System.out.println("SQLState: " + e.getSQLState());
    System.out.println("VendorError: " + e.getErrorCode());
} catch (ClassNotFoundException e) {
    System.out.println("Class:" + e.getMessage());
}

加上Class.forName("xxxx")就OK了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章