Jar package:
Spring-2.5.jar ojdbc14.jar commons-pool-1.3.jar commons-dbcp-1.4.jar
SYS. properties file:
JDBC. driverclassname = oracle. JDBC. Driver. oracledriver
JDBC. url = JDBC: oracle: thin :@ localhost: 1521: orcl
JDBC. Username = Scott
JDBC. Password = Tiger
App. xml file:
<? 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" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http: // www. Springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd "default-autowire =" byname "default-Lazy-init =" true "> <! -- Read attribute file --> <bean id = "propertyconfigurer" class = "org. springframework. beans. factory. config. propertyplaceholderpolicer "> <property name =" locations "> <list> <value> classpath *: conff/sys. properties </value> </List> </property> </bean> <! -- <Bean id = "mydatasource" class = "org. apache. commons. DBCP. basicdatasource "> <property name =" driverclassname "value =" oracle. JDBC. driver. oracledriver "> </property> <property name =" url "value =" JDBC: oracle: thin :@ localhost: 1521: orcl "> </property> <property name =" username "value =" Scott "> </property> <property name =" password "value =" tiger "> </Property> </bean> --> <! --> <Bean id = "mydatasource" class = "org. apache. commons. DBCP. basicdatasource "Destroy-method =" close "> <property name =" driverclassname "> <value >$ {JDBC. driverclassname} </value> </property> <property name = "url"> <value >$ {JDBC. URL} </value> </property> <property name = "username"> <value >$ {JDBC. username} </value> </property> <property name = "password"> <value >$ {JDBC. password} </value> </property> </bean> <bean id = "myjdbctemplate" class = "org. springframework. JDBC. core. jdbctemplate "> <property name =" datasource "> <ref bean =" mydatasource "/> </property> </bean> </beans>
Ora2.java file:
Package oracletest; import Java. SQL. connection; import Java. SQL. resultset; import Java. SQL. sqlexception; import Java. SQL. statement; import javax. SQL. datasource; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. classpathxmlapplicationcontext; import Org. springframework. JDBC. datasource. datasourceutils; public class ora2 {private datasource; private St Atic applicationcontext; private string [] xmlclasspath = {"conff/app. XML "}; Public ora2 () {applicationcontext = new classpathxmlapplicationcontext (xmlclasspath);} public static object getbean (string beanname) {return applicationcontext. getbean (beanname);} public datasource getdatasource () {return datasource;} public void setdatasource (datasource) {This. datasource = datasource;} @ Suppresswarnings ("rawtypes") public static void main (string [] ARGs) {ora2 dd = new ora2 (); DD. setdatasource (datasource) dd. getbean ("mydatasource"); // if you use the data source to obtain the connection object, you need to use datasourceutilsconnection con = datasourceutils. getconnection (DD. getdatasource (); try {statement stmt = con. createstatement (); string SQL = "select pass, DD from T1 where pass = 'vv '"; resultset res=stmt.exe cutequery (SQL); // traverses the result set while (Res. Next () {system. out. println (res. getstring ("pass") + "---" + Res. getstring ("DD");} // close the connection if (con! = NULL) | (! Con. isclosed () {con. Close (); con = NULL;} If (stmt! = NULL) | (! Stmt. isclosed () {stmt. Close (); stmt = NULL ;}} catch (sqlexception e) {e. printstacktrace ();}}}
// Output result:
-Refreshing
Org. springframework. context. support. classpathxmlapplicationcontext @ 3e86d0: Display name [Org. springframework. context. support. classpathxmlapplicationcontext @ 3e86d0]; Startup date [Tue Oct 18 11:04:30 CST 2011]; root of context hierarchy
-Loading XML bean definitions from class path resource [conff/APP. xml]
-Bean Factory for application context [org. springframework. Context. Support. classpathxmlapplicationcontext @ 3e86d0]:
Org. springframework. Beans. Factory. Support. defaultlistablebeanfactory @ 191d8c1
-Loading Properties file from URL
-Pre-instantiating singletons in
Org. springframework. Beans. Factory. Support. defaultlistablebeanfactory @ 191d8c1: defining beans [propertyconfigurer, mydatasource, myjdbctemplate]; root of factory hierarchy
VV --- 2011-10-10 16:48:39. 0
VV --- null