`
lushuaiyin
  • 浏览: 677310 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用spring 配置数据源,并用数据源得到连接,操作sql

 
阅读更多

需要的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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics