SQLException: No suitable driver found for jdbc:mysql
阿新 • • 發佈:2019-08-04
SQLException: No suitable driver found for jdbc:mysql
1. Java併發:徹底理解ThreadLocal)中的連線Mysql示例
1.1 獲取mysql連線
public class ConnectionManager { private static ThreadLocal<Connection> connectionHolder = ThreadLocal.withInitial(() -> { Connection conn = null; try { conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test", "root", "123"); } catch (SQLException e) { e.printStackTrace(); } return conn; }); public static Connection getConnection() { return connectionHolder.get(); } }
1.2 測試
public static void main(String[] args) throws SQLException { Connection conn = ConnectionManager.getConnection(); Statement stmt = conn.createStatement(); java.sql.ResultSet rs = stmt.executeQuery("select * from user"); while(rs.next()){ System.out.println(rs.getObject(1)); } }
1.3. 異常
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
1.4 解決
1.4.1 檢查連線URL格式是否正確
1.4.2 檢查驅動字串
1.4.3 檢查Classpath中是否加入mysql_jdbc驅動
1.4.4 將mysql-connector-java-XXX.jar放入%JAVA-HOME%下的jre\lib\ext下解決
2. The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized
url後面增加:?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT
其實只需要serverTimezone=G