Spring Boot MavenでSQLite3に接続したいのですが、
STSを使用して「Spring Boot アプリケーション」を起動すると、
以下のエラーが出て起動出来ません。
(文字制限内とする為、import文などを削除してあります。)
「...Cannot load driver class: "org.sqlite.JDBC"」
###発生している問題・エラーメッセージ
2016-12-22 14:55:50.236 WARN 7824 --- [main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: "org.sqlite.JDBC" ...
###該当のソースコード
SQLiteOperatorApplication.java
@SpringBootApplication public class SQLiteOperatorApplication { public static void main(String[] args) { SpringApplication.run(SQLiteOperatorApplication.class, args); } }
ServletInitializer.java
public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SQLiteOperatorApplication.class); } }
Operator.java
@Entity @Table(name="operator") public class Operator { @Id @Column @NotNull @NotEmpty @Getter @Setter private int operatorId; @Column @NotNull @NotEmpty @Getter @Setter private String employmentId; @Column @NotNull @NotEmpty @Getter @Setter private String password; @Column @NotNull @NotEmpty @Getter @Setter private String userName; @Column @NotNull @NotEmpty @Getter @Setter private int userAuthority; }
OperatorController.java
@Controller public class OperatorController { protected static Logger log = LoggerFactory.getLogger( OperatorController.class ); @Autowired OperatorRepository repository; @RequestMapping(value = "/", method = RequestMethod.GET) public ModelAndView index( @ModelAttribute("formModel")Operator operator, ModelAndView mav) { log.info("★★★root accessed.★★★"); mav.setViewName("index"); mav.addObject("msg","this is operator list."); mav.addObject("formModel",operator); Iterable<Operator> list = repository.findAll(); mav.addObject("operatorlist",list); return mav; } @RequestMapping(value = "/", method = RequestMethod.POST) @Transactional(readOnly=false) public ModelAndView form( @ModelAttribute("formModel") @Validated Operator operator, BindingResult result, ModelAndView mov) { ModelAndView res = null; if (!result.hasErrors()){ repository.saveAndFlush(operator); res = new ModelAndView("redirect:/"); } else { mov.setViewName("index"); mov.addObject("msg","sorry, error is occured..."); Iterable<Operator> list = repository.findAll(); mov.addObject("datalist",list); res = mov; } return res; } }
OperatorRepository.java
@Repository public interface OperatorRepository extends JpaRepository<Operator, Long> { public Operator findByOperatorId(int operatorId); public List<Operator> findByEmploymentId(String employmentId); public List<Operator> findByPasswordLike(String password); public List<Operator> findByUserNameLike(String userName); }
index.html
<body> <h1 th:text="${msg}"></h1> <table border="2"> <tr><th>ID</th><th>職番</th><th>パスワード</th><th>氏名</th><th>権限</th></tr> <tr th:each="obj : ${operatorlist}"> <td th:text="${obj.operatorId}"></td> <td th:text="${obj.employmentId}"></td> <td th:text="${obj.password}"></td> <td th:text="${obj.userName}"></td> <td th:text="${obj.userAuthority}"></td> </tr> </table> </body> </html>
application.properties
spring.datasource.driverClassName="org.sqlite.JDBC" spring.datasource.url="jdbc:sqlite:c:/operator.db"
pom.xml
<modelVersion>4.0.0</modelVersion> <groupId>com.operator</groupId> <artifactId>Operator</artifactId> <version>1.00</version> <packaging>jar</packaging> <name>SQLite</name> <description>Operator</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <scope>compile</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </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>
###試したこと
<scope>compile</scope>
を外してみましたが、同様でした。
pom.xml
<dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> </dependency>
PostgreSQLでは一覧表示したので、ロジックは合っていると思います。
pom.xml
<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency>
application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/operator spring.datasource.username=postgres spring.datasource.password=xxxxxxxx spring.datasource.driverClassName=org.postgresql.Driver
###補足情報(言語/FW/ツール等のバージョンなど)
Windows7
sts-3.8.2
java8
sqlte-jdbc-3.8.11.2.jar
を「参照ライブラリー」へ「ビルドパス」として構成しています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。