Java 无法创建PoolableConnectionFactory[JSP servlet和MySQL]

Java 无法创建PoolableConnectionFactory[JSP servlet和MySQL],java,mysql,jsp,servlets,glassfish,Java,Mysql,Jsp,Servlets,Glassfish,我遇到以下错误:java.sql.SQLException:无法创建PoolableConnectionFactory无法打开文件:C:\AppServers\glassfish5\glassfish\domains\domain1/config/keystore.jks[密钥库被篡改,或密码不正确] 通过简单地在MySQL数据库连接上添加useSSL=false,并在DBConnection类中添加JDBC_URL字符串,解决了这个问题 数据库连接 pom.xml 错误 您的问题与NullPo

我遇到以下错误:java.sql.SQLException:无法创建PoolableConnectionFactory无法打开文件:C:\AppServers\glassfish5\glassfish\domains\domain1/config/keystore.jks[密钥库被篡改,或密码不正确]

通过简单地在MySQL数据库连接上添加useSSL=false,并在DBConnection类中添加JDBC_URL字符串,解决了这个问题

数据库连接

pom.xml

错误


您的问题与NullPointerException无关。您的异常处理很糟糕,您需要修复它,这样您就不会再次感到困惑

问题 您的异常处理方案可以归结为:将一些信息打印到标准输出,然后继续。最后一点就是问题所在:您的代码并不是按照这样的理念编写的:每一行都可能只是做一些事情,或者什么都不做,这取决于是否发生异常。因此,您的代码一直在运行,因为事情没有处于您预期的状态,所以会发生更多的错误,这些错误是完全不相关的-它们不是问题所在,但它们会阻塞日志并使您感到困惑

处理例外情况的正确策略是处理它们。不管怎样,只要把它打印出来,然后继续下去,就不会和他们打交道了。如果你不能处理它们,这是常见的情况,那么就让异常冒出来吧。除非您可以编写解决问题的代码,否则不要捕获它。一个没有任何东西可以处理的异常应该一直冒泡并卷起线程,这是最好的解决方案,也是最少的代码,所以这是一个双赢的结果

显然打算与数据库交互的方法应该声明为抛出SQLException,而您的公共静态void main方法应该声明为抛出Exception,这是允许的

如果你不知何故必须捕获一个检查过的异常,即使你不知道该做什么,那是不好的,那么至少把它放在下面;抛出新的RuntimeExceptionUnhandled,e;在catch块中,而不是'e.printStackTraceSystem.out'

您实际的异常问题 您的mysql驱动程序正在尝试加载损坏的密钥库。这是一个相当棘手的问题

很可能您已经添加了一个配置,或者正在应用一个默认配置,该配置在
C:\AppServers\glassfish5\glassfish\domains\domain1/config/keystore.jks-我认为问题不在于斜杠的混合,java通常对此理解得很好,但我打赌该文件不存在。或者,您试图创建密钥文件,但发生了异常,同样令人遗憾的异常处理导致您没有意识到密钥文件已损坏。检查文件-它可能根本不在那里,或者0字节。重新生成它,或修复配置。

我通过在数据库URL中添加useSSL=false来解决问题。

它看起来像是一个关闭调用DBConnection.Closer;,DBConnection.closestmt;或DBConnection.closeconn;正在将null对象传递给close方法`调试代码以确定哪一个为null,以及它为什么为null是否已关闭?然后检查null或捕获null并采取措施解决它。只是想检查一下,您的代码是否真的从数据库中获取了用户?堆栈跟踪的其余部分在哪里?很可能try块中的某个内容抛出,导致rs或stmt甚至conn为null,并在finally块中抛出NPE。我们需要查看完整的堆栈跟踪。始终包含完整的堆栈跟踪。@Sorifind我标记了一个调试点,但当我在调试模式下运行web应用时,我无法前进任何一行。不知道如何搜索空值。@JimGarrison我刚刚添加了完整的堆栈跟踪。抱歉,这个问题与您的代码无关,NPE只是一个更严重问题的副作用,Java不喜欢您作为密钥库提供的文件。从这里开始。非常感谢你的回答。我找到了这个文件,它有5KB,所以我可以说它不是空的。我读到GlassFish有时会在一些版本中将默认密码从changeme更改为changeit。我看不懂这个文件,不知道怎么读。我还没有找到如何编辑它。难得的是,我有另一个web应用程序具有类似的代码,工作正常,使用完全相同的过程创建。它在JDK发行版中的keytool工具,位于jar和javac旁边,可以使用。在网上搜索有关如何调用它的教程。这是一个命令行工具。
package data;

import java.sql.*;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;

public class DBConnection {
    
    private static final String JDBC_URL = "jdbc:mysql://localhost:3306/course_management_system?zeroDateTimeBehavior=CONVERT_TO_NULL&useTimezone=true&serverTimezone=UTC";
    private static final String JDBC_USER = "root";
    private static final String JDBC_PASSWORD = "admin";

    private static BasicDataSource dataSource;

    public static DataSource getDataSource() {
        if (dataSource == null) {
            dataSource = new BasicDataSource();
            dataSource.setUrl(JDBC_URL);
            dataSource.setUsername(JDBC_USER);
            dataSource.setPassword(JDBC_PASSWORD);
            dataSource.setInitialSize(50);
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace(System.out);
            }
        }
        return dataSource;
    }

    public static Connection getConnection() throws SQLException {
        return getDataSource().getConnection();
    }

    public static void close(ResultSet rs) {
        try {
            rs.close();
        } catch (SQLException ex) {
            ex.printStackTrace(System.out);
        }
    }

    public static void close(PreparedStatement stmt) {
        try {
            stmt.close();
        } catch (SQLException ex) {
            ex.printStackTrace(System.out);
        }
    }

    public static void close(Connection conn) {
        try {
            conn.close();
        } catch (SQLException ex) {
            ex.printStackTrace(System.out);
        }
    }
}

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>mb</groupId>
    <artifactId>CourseManagementSystem</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>
    <name>CourseManagementSystem</name>
    
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <jakartaee>8.0</jakartaee>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>${jakartaee}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.22</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.8.0</version>
        </dependency>
    </dependencies>
    
     <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-api</artifactId>
                                    <version>${jakartaee}</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
CourseManagementSystem was successfully deployed in 316 milliseconds.|#]
  java.sql.SQLException: Cannot create PoolableConnectionFactory (Cannot open file:C:\AppServers\glassfish5\glassfish\domains\domain1/config/keystore.jks [Keystore was tampered with, or password was incorrect])
    at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:656)
    at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:534)
    at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:734)
    at data.DBConnection.getConnection(DBConnection.java:35)
    at data.UserDAO.list(UserDAO.java:30)
    at web.UserController.doGet(UserController.java:18)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:645)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1540)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:119)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:611)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:550)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:114)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:199)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:463)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:168)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:242)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.sql.SQLNonTransientConnectionException: Cannot open file:C:\AppServers\glassfish5\glassfish\domains\domain1/config/keystore.jks [Keystore was tampered with, or password was incorrect]
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:70)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:833)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:453)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
    at org.apache.commons.dbcp2.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:52)
    at org.apache.commons.dbcp2.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:357)
    at org.apache.commons.dbcp2.BasicDataSource.validateConnectionFactory(BasicDataSource.java:103)
    at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:652)
    ... 35 more
Caused by: com.mysql.cj.exceptions.SSLParamsException: Cannot open file:C:\AppServers\glassfish5\glassfish\domains\domain1/config/keystore.jks [Keystore was tampered with, or password was incorrect]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
    at com.mysql.cj.protocol.ExportControlled.getSSLContext(ExportControlled.java:550)
    at com.mysql.cj.protocol.ExportControlled.performTlsHandshake(ExportControlled.java:303)
    at com.mysql.cj.protocol.StandardSocketFactory.performTlsHandshake(StandardSocketFactory.java:188)
    at com.mysql.cj.protocol.a.NativeSocketConnection.performTlsHandshake(NativeSocketConnection.java:97)
    at com.mysql.cj.protocol.a.NativeProtocol.negotiateSSLConnection(NativeProtocol.java:333)
    at com.mysql.cj.protocol.a.NativeAuthenticationProvider.connect(NativeAuthenticationProvider.java:167)
    at com.mysql.cj.protocol.a.NativeProtocol.connect(NativeProtocol.java:1350)
    at com.mysql.cj.NativeSession.connect(NativeSession.java:157)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:953)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:823)
    ... 42 more
Caused by: java.io.IOException: Keystore was tampered with, or password was incorrect
    at sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:782)
    at sun.security.provider.JavaKeyStore$JKS.engineLoad(JavaKeyStore.java:56)
    at sun.security.provider.KeyStoreDelegator.engineLoad(KeyStoreDelegator.java:224)
    at sun.security.provider.JavaKeyStore$DualFormatJKS.engineLoad(JavaKeyStore.java:70)
    at java.security.KeyStore.load(KeyStore.java:1445)
    at com.mysql.cj.protocol.ExportControlled.getSSLContext(ExportControlled.java:530)
    ... 51 more
Caused by: java.security.UnrecoverableKeyException: Password verification failed
    at sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:780)
    ... 56 more
|#]
  StandardWrapperValve[web.UserController]: Servlet.service() for servlet web.UserController threw exception
java.lang.NullPointerException
    at data.DBConnection.close(DBConnection.java:40)
    at data.UserDAO.list(UserDAO.java:51)
    at web.UserController.doGet(UserController.java:18)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:645)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1540)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:119)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:611)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:550)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:114)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:199)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:463)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:168)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:242)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573)
    at java.lang.Thread.run(Thread.java:748)
|#]