英文:
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"):
<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>
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<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 also checked the class path of the driver, and it is in place. I cant imagine what i am doing wrong.
答案1
得分: 3
从依赖中删除<scope>test</scope>
。您并未仅在测试中使用hsqldb。
另外,spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
是多余的,因为驱动程序已包含在数据源URL中。
英文:
Remove the <scope>test</scope>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论