Magazine
 
Spring Framework
 

<property name=”password”>
<value>root</value>
</property></bean>
<bean id=”datacon”
class=”dataconimpl”>
<property name=”dataSource”>
<ref local=”dataSource”/>
</property>
</bean>
</beans>

4. D:\springdemo\helloclient.java

import java.io.*;
import javax.sql.*;
import java.sql.*;
import java.util.*;
importorg.springframework.
beans.factory.*;
import org.springframework.beans.
factory.xml.*;
import org.springframework.core.io.*;
import
org.springframework.core.io.
ClassPath Resource;
import org.springframework.
jdbc.core.*;
import
org.springframework.
jdbc.datasource.*;
import org.springframework.jdbc.object.*;
import
org.springframework.jdbc.support.*;
public class helloclient {
public static void main(String args[])
throws Exception{
try {
String a=”Amit”;
String b=”Dehradun”;
System.out.println(“Wait...”);
Resource res = new
ClassPathResource(“datacon.xml”);
System.out.println(“Please Wait...”);
BeanFactory factory = new
XmlBeanFactory(res);
datacon bean1 =
(datacon)factory.getBean(“datacon”);
DataSource ds=bean1.dbcon();
JdbcTemplate jt = new
JdbcTemplate(ds);
jt.execute(“insert into table1 values(‘“+a +”’,’”+ b+”’) “);
System.out.println(“Record Added”);
}
catch(Exception e1) {
System.out.println(“”+e1);
}
}
}

  Now, execute the programs:

D:\springdemo>javac datacon.java
D:\springdemo>javac dataconimpl.java
D:\springdemo>javac helloclient.java
D:\springdemo>java helloclient

We will get the output as follows:

Wait... Please Wait...Aug 27, 2007 4:35:00 PM org.springframework.core
.CollectionFactory <clinit>INFO: JDK 1.4+ collections
availableAug 27, 2007 4:35:01 PM org.springframework.
beans.factory.xml. XmlBeanDefinitionReader

loadBeanDefinitionINFO:
Loading XML bean
definitions from class path resource
[datacon.xml]Aug 27,
2007 4:35:01 PM
org.springframework.
jdbc.datasource.Driver
ManagerDataSource
setDriverClassNameINFO:
Loaded JDBC driver: com.mysql.jdbc.DriverRecord
Added{name=Amit, place=Dehradun
}

Spring also 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.

Sept 2007 | Java Jazz Up | 57
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,   Download PDF