在初学Java连接Druid数据库连接池时,遇到的问题及解决如下:
程序运行时报以下错误:
![](https://i-blog.csdnimg.cn/blog_migrate/840a7eeb68e69d7e4abdf758626d3c0d.png)
2月 09, 2023 9:48:16 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Client does not support authentication protocol requested by server; consider upgrading MySQL client
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:78)
at.....
错误原因:
mysql8 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password,所以可以需要改变mysql的加密规则
解决:
①. 打开cmd窗口,登录mysql;
mysql -u root -h 127.0.0.1 -P3306 -p
- -u 后面root 为自己数据库的用户名
- -h 后面为 (mysql服务所在地址),我自己是在本机测试,所以为127.0.0.1
- -P (可选,默认3306 指定端口号)
②. 设置加密方式
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你自己的密码';
③. 让设置生效
FLUSH PRIVILEGES;
④. 查看设置是否成功
SELECT user,host,plugin from mysql.user;
2.程序运行时报以下错误:
![](https://i-blog.csdnimg.cn/blog_migrate/af9465bac028461dd8379f512478ec19.png)
2月 09, 2023 9:56:43 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
Exception in thread "main" java.sql.SQLException: Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1056)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
...
错误原因:
![](https://i-blog.csdnimg.cn/blog_migrate/709ebc59ca0e73dad6c52a75b48ec043.png)
已添加&characterEncoding=utf8
数据库连接加载配置文件编码错误
解决:
在url处加上&characterEncoding=utf8
问题总结:
出现这两个问题一般是刚接触数据库连接池,随着对Druid的深入学习,后边很多相关问题还等着我们去探索.
Druid数据库连接池的基本使用:
/*
问题1:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Client does not support authentication protocol requested by server; consider upgrading MySQL client
解决:
①. 打开cmd窗口,登录mysql;
mysql -u root -h 127.0.0.1 -P3306 -p
- -u 后面root 为自己数据库的用户名
- -h 后面为 (mysql服务所在地址),我自己是在本机测试,所以为127.0.0.1
- -P (可选,默认3306 指定端口号)
②. 设置加密方式
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你自己的密码';
③. 让设置生效
FLUSH PRIVILEGES;
④. 查看设置是否成功
SELECT user,host,plugin from mysql.user;
问题2:
Unknown initial character set index '255' received from server. Initial client character set can be
解决:
数据库连接编码错误 url=jdbc:mysql://localhost:3306/design?useSSL=false&useServerPrepStmts=true&characterEncoding=utf8
在url处加上characterEncoding=utf8
* */
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.util.Properties;
/*
Druid数据库连接池演示
* */
public class Druid_Demo {
public static void main(String[] args) throws Exception {
//1.导入jar包
//2.定义配置文件
//3.加载配置文件
Properties prop=new Properties();
prop.load(new FileInputStream("src/druid.properties"));
//4.获取连接池对象
DataSource dataSource=DruidDataSourceFactory.createDataSource(prop);
//5.获取数据库连接
Connection connection=dataSource.getConnection();
System.out.println(connection);
}
}
运行结果:
![](https://i-blog.csdnimg.cn/blog_migrate/e91936083201ba3e402b9bea70c2d5eb.png)