I am creating a Web Application using Java and Spring. I need to connect to MySQL database and retrieve data in Servlet to display in my JSP page. I searched lot and got many examples to connect to database, but nothing works for me
Here is my 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
From my servlet I am calling a class called GetTargetFields
. In that class I want to pass the query and retrieve the data. But I don't know how to pass. Tried many examples using the query()
method of JdbcTemplate
class. But it is showing error as I dont know which class to pass and what is the return type. Its return type is Object
. I tried ResultSet
but showed error.
What I want is in my GetTargetFields
class I want to pass a select query and return the result and I want to save the result in ResultSet.
public class GetTargetFields
{
public void getTargetField()
{
// What code should be here?
}
}
Can anyone help me to code? Thanks
UPDATE 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;
}
}
You are not injecting or autowiring the jdbcTemplate instance into your GetTargetFields class. It should be either injected like this.
<bean id="getTargetFields" class="com.app.myapp.controllers.controllerClasses.GetTargetFields">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
or autowire it using the @Autowired
annotation.
In the current case even though you have a setter method for data source you are neither injecting that as well. You do not need to create a new instance of JdbcTemplate
as you have already done the bean definition in app-context file and Spring will instantiate it for you. So the correct implementation should be to inject the jdbcTemplate
You need to use the following syntax
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;
}
});
You will have to either inject your jdbcTemplate object manually using the following:
<bean id="getTargetFields" class="com.app.myapp.controllers.controllerClasses.GetTargetFields">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
Then, you can change the class that injects the jdbcTemplate
object to:
public class GetTargetFields {
private JdbcTemplate jdbcTemplate;
...
}
Or use the @Autowired
and @Repository
annotations.
Please see the first half of the following Spring reference which shows how can use those two annotations that would take away much of the complexity: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html
You have the following method in your GetTargetFields class
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplate = new JdbcTemplate(this.dataSource);
}
This means that, during your bean initialization, you can give a datasource as a property which will be injected by spring framework.
<bean id="getTargetFields" class="com.app.myapp.controllers.controllerClasses.GetTargetFields">
<property name="dataSource" ref="dataSource">
</bean>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.