我已经克隆了一份 gs-relational-data-access-complete到我的工作区,下载了 Apache Derby 的独立副本并启动它“./bin/startNetworkServer”。
我想让我的 Spring Boot 项目使用 Apache Derby 的外部实例,而不是 Spring Boot 使用的默认嵌入式 H2 数据库。为了节省时间,我想使用 Spring Boot @Autowired 注释。
当我启动 Derby 的新副本时没有问题:
./bin/startNetworkServer
Sat Nov 21 11:11:06 GMT 2015 : Security manager installed using the Basic server security policy.
Sat Nov 21 11:11:06 GMT 2015 : Apache Derby Network Server - 10.12.1.1 - (1704137) started and ready to accept connections on port 1527
当我启动 Spring Boot 应用程序时出现问题:
2015-11-21 11:55:07.312 ERROR 11361 --- [ main] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
java.sql.SQLException: Driver:org.apache.derby.jdbc.EmbeddedDriver@db9d03fc returned null for URL:jdbc:derby://10.12.1.1:1527/example1;create=true;user=test1;password=pass1
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:326) ~[tomcat-jdbc-8.0.28.jar:na]
如果您能提供有关如何解决此问题同时仍利用 Spring Boot @Autowired 注释的任何帮助,我们将不胜感激。
我的工作区如下:
pom.xml
src/main/java/hello
----Application.java
----Customer.java
src/main/resources
----application.properties
pom.xml:
<?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>org.springframework</groupId>
<artifactId>gs-relational-data-access</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
应用程序属性:
spring.datasource.url=jdbc:derby://10.12.1.1:1527/example1;create=true;user=test1;password=pass1
spring.datasource.username=test1
spring.datasource.password=pass1
应用程序.java:
package hello;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@SpringBootApplication
public class Application implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String args[]) {
SpringApplication.run(Application.class, args);
}
@Autowired
JdbcTemplate jdbcTemplate;
最佳答案
这是一个不匹配:
Driver:org.apache.derby.jdbc.EmbeddedDriver@db9d03fc returned null
for URL:jdbc:derby://10.12.1.1:1527/example1;create=true;user=test1;password=pass1
您指定要使用 Derby 嵌入式 驱动程序,但您已为 Derby 提供数据库的客户端-服务器 URL。
Derby 检测到不匹配并拒绝您的连接尝试。
如果您想使用客户端-服务器 URL,您需要使用 Derby ClientDriver 类,并且您需要在您的 CLASSPATH 中包含 derbyclient.jar。
如果要使用内嵌驱动,需要使用内嵌驱动的URL,例如
jdbc:derby:example1;create=true;user=test1;password=pass1
关于java - 使用 Derby 配置 Spring-Boot Autowiring 的 JdbcTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33843709/