错误
使用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 错误,很有可能是 数据库驱动包 与 数据库版本 不匹配,选择对应的 数据库驱动包