gpt4 book ai didi

java - 如何进行Spring JDBC连接并检索数据?

转载 作者:行者123 更新时间:2023-12-01 04:44:31 28 4
gpt4 key购买 nike

我正在使用 Java 和 Spring 创建一个 Web 应用程序。我需要连接到 MySQL 数据库并检索 Servlet 中的数据以显示在我的 JSP 页面中。我搜索了很多并得到了很多连接数据库的示例,但没有任何对我有用

这是我的applicationContext.xml

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<!-- 1) USE ANNOTATIONS TO CONFIGURE SPRING BEANS -->
<context:component-scan base-package="com.app.any" />
<!-- 2) DATASOURCE, TRANSACTION MANAGER AND JDBC TEMPLATE -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mydatabase" />
<property name="username" value="root" />
<property name="password" value="admin" />
<bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="getTargetFields" class="com.app.myapp.controllers.controllerClasses.GetTargetFields">
</bean>
<beans

从我的 servlet 中,我调用一个名为 GetTargetFields 的类。在该类中,我想传递查询并检索数据。但我不知道如何通过。使用 JdbcTemplate 类的 query() 方法尝试了许多示例。但它显示错误,因为我不知道要传递哪个类以及返回类型是什么。它的返回类型是Object。我尝试了 ResultSet 但显示错误。

我想要的是在我的 GetTargetFields 类中,我想传递一个选择查询并返回结果,并且我想将结果保存在 ResultSet 中。

public class GetTargetFields
{


public void getTargetField()
{
// What code should be here?
}

}

有人可以帮我编码吗?谢谢

更新1

public class GetTargetFields
{
private JdbcTemplate jdbcTemplate;
private DataSource dataSource;

public static ResultSet rs=null;

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

public ResultSet getTargetField() // I know the code is wrong. But I tried this
{
ResultSet rs=jdbcTemplate.query("SELECT * from employee",
ResultSet); //shows error. I want to get the result in ResultSet
//Or how the data will be stored in *Collector* and I can access each column fields?
return rs;
}

}

最佳答案

您需要使用以下语法

jdbcTemplate.query("", new ResultSetExtractor<Object>() {

@Override
public Object extractData(ResultSet rs) throws SQLException,
DataAccessException {
//do what ever you want to do with rs
return null;
}
});

关于java - 如何进行Spring JDBC连接并检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16054950/

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