使用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文件:

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:aop="http://www.springframework.org/schema/aop"  
  5.        xmlns:tx="http://www.springframework.org/schema/tx"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  7.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
  8.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"  
  9.        default-autowire="byName" default-lazy-init="true">  
  10.   
  11.     <!-- 属性文件读入 -->  
  12.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  13.         <property name="locations">  
  14.             <list>  
  15.                 <value>classpath*:conff/sys.properties</value>  
  16.             </list>  
  17.         </property>  
  18.     </bean>  
  19.     <!--  
  20.     <bean id="mydataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  21.         <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>  
  22.         <property name="url"  
  23.           value="jdbc:oracle:thin:@localhost:1521:ORCL">  
  24.         </property>  
  25.         <property name="username" value="scott"></property>  
  26.         <property name="password" value="tiger"></property>  
  27.     </bean>  
  28.       -->  
  29.      <!--  -->  
  30.     <bean id="mydataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  31.         <property name="driverClassName"><value>${jdbc.driverClassName}</value></property>  
  32.         <property name="url"><value>${jdbc.url}</value></property>  
  33.         <property name="username"><value>${jdbc.username}</value></property>  
  34.         <property name="password"><value>${jdbc.password}</value></property>  
  35.     </bean>  
  36.       
  37.     <bean id="myJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
  38.         <property name="dataSource">  
  39.             <ref bean="mydataSource"/>  
  40.         </property>  
  41.     </bean>  
  42.   
  43. </beans>  


Ora2.java文件:

[java] view plaincopy
  1. package oracletest;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.ResultSet;  
  5. import java.sql.SQLException;  
  6. import java.sql.Statement;  
  7.   
  8. import javax.sql.DataSource;  
  9.   
  10. import org.springframework.context.ApplicationContext;  
  11. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  12. import org.springframework.jdbc.datasource.DataSourceUtils;  
  13.   
  14. public class Ora2 {  
  15.       
  16.     private DataSource dataSource;  
  17.     private static ApplicationContext applicationContext;  
  18.     private String[] xmlClassPath={  
  19.             "conff/app.xml"  
  20.     };  
  21.       
  22.     public Ora2(){  
  23.         applicationContext=new ClassPathXmlApplicationContext(xmlClassPath);  
  24.     }  
  25.     public static Object getBean(String beanName){    
  26.           
  27.         return  applicationContext.getBean(beanName);  
  28.     }  
  29.       
  30.     public DataSource getDataSource() {  
  31.         return dataSource;  
  32.     }  
  33.     public void setDataSource(DataSource dataSource) {  
  34.         this.dataSource = dataSource;  
  35.     }  
  36.     @SuppressWarnings("rawtypes")  
  37.     public static void main(String[] args) {  
  38.         Ora2 dd=new Ora2();  
  39.         dd.setDataSource((DataSource)dd.getBean("mydataSource"));  
  40.         //如果用数据源获取Connection对象,则要用到DataSourceUtils  
  41.         Connection con=DataSourceUtils.getConnection(dd.getDataSource());  
  42.         try {  
  43.             Statement stmt=con.createStatement();  
  44.             String sql="select pass,dd  from t1 where pass='vv'";  
  45.             ResultSet res=stmt.executeQuery(sql);  
  46.               
  47.             //遍历结果集  
  48.             while(res.next()){  
  49.                 System.out.println(res.getString("PASS")+"---"+res.getString("DD"));  
  50.             }  
  51.               
  52.             //关闭连接  
  53.             if((con!=null)||(!con.isClosed())){  
  54.                 con.close();  
  55.                 con=null;  
  56.             }  
  57.             if((stmt!=null)||(!stmt.isClosed())){  
  58.                 stmt.close();  
  59.                 stmt=null;  
  60.             }  
  61.               
  62.         } catch (SQLException e) {  
  63.             e.printStackTrace();  
  64.         }  
  65.           
  66.   
  67.     }  
  68.   
  69. }  


//输出结果:

- 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

0 0
原创粉丝点击