Java uses the properties file to configure the spring data source, and uses spring's jdbctemplate's queryforlist to query data.

Source: Internet
Author: User

Jar package used:

Ojdbc14.jar spring-2.5.jar commons-dbcp-1.4.jar

Directory structure RESS (source folder)-> conff (Package) contains app. xml and SYS. Properties

SYS. properties:

jdbc.driverClassName=oracle.jdbc.driver.OracleDriverjdbc.url=jdbc:oracle:thin:@localhost:1521:orcljdbc.username=scottjdbc.password=tiger

App. 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" 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>

Oratest. Java

 
Package oracletest; import Java. util. iterator; import Java. util. list; import Java. util. map; import Java. util. map. entry; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. classpathxmlapplicationcontext; import Org. springframework. JDBC. core. jdbctemplate; public class oratest {private jdbctemplate myjdbctemplate; Private Static applicationcontext applicationconte XT; private string [] xmlclasspath = {"conff/app. XML "}; Public oratest () {applicationcontext = new classpathxmlapplicationcontext (xmlclasspath);} public static object getbean (string beanname) {return applicationcontext. getbean (beanname);} // getter setterpublic jdbctemplate getmyjdbctemplate () {return myjdbctemplate;} public void setmyjdbctemplate (jdbctemplate myjdbctemplate) {This. myjdbctemplate = myjdbctem Plate ;}@ suppresswarnings ("rawtypes") public static void main (string [] ARGs) {oratest dd = new oratest (); DD. getbean ("myjdbctemplate"); string SQL = "select pass, DD from T1 where pass =? "; DD. setmyjdbctemplate (jdbctemplate) dd. getbean ("myjdbctemplate"); List list = DD. getmyjdbctemplate (). queryforlist (SQL, new object [] {"VV"}); system. out. println (list. size (); // traverse listfor (INT I = 0; I <list. size (); I ++) {map Mm = (MAP) list. get (I); // traverse mapiterator it = mm. entryset (). iterator (); While (it. hasnext () {map. entry entry = (map. entry) it. next (); system. out. println ("key:" + entry. getkey () + "-- Value:" + entry. getvalue ());}}}}

Output:

-Refreshing
Org. springframework. context. support. classpathxmlapplicationcontext @ 19fcc69: Display name [Org. springframework. context. support. classpathxmlapplicationcontext @ 19fcc69]; Startup date [Mon Oct 17 16:54:57 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 @ 19fcc69]:

Org. springframework. Beans. Factory. Support. defaultlistablebeanfactory @ 5e5a50
-Loading Properties file from URL
-Pre-instantiating singletons in
Org. springframework. Beans. Factory. Support. defaultlistablebeanfactory @ 5e5a50: defining beans [propertyconfigurer, mydatasource, myjdbctemplate]; root of factory hierarchy
2
Key: pass -- Value: VV
Key: dd -- Value: 2011-10-10 16:48:39. 0
Key: pass -- Value: VV
Key: dd -- Value: NULL

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.