java 用properties文件配置spring数据源,用spring的JdbcTemplate的queryForList查数据
来源:互联网 发布:mac如何下载office 编辑:程序博客网 时间:2024/05/21 07:10
使用的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 [file:/D:/work/des/bin/conff/sys.properties]
- 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
- java 用properties文件配置spring数据源,用spring的JdbcTemplate的queryForList查数据
- java 用properties文件配置spring数据源,用spring的JdbcTemplate的queryForList查数据
- jdbcTemplate的queryForList
- jdbcTemplate的queryForList
- jdbcTemplate的queryForList
- spring中通过properties文件配置数据源
- 关于mysql 数据库使用spring 2.0低版本 JdbcTemplate.queryForList 查询结果别名不起作用的解决方法
- Spring JdbcTemplate完成数据的增删改查
- 用spring的@PropertySouce配置数据源
- Spring的数据源配置
- AAA java向xml文件传值是通过属性properties进行的 spring + mybatis 动态切换数据源
- spring jdbctemplate的增删改查
- spring jdbctemplate的增删改查sample
- Spring - 资源文件properties的配置
- Spring 资源文件 properties 的配置
- Spring - 资源文件properties的配置
- Spring - 资源文件properties的配置
- Spring - 资源文件properties的配置
- LOCAL_SDK_VERSION and HISTORICAL_SDK_VERSIONS_ROOT
- 网站性能越来越差怎么办?
- ModelSim PE, DE, SE, XE
- (转)php排列组合 1到9数字相加都等于20
- 外壳生意那些事儿:苹果iPhone 4S坑爹啊!
- java 用properties文件配置spring数据源,用spring的JdbcTemplate的queryForList查数据
- Code::Blocks集成QT的方法
- 编译android2.3需要jdk1.6
- jquery使用div实现滚动条效果
- 项目记录-因为类增加了一个成员变量而导致的access violation
- CodeBlocks + MinGW + wxWidgets 搭建开发平台
- eclipse中找不到Software Updates…怎么办??
- 10月4日,iPhone 4S发布。第二天,山寨厂商机体抱头痛哭,尼玛我都做出了iphone5,坑爹啊!
- 如何面试Web前端开发工程师