MySQL 连接 Java 错误问题

错误

使用MySQL并且经常在使用Java应用程序时遇到这个错误:“java.sql.SQLException: Unknown system variable ‘query_cache_size’“。这是因为MySQL的版本更新后去掉了“query_cache_size”系统变量

解决

解决方法一:在MySQL参数设置中删除“query_cache_size”

SHOW GLOBAL VARIABLES LIKE 'query_cache_size';
SET GLOBAL query_cache_size = 0;

解决方法二:查询系统变量并修改Java应用程序

查询“query_cache_size”的值,并在Java应用程序中进行修改。

查询“query_cache_size”系统变量的值:

SHOW VARIABLES LIKE 'query_cache_size';

在Java应用程序中修改

String url = "jdbc:mysql://localhost:3306/yourdbname?jdbcCompliantTruncation=false&zeroDateTimeBehavior=convertToNull&useSSL=false&useCursorFetch=true&useServerPrepStmts=true";
Properties props = new Properties();
props.setProperty("user", "yourusername");
props.setProperty("password", "yourpassword");
props.setProperty("cachePrepStmts", "true");
props.setProperty("prepStmtCacheSize", "250");
props.setProperty("prepStmtCacheSqlLimit", "2048");
props.setProperty("useServerPrepStmts", "true");
props.setProperty("jdbcCompliantTruncation", "false");
props.setProperty("zeroDateTimeBehavior", "convertToNull");
props.setProperty("useSSL", "false");
props.setProperty("useCursorFetch", "true");
props.setProperty("tcpNoDelay", "1");
props.setProperty("queryCacheType", "FULL");
props.setProperty("useLocalSessionState", "true");
props.setProperty("useLocalTransactionState", "true");
props.setProperty("rewriteBatchedStatements", "true");
props.setProperty("cacheResultSetMetadata", "true");
props.setProperty("cacheServerConfiguration", "true");
props.setProperty("elideSetAutoCommits", "true");
props.setProperty("maintainTimeStats", "false");
props.setProperty("useServerPrepStmts", "true");
props.setProperty("useClientPrepStmts", "false");
props.setProperty("useServerPrepStmts", "true");
props.setProperty("useServerPreparedStmts", "true");
Connection conn = DriverManager.getConnection(url, props);

解决方法三:升级Java驱动程序版本

连接代理出现 java.sql.SQLException: Unknown system variable query_cache 错误,很有可能是 数据库驱动包数据库版本 不匹配,选择对应的 数据库驱动包

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值