使用spring 配置数据源,并用数据源得到连接,操作sql
2011-10-18 11:02
621 查看
需要的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文件:
Ora2.java文件:
//输出结果:
- 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-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 Boot通过Mybatis,使用mapper接口和xml配置sql,连接数据库
- mybatis使用spring-druid数据源连接池配置log4j打印sql语句以及开启监控平台
- Spring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactory
- spring使用JdbcTemplate、JdbcDaoSupport模板类的数据源配置及db访问
- 使用Spring的JndiObjectFactoryBean配置数据源时需要注意的问题
- PL/SQL Developer结合oracle精简客户端使用配置错误代码ORA-12154: TNS: 无法解析指定的连接标识符
- 使用spring连接及操作mongodb3.0
- Spring多数据源的配置和使用
- [置顶] 在Springboot上使用jedis来操作缓存redis --jedis的配置
- Spring3.0配置多个事务管理器(即操作多个数据源)的方法
- Spring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactory(转)
- mybatis使用拦截器显示sql,使用druid配置连接信息
- Spring多数据源的配置和使用
- java 基于springboot使用ssh(spring + springmvc + hibernate)分库配置多数据源方式
- 使用spring配置jndi数据源
- spring 配置sqlserver数据源-使用集成身份登陆
- SpringBoot使用Druid数据源的配置方法
- spring 使用tomcat自带的dbcp数据源,如何查找配置参数
- Spring 多数据源连接配置 解决方案