Java Web应用连接MySQL报错:密钥库文件异常问题求助
问题:GlassFish连接MySQL时密钥库错误导致HTTP 500异常
环境信息
- 开发工具:Apache NetBeans IDE 18
- JDK版本:20
- 操作系统:Windows 10
- 应用服务器:GlassFish 7.0.3
- 数据库:运行中的MySQL 8.0 CE,目标数据库
library - 问题场景:Web应用的GET方法正常执行,但执行SQL查询时,浏览器返回HTTP Status 500 - Internal Server Error
异常链详情
HTTP Status 500 - Internal Server Error type Exception report messageInternal Server Error descriptionThe server encountered an internal error that prevented it from fulfilling this request. exception jakarta.servlet.ServletException: Servlet Could not display records. root cause java.sql.SQLNonTransientConnectionException: Cannot open file:C:\Users\Subhranshu\glassfish7\glassfish\domains\domain1/config/keystore.jks [Keystore was tampered with, or password was incorrect] root cause com.mysql.cj.exceptions.SSLParamsException: Cannot open file:C:\Users\Subhranshu\glassfish7\glassfish\domains\domain1/config/keystore.jks [Keystore was tampered with, or password was incorrect] root cause java.io.IOException: Keystore was tampered with, or password was incorrect root cause java.security.UnrecoverableKeyException: Password verification failed
相关代码片段
Connection con; con = null; PreparedStatement stmt = null; ResultSet rs = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); con = DriverManager.getConnection //("jdbc:mysql://127.0.0.1:3306/library","root",""); ("jdbc:mysql://localhost:3306/library","root",""); stmt = con.prepareStatement("select * from users where username = ? and password = ?"); stmt.setString(1, username); stmt.setString(2, password); rs = stmt.executeQuery();
解决方法
1. 临时禁用SSL连接(快速修复)
在JDBC连接URL中添加useSSL=false参数,跳过SSL证书验证,直接建立非加密连接:
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/library?useSSL=false", "root", "");
2. 修复GlassFish密钥库(如需启用SSL)
如果必须使用SSL连接,需处理损坏或密码错误的密钥库:
- 确认密钥库路径:
C:\Users\Subhranshu\glassfish7\glassfish\domains\domain1/config/keystore.jks - 验证默认密码:GlassFish默认密钥库密码为
changeit,若修改过需确认正确 - 重新生成密钥库(若文件损坏):
- 删除原
keystore.jks文件 - 打开命令提示符,进入上述config目录
- 执行以下命令生成新密钥库:
keytool -genkey -alias glassfish-instance -keyalg RSA -keystore keystore.jks -storepass changeit - 重启GlassFish服务器
- 删除原
3. 确认MySQL JDBC驱动兼容性
确保项目使用的MySQL JDBC驱动版本与MySQL 8.0匹配,推荐使用mysql-connector-java:8.0.33及以上版本,可通过NetBeans的项目依赖管理添加正确版本。
内容的提问来源于stack exchange,提问作者Subhranshu Mohapatra
相关产品推荐
相关产品推荐

