You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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,若修改过需确认正确
  • 重新生成密钥库(若文件损坏):
    1. 删除原keystore.jks文件
    2. 打开命令提示符,进入上述config目录
    3. 执行以下命令生成新密钥库:
      keytool -genkey -alias glassfish-instance -keyalg RSA -keystore keystore.jks -storepass changeit
      
    4. 重启GlassFish服务器

3. 确认MySQL JDBC驱动兼容性

确保项目使用的MySQL JDBC驱动版本与MySQL 8.0匹配,推荐使用mysql-connector-java:8.0.33及以上版本,可通过NetBeans的项目依赖管理添加正确版本。

内容的提问来源于stack exchange,提问作者Subhranshu Mohapatra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 07:27:16