我正在使用 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
. 在那个类中,我想传递查询并检索数据。但我不知道如何通过。query()
使用类的方法尝试了许多示例JdbcTemplate
。但它显示错误,因为我不知道要传递哪个类以及返回类型是什么。它的返回类型是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;
}
}