使用spring 配置数据源,并用数据源得到连接,操作sql
来源:互联网 发布:懒人听书软件 编辑:程序博客网 时间:2024/05/22 04:32
需要的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对象,则要用到DataSourceUtils
- Connection 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管理数据源连接池
- Java的一个堆栈例子
- N皇后问题
- 一种自适应的图像二值化
- laravel插件包 网站 国外的哦
- ConcurrentHashMap中rehash函数理解
- 使用spring 配置数据源,并用数据源得到连接,操作sql
- Ubuntu 复制文件到远端时错误,Permission denied 失败原因深度探索
- 如何给 textview添加类似于textfield的水印文字
- Redis 使用 Lua 实现 split 结合 HMGET 批量读取数据
- keil C51变量定义位置
- struts2整合spring应用实例
- hrbust 2287 Bridge【最长上升子序列变形+dp最小花费】好题
- hdu5572 简单几何
- struts集成spring官方例子