Spring. 无法加载驱动程序类: org.hsqldb.jdbc.JDBCDriver

huangapple go评论91阅读模式
英文:

Spring. Cannot load driver class: org.hsqldb.jdbc.JDBCDriver

问题

我正在创建一个简单的应用程序来尝试使用hsqldb数据库,但是我甚至无法开始,因为我的IDE给了我一个异常:

> "无法加载驱动程序类:org.hsqldb.jdbc.JDBCDriver"

我已经研究了这个问题,但是我没有找到解决方案。
我正在使用Spring Tool Suite 4.4作为我的IDE。

这是我的`application.properties`文件:

spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
spring.datasource.url=jdbc:hsqldb:mem:testdb
spring.datasource.username=sa
spring.datasource.password=

这是我的`pom.xml`(我添加了比我需要的更多的内容,我以为问题可能是“依赖不足”):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.xerial</groupId>
    <artifactId>sqlite-jdbc</artifactId>
    <version>3.32.3.2</version>
</dependency>

<!-- Derby DB -->
<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
    <version>${derby.version}</version>
</dependency>

<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>2.3.2</version>
    <scope>test</scope>
</dependency>

我还尝试过更改hsqldb依赖的版本,但没有成功。

这是我的简单**dao**类:

@Transactional
@Repository
public class ProductDaoImpl implements ProductDao {

@Autowired
private JdbcTemplate jdbcTemplate;

@Override
public List<Product> getAllProducts() {
    String query = "SELECT * from products";
    RowMapper<Product> rowMapper = new ProductRowMapper();
    List<Product> list = jdbcTemplate.query(query, rowMapper);
    return list;
}

@Override
public void addProduct(Product product) {
    String query = "INSERT INTO products(id, name) VALUES(?, ?)";
    jdbcTemplate.update(query, product.getId(), product.getName());

}

}

我还检查了驱动程序的类路径,它是正确的。我无法想象我做错了什么。
英文:

I am creating a simple app to experiment with the hsqldb database, but i cant even start because my IDE giving me an exeption

> "Cannot load driver class: org.hsqldb.jdbc.JDBCDriver"

I've researched this problem, but i didnt find the solution.
I am using Sprint tool suit 4.4 as my IDE.

This is my application.properties file:

spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
spring.datasource.url=jdbc:hsqldb:mem:testdb
spring.datasource.username=sa
spring.datasource.password=

This is my pom.xml (i have added more than i need, i thought that maybe the problem is "not enough dependencies"):

    &lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-data-jdbc&lt;/artifactId&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
		&lt;scope&gt;test&lt;/scope&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.xerial&lt;/groupId&gt;
		&lt;artifactId&gt;sqlite-jdbc&lt;/artifactId&gt;
		&lt;version&gt;3.32.3.2&lt;/version&gt;
	&lt;/dependency&gt;


	&lt;!-- Derby DB --&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.apache.derby&lt;/groupId&gt;
		&lt;artifactId&gt;derby&lt;/artifactId&gt;
		&lt;version&gt;${derby.version}&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.hsqldb&lt;/groupId&gt;
		&lt;artifactId&gt;hsqldb&lt;/artifactId&gt;
		&lt;version&gt;2.3.2&lt;/version&gt;
		&lt;scope&gt;test&lt;/scope&gt;
	&lt;/dependency&gt;

I also tried to change the version of hsqldb dependency but i didnt work.

This is my simple dao class:

@Transactional
@Repository
public class ProductDaoImpl implements ProductDao {

@Autowired
private JdbcTemplate jdbcTemplate;

@Override
public List&lt;Product&gt; getAllProducts() {
	String query = &quot;SELECT * from products&quot;;
	RowMapper&lt;Product&gt; rowMapper = new ProductRowMapper();
	List&lt;Product&gt; list = jdbcTemplate.query(query, rowMapper);
	return list;
}

@Override
public void addProduct(Product product) {
	String query = &quot;INSERT INTO products(id, name) VALUES(?, ?)&quot;;
	jdbcTemplate.update(query, product.getId(), product.getName());

}

}

I also checked the class path of the driver, and it is in place. I cant imagine what i am doing wrong.

答案1

得分: 3

从依赖中删除&lt;scope&gt;test&lt;/scope&gt;。您并未仅在测试中使用hsqldb。
另外,spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver是多余的,因为驱动程序已包含在数据源URL中。

英文:

Remove the &lt;scope&gt;test&lt;/scope&gt; from the dependency. You are not using the hsqldb for tests only.
Also, the spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver is redundant, since the driver is part of the datasource url.

huangapple
  • 本文由 发表于 2020年9月12日 16:43:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63858503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定