数据库连接问题集锦
java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
检查账号密码是否正确
版本是否匹配
5.0之前是 Class.forName(“com.mysql.jdbc.Driver”)
5.0之后是 Class.forName(“com.mysql.cj.jdbc.Driver”)
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mysql
at java.sql.DriverManager.getConnection
复制mysql驱动jar包到我的web工程WebRoot/WEB-INF/lib下
复制mysql驱动jar包到Tomcat的lib路径下
添加mysql驱动jar包到环境变量ClassPath中
Key Retrieval is not allowed
加上:&allowPublicKeyRetrieval=true
(一般是mysql8会遇到的问题)
com.mysql.jdbc.exceptions.
MySQLNonTransientConnectionException: Client does Client does not support authentication protocol requested by server; consider upgrading MySQL client
更新数据库连接的jar包
各版本数据包下载链接
http://central.maven.org/maven2/mysql/mysql-connector-java/
java.sql.SQLException: Access denied for user ‘root’@‘DESKTOP-2RCQHHG’ (using password: YES)
是权限不够的原因
mysql8.0之后的语法也有相应改变,以下是正确的语法与步骤
以下步骤在mysql命令窗口执行
1.mysql8.0不再隐式创建用户,所以需要先创建用户
create user 'test'@'19``2.168.%' identified by 'test1111' PASSWORD EXPIRE NEVER;
2.但是会报用户权限不够这样的错误,应该是已经有了用户名的前提下所以一般可以跳过第一步;
grant all on 数据库名.* to 用户名@'% ' ;
如果报错
You are not allowed to create a user with GRANT
3.则修改host:
update user set host='%' where user='用户名';
grant all on 数据库名.* to 用户名@'% ' ;
若还是不行,可多执行两遍
grant all on 数据库名.* to 用户名@'% ' ;
若还是出错,试试下面的步骤
在库后面加上这行代码
useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&allowMultiQueries=true
若还不行 就没办法了。。。