使用spring 配置数据源,并用数据源得到连接,操作sql
来源:互联网 发布:cda数据分析师招聘 编辑:程序博客网 时间:2024/05/01 05:04
需要的jar包:
spring-2.5.jar ojdbc14.jar commons-pool-1.3.jar commons-dbcp-1.4.jar
sys.properties文件:
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=scott
jdbc.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>
Ora2.java文件:
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 dataSource;private static ApplicationContext 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 dataSource) {this.dataSource = dataSource;}@SuppressWarnings("rawtypes")public static void main(String[] args) {Ora2 dd=new Ora2();dd.setDataSource((DataSource)dd.getBean("mydataSource"));//如果用数据源获取Connection对象,则要用到DataSourceUtilsConnection con=DataSourceUtils.getConnection(dd.getDataSource());try {Statement stmt=con.createStatement();String sql="select pass,dd from t1 where pass='vv'";ResultSet res=stmt.executeQuery(sql);//遍历结果集while(res.next()){System.out.println(res.getString("PASS")+"---"+res.getString("DD")); }//关闭连接if((con!=null)||(!con.isClosed())){con.close();con=null;}if((stmt!=null)||(!stmt.isClosed())){stmt.close();stmt=null;}} catch (SQLException e) {e.printStackTrace();}}}
//输出结果:
- 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 [file:/D:/work/des/bin/conff/sys.properties]
- 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
- 使用spring 配置数据源,并用数据源得到连接,操作sql
- 使用spring 配置数据源,并用数据源得到连接,操作sql
- spring配置数据源连接池
- spring配置数据源连接池
- SQL server 配置连接数据源
- mybatis使用spring-druid数据源连接池配置log4j打印sql语句以及开启监控平台
- 使用spring配置jndi数据源
- spring 使用 c3p0 配置数据源
- spring中使用proxool作数据源连接池的配置
- spring中使用proxool作数据源连接池的配置
- 使用 JNDI 得到数据源
- Spring远程数据源JTA事务连接配置
- 搭建spring工程配置数据源连接池
- spring配置hibernate 数据源(数据连接池)
- spring 配置Spring数据源
- 使用Spring管理数据源连接池
- 使用Spring管理数据源连接池
- 使用Spring管理数据源连接池
- ASP.NET跨页面传值技巧总结
- 正义战胜邪恶
- 包含arxHeader时定义CMESH引发的错误
- HTML5 LocalStorage 本地存储
- 跨平台库--PWLib入门
- 使用spring 配置数据源,并用数据源得到连接,操作sql
- onInterceptTouchEvent和onTouchEvent调用时序
- 丹尼斯·里奇,那个给乔布斯提供肩膀的巨人
- 26个Jquery使用小技巧(jQuery tips, tricks & solutions)
- Tablespace PSAPSR3 is 100% used
- 这个学期的主要任务
- 射频识别技术:RFID 您了解不?
- Unix限制重复IP登录服务器的脚本。
- 在资源文件中加入声音资源