前提・実現したいこと
PostgreSQL12.8から統計データを取得して表示させたいです。
データベース、ユーザーを作り、A5:SQL Mk-2によりデータをインポートし、
ソースを書いてSTSを実行したところ、以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
ネットでログメッセージを調べ、pom.xmlにdependencyを追加、application.propertiesを修正等しましたが、どうやってもPostgreSQLにアクセスしている気配がないため、ご教授お願い致します。
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2021-12-17 09:56:42.521 ERROR 39472 --- [ main] o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.RuntimeException: Driver net.sourceforge.jtds.jdbc.Driver claims to not accept jdbcUrl, jdbc:postgresql://localhost:5432/otnj?user=otnj&sslmode=disable&password=<masked> at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.13.jar:5.3.13] 中略 Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.RuntimeException: Driver net.sourceforge.jtds.jdbc.Driver claims to not accept jdbcUrl, jdbc:postgresql://localhost:5432/otnj?user=otnj&sslmode=disable&password=<masked> at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.13.jar:5.3.13] at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.13.jar:5.3.13] ... 19 common frames omitted Caused by: java.lang.RuntimeException: Driver net.sourceforge.jtds.jdbc.Driver claims to not accept jdbcUrl, jdbc:postgresql://localhost:5432/otnj?user=otnj&sslmode=disable&password=<masked> at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:110) ~[HikariCP-4.0.3.jar:na] 中略 ... 20 common frames omitted
該当のソースコード
pom.xml
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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.sct</groupId> <artifactId>OTNJSpring</artifactId> <version>0.0.1</version> <name>OTNJSpring</name> <description>OTNJ Spring Project for Spring Boot</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> </dependency> <dependency> <groupId>net.sourceforge.jtds</groupId> <artifactId>jtds</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties
properties
spring.datasource.url=jdbc:postgresql://localhost:5432/otnj?user=otnj&sslmode=disable&password=otnj spring.datasource.username=otnj spring.datasource.password=otnj spring.datasource.driver-class-name=net.sourceforge.jtds.jdbc.Driver logging.level.org.hibernate.SQL=DEBUG logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE logging.level.org.springframework.web=DEBUG logging.level.org.hibernate=DEBUG spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true
SQL
create table stat( id integer , row integer , col integer , data double precision not null , constraint stat_pk1 primary key (id, row, col) )
Java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class OtnjSpringApplication { public static void main(String[] args) { SpringApplication.run(OtnjSpringApplication.class, args); } }
Java
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class StatController { @Autowired private StatService statService; @GetMapping(value = "/stat") public String displayList(Model model) { List<Stat> statList = statService.searchAll(); model.addAttribute("statList", statList); return "stat"; } }
Java
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class StatService { @Autowired private StatRepository statRepository; public List<Stat> searchAll() { return statRepository.findAll(); } }
Java
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface StatRepository extends JpaRepository<Stat, StatCell> { }
Java
import javax.persistence.Column; import javax.persistence.Embeddable; @Embeddable public class StatCell { @Column(name = "id") private Integer id; @Column(name = "row") private Integer row; @Column(name = "col") private Integer col; }
Java
import java.io.Serializable; import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.IdClass; import javax.persistence.Table; @Entity @Table(name="stat") @IdClass(value=StatCell.class) public class Stat implements Serializable { private static final long serialVersionUID = 6501935237628672264L; @EmbeddedId private StatCell cell; @Column(name = "data") private Double data; }
html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <title>推移</title> <meta charset="utf-8" /> </head> <body> <h1>推移</h1> <table> <thead> <tr> <th>年</th> <th>数</th> </tr> </thead> <tbody> <tr th:each="stat : ${statList}" th:object="${stat}"> <td class="center" th:text="*{row}"></td> <td class="center" th:text="*{data}"></td> </tr> </tbody> </table> </body> </html>
試したこと
JDBCのURLとデータ取得をpsqlで確認しました。
bash
> psql -d 'postgresql://localhost:5432/otnj?user=otnj&sslmode=disable&password=otnj' psql (12.8) Type "help" for help. otnj=> select * from stat; id | row | col | data ----+------+-----+---------- 1 | 1964 | 1 | 352832 1 | 1965 | 1 | 366649 1 | 1966 | 1 | 432937 省略 (58 rows) otnj=>
補足情報(FW/ツールのバージョンなど)
sts-4.13.0.RELEASE
まだ回答がついていません
会員登録して回答してみよう