我将Maven项目与JSP servlet一起使用。
在Main类中,我通过以下方式连接:
Connection connection = ConnectionFactory.getInstance().getConnection();
ConnectionFactory类的逻辑是:
public class ConnectionFactory {
static final Logger logger = Logger.getLogger(ConnectionFactory.class);
private static final String PATH_TO_PROPERTIES = RootPathUtil.getRootApplicationClassPath("database.properties");
private static final Properties properties;
static {
InputStream inputStream;
Properties prop = new Properties();
try {
inputStream = new FileInputStream(PATH_TO_PROPERTIES);
prop.load(inputStream);
logger.info("Connection Factory Property loaded.\n\t\tProperty file location: " + PATH_TO_PROPERTIES);
} catch (FileNotFoundException e) {
logger.error("Property file \"database.properties\" didn't found. Database main properties didn't initialize.");
e.printStackTrace();
} catch (IOException e) {
logger.error("IO exception during property file \"database.properties\" loading. Database main properties didn't initialize.");
e.printStackTrace();
}
properties = prop;
}
private static ConnectionFactory instance = new ConnectionFactory();
private BasicDataSource ds;
private ConnectionFactory() {
if (properties.isEmpty()){
logger.error("Property instance is empty. Database main properties didn't initialize. ConnectionFactory creation failed.");
throw new PropertyLoadException("ConnectionFactory creation failed. Property didn't load.");
}
ds = new BasicDataSource();
ds.setDriverClassName(properties.getProperty("dbDriver"));
ds.setUsername(properties.getProperty("userName"));
ds.setPassword(properties.getProperty("password"));
ds.setUrl(properties.getProperty("connectionUrl"));
ds.setMinIdle(Integer.parseInt(properties.getProperty("connection_pull.min_idle")));
ds.setMaxIdle(Integer.parseInt(properties.getProperty("connection_pull.max_idle")));
ds.setMaxOpenPreparedStatements(Integer.parseInt(properties.getProperty("connection_pull.max_open_prepared_statements")));
}
public static ConnectionFactory getInstance() {
if (instance == null) {
instance = new ConnectionFactory();
return instance;
} else {
return instance;
}
}
public Connection getConnection() throws SQLException {
return this.ds.getConnection();
}
}
当我运行Tomcat Server时,我总是遇到如下问题:
java.sql.SQLException: Cannot create PoolableConnectionFactory (Could not create connection to database server.)
at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2294)
at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2039)
at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1533)
at com.test.project.carrental.data.service.ConnectionFactory.getConnection(ConnectionFactory.java:69)
at com.test.project.carrental.data.service.DataBaseUtil.getConnection(DataBaseUtil.java:21)
at com.test.project.carrental.controller.filter.JDBCFilter.doFilter(JDBCFilter.java:63)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.test.project.carrental.controller.filter.LogSessionIdFilter.doFilter(LogSessionIdFilter.java:39)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
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.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:917)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2330)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
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.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
at org.apache.commons.dbcp2.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:39)
at org.apache.commons.dbcp2.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:256)
at org.apache.commons.dbcp2.BasicDataSource.validateConnectionFactory(BasicDataSource.java:2304)
at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2290)
... 27 more
Caused by: java.lang.NullPointerException
at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:2997)
at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1865)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1228)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
... 41 more
为了解决这个问题,我尝试了不同版本的mysql连接器-java
依赖,如:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
此外,正如这里和这里提到的,我尝试使用:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
使用5.1.41
或5.1.46的替代修复对我也没有帮助。
同时,我尝试使用各种连接属性:
dbDriver=com.mysql.jdbc.Driver
connectionUrl=jdbc:mysql://localhost:3306/db?verifyServerCertificate=false&useSSL=true
userName=...
password=...
connection_pull.min_idle=5
connection_pull.max_idle=20
connection_pull.max_open_prepared_statements=180
我已经测试了不同的变体,如:
#connectionUrl=jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
#connectionUrl=jdbc:mysql://localhost:3306/db?autoReconnect=true
#connectionUrl=jdbc:mysql://localhost:3306/db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
#connectionUrl=jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
#connectionUrl=jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
#connectionUrl=jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&serverTimezone=UTC&useSSL=false
一些连接是基于此的
UPD:
我已经添加了我的POM文件,因为正如下面的注释所提到的,MySQL Connector/J8.0.11
包含连接字符串的两个变体。
<?xml version="1.0" encoding="UTF-8"?>
<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>CarRental</groupId>
<artifactId>CarRental</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Car Rental Service</name>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
<junit.version>4.12</junit.version>
<mokito.version>1.10.19</mokito.version>
<slf4j.version>1.7.21</slf4j.version>
</properties>
<dependencies>
<!-- SERVLETS AND VIEWS-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<!-- DATDBASE DRIVER -->
<!--<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<!-- TESTING -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mokito.version}</version>
<scope>test</scope>
</dependency>
<!-- LOGGING -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
<build>
<finalName>CarRentalService</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<encoding>UTF-8</encoding>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</version>
<configuration>
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>${basedir}/target/coverage-reports/jacoco-unit.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
我试图在没有Intelij IDEA战争文件的情况下运行-同样的问题。
我花了几天时间来解决这个问题。我已经测试了许多在不同答案中提到的方法,但没有一种有效。
我的特定问题的原因是在dbDriver=com.mysql.jdbc.Driver
中。为了解决这个问题,我已经更改为dbDriver=com.mysql.cj.jdbc.Driver
。
例如,可以在此处阅读更多信息。
注意:我使用的是MySQL服务器8.0.20
UPD:事实证明,我没有检查目标文件夹,该文件夹位于带有罐的WEB-INF/lib中。
一旦我清理了不必要的版本,程序也开始使用dbDriver=com.mysql.jdbc.Driver
。解压提示作为解决方案也很有帮助。
获取以下代码的错误 我得到的输出错误是
我正在处理一个奇怪的问题,即使用JDBC在AmazonRDS上连接MySQL数据库。我有两部不同的Android手机在同一个网络上运行相同的代码。一个人连接起来没有任何问题。另一个是给我以下的例外: 数据库只显示1个活动连接,所以我知道这不是因为打开的连接太多。我尝试重新启动服务器,但没有帮助。我使用的是MySQL JDBC Connector/J版本5.1.23。工作的手机运行的是Android
我有一个本地MySQL社区服务器(8.0.11)运行在本地主机。 (我创建了一个数据库并为其注册了一个用户)。我可以使用以下终端查询访问数据库: 我能够访问服务器。 我使用Spring和JDBC使用以下代码做同样的事情: 但是我得到了以下错误: 在错误消息的底部,我得到一个提示,它是由空指针异常引起的,但我就是不知道什么可以在我的查询/代码中为空?一切都可以完美地与终端,只是不是与JDBC。
我创建了一个简单的Spring启动应用程序,以使用相同的网络通过docker与MYSQL通信。一旦我运行命令,就会发生以下错误 雇员-jdbc-容器|java.sql.SQLNon瞬态连接异常:无法创建与数据库服务器的连接。尝试重新连接3次。放弃。 员工jdbc容器|由:com引起。mysql。cj。例外情况。UnableToConnectionException:不允许检索公钥 docker编写
问题内容: 我今天搬到了macOS,并重新创建了一个非常简单的数据库测试项目来检查mySQL连接。但是我收到一个SQL异常:“无法创建与数据库服务器的连接”。这是完整的日志: https //pastebin.com/iZrktVKn 我照搬了我在Windows上所做的每一步。在Windows上,一切都很好。我有一个context.xml,一个Servlet和一个connector.jar文件。该
Payara错误 连接池 资源 本地数据库 https://pastebin.com/QSKEL0Kd 我用mysql workbench在localhost上创建了一个数据库,用户:root,端口:3306,密码正确。当我输入localhost时,我也检查了两次,但没有工作