java 用properties檔案配置spring資料來源,用spring的JdbcTemplate的queryForList查資料

來源:互聯網
上載者:User

使用的jar包:

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

目錄結構ress(source folder)->conff(package)下有app.xml和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"><!-- 屬性檔案讀入 --><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><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 applicationContext;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 = myJdbcTemplate;}@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());//遍曆listfor(int i=0;i<list.size();i++){Map mm=(Map)list.get(i);//遍曆mapIterator it= mm.entrySet().iterator();while(it.hasNext()){Map.Entry entry=(Map.Entry)it.next();System.out.println("key:"+entry.getKey()+"--value:"+entry.getValue());}}}}

輸出:

- 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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.