Java 如何执行SpringJDBC连接和检索数据?
我正在使用Java和Spring创建一个Web应用程序。我需要连接到MySQL数据库并在Servlet中检索数据以显示在我的JSP页面中。我搜索了很多,并得到了许多连接到数据库的示例,但对我来说什么都不管用 这是我的Java 如何执行SpringJDBC连接和检索数据?,java,spring,servlets,spring-jdbc,Java,Spring,Servlets,Spring Jdbc,我正在使用Java和Spring创建一个Web应用程序。我需要连接到MySQL数据库并在Servlet中检索数据以显示在我的JSP页面中。我搜索了很多,并得到了许多连接到数据库的示例,但对我来说什么都不管用 这是我的applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xs
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
有人能帮我编码吗?谢谢
更新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实例注入或自动连接到GetTargetFields类中。要么像这样注射
<bean id="getTargetFields" class="com.app.myapp.controllers.controllerClasses.GetTargetFields">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
或者使用@Autowired
注释自动连接
在当前的情况下,即使您有一个数据源setter方法,您也没有注入该方法。您不需要创建
JdbcTemplate
的新实例,因为您已经在app上下文文件中完成了bean定义,Spring将为您实例化它。因此正确的实现应该是注入jdbcTemplate,您需要使用以下语法
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;
}
});
jdbcTemplate.query(“,新结果文本提取程序(){
@凌驾
公共对象提取数据(ResultSet rs)引发SQLException,
DataAccessException{
//你想用rs做什么
返回null;
}
});
您必须使用以下方法手动注入jdbcTemplate对象:
<bean id="getTargetFields" class="com.app.myapp.controllers.controllerClasses.GetTargetFields">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
或者使用@Autowired
和@Repository
注释
请参阅下面的Spring参考资料的前半部分,该部分展示了如何使用这两个注释,这将大大降低复杂性:您的GetTargetFields类中有以下方法
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplate = new JdbcTemplate(this.dataSource);
}
这意味着,在bean初始化期间,您可以将数据源作为属性提供给spring framework,该属性将被注入
<bean id="getTargetFields" class="com.app.myapp.controllers.controllerClasses.GetTargetFields">
<property name="dataSource" ref="dataSource">
</bean>
如何在servlet/应用程序中使用applicationContext.xml。请您向我们展示一些使用JdbcTemplate对象的代码。请发布代码。请参阅更新1为什么需要结果集?@ArunPJohny或如何获得结果?我的意思是读取和处理列字段当您已经注入一个jdbcTemplate实例时,为什么需要创建一个新的jdbcTemplate实例?
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplate = new JdbcTemplate(this.dataSource);
}
<bean id="getTargetFields" class="com.app.myapp.controllers.controllerClasses.GetTargetFields">
<property name="dataSource" ref="dataSource">
</bean>