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=GMT就行了
来源:oschina
链接:https://my.oschina.net/u/248712/blog/3079127