PART-III
(PUBLISHED IN DEVELOPER IQ - September2005)
Now we will move on to the main process of any enterprise application: Data Persistence. For this we have to initialize our data access framework, manage resources, handle various exceptions and if anything goes wrong, we must roll-back so as to save the existing data.
Spring comes with a family of data access frameworks that integrates well will variety of data access technologies like JDBC, Java Data Objects and Object Relational Mapping (ORM) tools like Hibernate, OJB, iBatis etc.,
Many J2EE application servers and even web servers provide a 'dataSource' via Jndi name. To configure the spring bean with the Jndi name of our 'dataSource' and use its connection pooling facility 'JndiObjectFactoryBean' is used. When a DataSource is not present, we need a connection pooling bean that implements 'dataSource'. For this purpose we use 'dbcp.BasicDataSource' is used. By using this we can have a 'dataSource' with connection pooling independent of application server.
To perform unit-tests in our data access code, spring comes with a very lightweight 'dataSource' implementation class: 'DriverManagerDataSource'. This class can be easily configured for unit tests as,
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
These properties can be configured in the spring configuration file also.
----------------------------------------------
Spring comes with its own data access framework. Spring separates the fixed and variant parts of data access process into two distinct classes : template and callback. Template manages the fixed part of our framework like data connection, managing resources, controlling transaction etc., while the Callback defines the things that are specific to our application like creating statements, binding parameters etc.,
The template class of Spring is 'JdbcTemplate'. A 'dataSource' is provided inside JdbcTemplate.
An example of database connection using 'JdbcTemplate' is shown below. Here we are using 'MySql' database. The MySql database can be downloaded from http://www.mysql.com. Download mysql4.1 and MyODBC-3.51 (ODBC Connector) install these in the hard disk. For Mysql give a username('root') and a password ('sql').
Then start the 'My Sql Console Line Client' from programs and type the password.
The prompt will be changed to mysql,
mysql> show databases;
Two databases will be present default: mysql and test.
mysql> use test;
We will get message as 'Database changed'. Next create table in test database as follows
mysql> create table table1(name text, place text);
We will get the message 'Query OK, 0 rows affected'. Now we have created a table in mysql database, set the path and classpath as before and edit the program
----- f:\sprindemo\datacon.java
import javax.sql.*;
public interface datacon
{
public DataSource dbcon();
}
----------------------------------------
f:\sprindemo\dataconimpl.java
import org.springframework.jdbc.core.*;
import org.springframework.jdbc.datasource.*;
import org.springframework.jdbc.object.*;
import org.springframework.jdbc.support.*;
import javax.sql.*;
public class dataconimpl implements datacon
{
private DataSource dataSource;
public void setDataSource(DataSource ds)
{
dataSource = ds;
}
public DataSource dbcon()
{
return dataSource;
}
}
----------------------------------------
f:\sprindemo\datacon.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>sun.jdbc.odbc.JdbcOdbcDriver</value>
</property>
<property name="url">
<value>jdbc:odbc:test</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>sql</value>
</property>
</bean>
<bean id="datacon" class="dataconimpl">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
</beans>
----------------------------------------
f:\sprindemo\springservlet.java
import java.io.*;
import javax.sql.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.*;
import org.springframework.core.io.*;
import org.springframework.jdbc.core.*;
import org.springframework.jdbc.datasource.*;
import org.springframework.jdbc.object.*;
import org.springframework.jdbc.support.*;
public class springservlet extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException
{
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String a = req.getParameter("text1");
String b = req.getParameter("text2");
String c = req.getParameter("combo1");
String d = req.getParameter("combo2");
try
{
System.out.println("Wait...");
Resource res = new ClassPathResource("datacon.xml");
BeanFactory factory = new XmlBeanFactory(res);
datacon bean1 = (datacon)factory.getBean("datacon");
DataSource ds=bean1.dbcon();
if(d.equals("add"))
{
JdbcTemplate jt = new JdbcTemplate(ds);
jt.execute("insert into table1 values('"+a+"','"+b+"') ");
out.println("Record Added");
}
if(d.equals("delete"))
{
JdbcTemplate jt = new JdbcTemplate(ds);
jt.execute("delete from table1 where name='"+a+"' ");
out.println("Record Deleted");
}
if(d.equals("find"))
{
List list1;
JdbcTemplate jt = new JdbcTemplate(ds);
list1=jt.queryForList("select * from table1 where name='"+a+"'");
Iterator i=list1.iterator();
while(i.hasNext())
{
Object ob = i.next();
out.println(ob.toString());
}
}
if(d.equals("update"))
{
if(c.equals("name"))
{
JdbcTemplate jt = new JdbcTemplate(ds);
jt.execute("update table1 set table1.place='"+b+"'where
table1.name='"+a+"' ");
}
if(c.equals("place"))
{
JdbcTemplate jt = new JdbcTemplate(ds);
jt.execute("update table1 set table1.name='"+a+"'where
table1.place='"+b+"' ");
}
out.println("Record Updated");
}
}
catch(Exception e1)
{System.out.println(""+e1);}
}
}
----------------------------------------
f:\sprindemo\springservlet.htm
<html>
<body bgcolor="pink">
<form method=post
action="http://localhost:8080/
servlet/springservlet">
Name : <input type=text name="text1"> <br><br>
Place : <input type=text name="text2"> <br><br>
Criterion :
<select name="combo1" size=1>
<option value="name">Name
<option value="place">Place
</select> <br><br>
<select name="combo2" size=1>
<option value="add">Add
<option value="delete">Remove
<option value="find">Find
<option value="update">Update
</select> <br><br>
<input type=submit>
</form>
</body>
</html>
The deployment procedure is same as before compile the all the files datacon.java, dataconimpl.java and springservlet.java and copy all the class files and the xml file datacon.xml to g:\tomcat5\webapps\root\web-inf\classes. Copy the html file to g:\tomcat5\webapps\root. Add entry to web.xml file.
Start Tomcat server and open browser
and type url as http://localhost:8080/ servletclient.htm. We will get a two textboxes, two comboboxes and a 'submit' button. Type name and place in textboxes, select 'add' from combobox and click 'submit' button. We will get message as 'Record Added'
---------------------------------------- Spring provides integration for many of the ORM frameworks like Hibernate, JDO, Apache OJB and iBATIS SQL Maps.
For mapping the hibernate resources, an instance of 'SessionFactory' is needed, 'LocalSessionFactoryBean' is used for this purpose and its properties 'hibernateProperties', 'mappingResources' and 'mappingDirectoryLocation' are set. Like Spring's DAO framework, here we have 'HibernateTemplate' to create an object of 'SessionFactory'. To access the data with 'HibernateTemplate' 'execute(HibernateCallback)' method is used.
Similar to the 'SessionFactory' of hibernate, JDO has 'PersistenceManager Factory'. It can be configured by using 'LocalPersistenceManagerFactoryBean'. Also 'JDOTemplate' to create an object of 'PersistenceManagerFactory'. To access the data with 'JDOTemplate' 'execute(JDOCallback)' method is used.
For iBATIS, we have to configure a 'SQLMapClient' by using 'SQLMapClientFactoryBean' and its properties 'configLocation' and 'dataSource' are set. Here also we have 'SQLMapClientTemplate'.
To access the data 'execute(SQLMapClientCallback)' method is used.
The only property that we need to change to integrate Spring with OJB is 'ConnectionFactoryClass' and it is done by using 'LocalDataSourceConnectionFactory'.
In the next article we shall see how to use a RMI service in Spring and how to export any Spring managed bean as RMI.
---------------------------------------------
|
相关推荐
Hitting the database with Spring and JDBC 10.1. Learning Spring’s data-access philosophy 10.1.1. Getting to know Spring’s data-access exception hierarchy 10.1.2. Templating data access 10.2. ...
153 5 ■ Hitting the database 155 6 ■ Managing transactions 220 7 ■ Securing Spring 247 8 ■ Spring and POJO-based remote services 305 9 ■ Building contract-first web services in ...
- **Hitting the Database**:这部分内容主要讲述了如何使用Spring来访问数据库。Spring提供了多种数据库访问的支持,包括JDBC模板、ORM框架集成(如Hibernate、MyBatis)等。 - **事务管理**:Spring提供了强大的...
- 例如,在第4章《Hitting the database》中介绍了如何利用Spring框架进行数据库操作;第5章《Managing transactions》则深入探讨了Spring提供的事务管理机制;第6章《Remoting》讲解了如何使用Spring进行远程调用...
一类基因表达式程序设计的首达最优解期望时间,杜欣,丁立新,本文研究了一类基于精英保留策略的基因表达式程序设计方法(Gene Expression Programming with maintaining elitist strategy,简称ME-GEP)的首达最优...
2. 首中时(Hitting Time):首中时是指随机过程首次达到某一特定状态的时间。在Cox风险过程的背景下,这可以是保险公司的资本首次耗尽,即破产的时间。研究首中时对于评估保险公司的风险和稳定性至关重要。 3. 末...
安卓小游戏,Android系统在4.2以上... Hitting the stars and dogs and cats may get higher score. After clearing the obstacles, player can throw the yellow package into the small house, then game is passed.
12. hitting 13. running 14. setting 15. sitting 16. spitting 17. stopping 18. swimming 19. begging 20. dropping 21. fitting 22. nodding 23. digging 24. forgetting 25. travelling **补充题目**: 用单词...
4. hit (过去式/过去分词) - hit, (现在分词) - hitting 5. press (n.) - pressure 6. climb (n.) - climber 7. accident (adj.) - accidental 8. knife (复数) - knives 9. importance (adj.) - important, (反义词...
Chapter 4 Hitting the Bullseye First Time Around Chapter 5 Data Stimuli for a Better World Chapter 6 Data Analytics Is the Society Chapter 7 Wanted: Thousands of Sherlock Holmes Clones Chapter 8 The...
So source code is always available for your libraries, and you can go straight to the definition of any code, either from the context menu, or by hitting F12 Decompile any referenced ...
标题和描述均提到了“Analysis of hitting the target vertically kill probability for the television homing UCAV”,这指向了一项专门针对电视制导无人攻击机(UCAV)垂直击中目标并造成致命打击概率的分析。...
Bugfix - TJamFileList: Fixed delay in lists with many items when hitting 'End' key to jump to the end of a list. Bugfix - TJamFileList: Fixed problem that occurred when deleting many files at once. ...
13. 考察非谓语动词作定语,hit与car之间为主动关系,用现在分词hitting表示主动,正确答案是B. hitting。 14. one of + 复数名词,表示“……之一”,正确答案是C. classmates。 15. 此为题目的第15题,由于部分...
Davis, G. Educational psychology: Theory and practice. Reading, MA: Addison-Wesley, 1983, 670 pp., 21.95 212 Book Reviews ... Hitting an erratically moving target takes as much luck as skill; e
19. **压缩 (Hitting)**:使物体缩短的力。 20. **剪切 (Shear)**:平行于物体表面的剪切力。 21. **拉孔 (Broaching)**:一种精密加工孔的工艺,通过多刃刀具的连续切削完成。 22. **装配 (Assembling)**:将多...
Davis, G. Educational psychology: Theory and practice. Reading, MA: Addison-Wesley, 1983, 670 pp., 21.95 212 Book Reviews ... Hitting an erratically moving target takes as much luck as skill; e
做某事,所以用"from hitting"。 14. 第一个空格用定冠词"the"表示特指,第二个空格用不定冠词"a"表示泛指。 15. "the taller of the two"表示两者中较高的那一个,"the tallest in our class"表示在班级中最高的...