SQLException: No suitable driver found for jdbc:mysql

[亡魂溺海] 提交于 2019-12-01 06:31:24

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&amp;characterEncoding=UTF-8&amp;useSSL=false&amp;autoReconnect=true&amp;failOverReadOnly=false&amp;serverTimezone=GMT
其实只需要serverTimezone=GMT就行了
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!