gpt4 book ai didi

java - 如何检查h2嵌入式数据库是否已创建?

转载 作者:行者123 更新时间:2023-12-02 11:25:56 25 4
gpt4 key购买 nike

我有带有嵌入式数据库的app-context文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:META-INF/schema.sql" />
<jdbc:script location="classpath:META-INF/data.sql" />
</jdbc:embedded-database>

<bean id="reportDao"
class="jdbc.dao.ImplReportDao"
p:dataSource-ref="dataSource" />

</beans>

我有以下src/META-INF/schema.sql文件:

USE `reports`;

/*Table structure for table `reports` */

DROP TABLE IF EXISTS `reports`;

CREATE TABLE `reports` (
`id` int(9) unsigned zerofill NOT NULL,
`parent_id` int(9) unsigned zerofill NOT NULL DEFAULT '0000',
`name` varchar(99) NOT NULL,
`is_category` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=cp1251;

src/META-INF/data.sql文件:

insert  into `reports`.`reports`(`id`,`parent_id`,`name`,`is_category`) 
values
(0000,0000,'Reports',1),
(0001,0000,'Card emission',1),
(0002,0000,'Technical reports',1);

ImplReportDao 看起来像这样:

public class ImplReportDao implements ReportDao {
private DataSource dataSource;

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

@Override
public List<Report> findAll() {
System.out.println("find all");
List<Report> result = new ArrayList<Report>();
Connection conn = null;
try {
if (dataSource != null) {
System.out.println("dataSource != null");
} else {
System.out.println("dataSource == null");
}

conn = dataSource.getConnection();
PreparedStatement statement = conn.prepareStatement("select * from reports");
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Report report = new Report();
report.setId(resultSet.getInt("id"));
report.setParentId(resultSet.getInt("parent_id"));
report.setName(resultSet.getString("name"));
result.add(report);
}
return result;
} catch (Exception ex) {
return null;
}
}
}

并且类看起来像:

public class Main {
private static ReportDao reportDao = new ImplReportDao();

public static void main(String[] args) {
List<Report> reports = reportDao.findAll();
for (Report report : reports) {
System.out.println("report: " + report.getName());
}
}
}

但是启动后我收到错误 NullPointerException 因为在 ImplReportDao.findAll()dataSource 为空。如何检查h2嵌入式数据库是否创建?

最佳答案

您在 Spring 中配置数据库,然后使用构造函数创建对象。您没有创建 Spring 上下文,这就是未创建数据库的原因。另外,由于您使用构造函数,Spring 无法将数据源注入(inject)到新创建的对象中,因为 Spring IoC 容器不知道它。

因此您需要对代码和配置 XML 进行一些更改。

首先,要使用 p:dataSource-ref,您需要将此行添加到 XML 文件的标题中: xmlns:p="http://www.springframework.org/schema/p" 就在 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 之后。

其次,您需要像这样实例化 Spring 上下文

public class Main {

public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("app-context.xml"); //assuming app-context.xml is in classpath of project
ReportDao reportDao = ctx.getBean(ReportDao.class); //Ask Spring to gice you DAO object with injected dataSource
List<Report> reports = reportDao.findAll();
for (Report report : reports) {
System.out.println("report: " + report.getName());
}
}
}

关于java - 如何检查h2嵌入式数据库是否已创建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49629908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com