Required Libraries are,
commons-logging
spring-jdbc
spring-beans
spring-core
spring-tx
spring-asm
mysql-connector-java
spring-context
spring-context-support
spring-expression
You can do this simply by using maven. Just google maven dependencies for spring jdbc template and put the dependencies in your pom.xml file and you are good to go.
Let's see how to use jdbc template in spring to communicate with mysql database.
Create a model class named Book.
package mad.library.form;
public class Book {
private String name;
private String author;
private String category;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
Now create the BookDataManager class as following.
package mad.library.model;
import javax.sql.DataSource;
import mad.library.form.Book;
import org.springframework.jdbc.core.JdbcTemplate;
public class BookDataManager {
private DataSource dataSource;
public void setDataSource(DataSource ds) {
dataSource = ds;
}
public void addBook(Book book) {
JdbcTemplate insert = new JdbcTemplate(dataSource);
insert.update("INSERT INTO Book (name, author,category) VALUES(?,?,?)",
new Object[] { book.getName(), book.getAuthor(),book.getCategory() });
}
}
Then add the spring xml bean configuration file springmodel.xml as below
put it in the same package as BookDataManager (mad.library.model)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd ">
<!-- Initialization for data source -->
<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/library"/>
<property name="username" value="root"/>
<property name="password" value="pass"/>
</bean>
<!-- Definition for studentJDBCTemplate bean -->
<bean id="dao" class="mad.library.model.BookDataManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
Now create a test class
public class Test{
public static void main(String args[]){
ApplicationContext ctx = new ClassPathXmlApplicationContext("mad/library/model/springmodel.xml");
BookDataManager bookDataMngr = (BookDataManager) ctx.getBean("dao");
}
Book book = new Book();
book.setName("operating systems");
book.setAuthor("william pieterson");
book.setCategory("computer science");
bookDataMngr.addBook(book);
}
commons-logging
spring-jdbc
spring-beans
spring-core
spring-tx
spring-asm
mysql-connector-java
spring-context
spring-context-support
spring-expression
You can do this simply by using maven. Just google maven dependencies for spring jdbc template and put the dependencies in your pom.xml file and you are good to go.
Let's see how to use jdbc template in spring to communicate with mysql database.
Create a model class named Book.
package mad.library.form;
public class Book {
private String name;
private String author;
private String category;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
Now create the BookDataManager class as following.
package mad.library.model;
import javax.sql.DataSource;
import mad.library.form.Book;
import org.springframework.jdbc.core.JdbcTemplate;
public class BookDataManager {
private DataSource dataSource;
public void setDataSource(DataSource ds) {
dataSource = ds;
}
public void addBook(Book book) {
JdbcTemplate insert = new JdbcTemplate(dataSource);
insert.update("INSERT INTO Book (name, author,category) VALUES(?,?,?)",
new Object[] { book.getName(), book.getAuthor(),book.getCategory() });
}
}
Then add the spring xml bean configuration file springmodel.xml as below
put it in the same package as BookDataManager (mad.library.model)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd ">
<!-- Initialization for data source -->
<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/library"/>
<property name="username" value="root"/>
<property name="password" value="pass"/>
</bean>
<!-- Definition for studentJDBCTemplate bean -->
<bean id="dao" class="mad.library.model.BookDataManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
Now create a test class
public class Test{
public static void main(String args[]){
ApplicationContext ctx = new ClassPathXmlApplicationContext("mad/library/model/springmodel.xml");
BookDataManager bookDataMngr = (BookDataManager) ctx.getBean("dao");
}
Book book = new Book();
book.setName("operating systems");
book.setAuthor("william pieterson");
book.setCategory("computer science");
bookDataMngr.addBook(book);
}
Hi ......I am struggling in spring jdbc template i want to use single connection throughout my application i know we can use single connection datasource and many more classes are available that provide these features like DataSourceUtils so please help me How to maintain connection pooling in springjdbc template that keeps connection open for multiple queries
ReplyDeleteThanks.
Regards
Piyush