日期:2014-05-20  浏览次数:20726 次

新手用Java连接数据库 读取配置文件的内容怎么弄?

对于用spring的java来说应该很简单吧,
我现在要取用户,密码,连接地址。
在public-context.xml里面的配置,如蓝色部分;求代码,谢谢。

application-context配置文件如下:
<?xml version="1.0" encoding="gb2312"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 技术-->
<import resource="classpath:public-context.xml" />
</beans>


public-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
                 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                 ">

<bean id="dataSource"
class="com.atomikos.jdbc.AtomikosDataSourceBean"  destroy-method="close">
<property name="uniqueResourceName" value="OracleXADataSource" />
<property name="xaDataSourceClassName"
value="oracle.jdbc.xa.client.OracleXADataSource" />
<property name="poolSize" value="3" />
<property name="maxPoolSize" value="100" />
<property name="minPoolSize" value="1" />
<property name="xaProperties">
<props>
<prop key="user">hbaccount1107</prop>
<prop key="password">a</prop>
<prop key="URL">  jdbc:oracle:thin:@192.168.0.10:1521:orcl
</prop>
</props>
</property>
<property name="testQuery" value="select 1 from dual" />
</bean>
------最佳解决方案--------------------
ApplicationContext acx=new ClassPathXmlApplicationContext("public-context.xml");
AtomikosDataSourceBean db=acx.getBean("dataSource");
Properties props=db.getXaProperties();
String user_name=props.getProperty("user");
String url=props.getProperty("URL");
String passWord=props.getPassword("password");
------其他解决方案--------------------

把上面取到的值赋值给下面的, 怎么取上面的值呢?
String url = "";
String user_name = "";
String passWord = "";
------其他解决方案--------------------
引用:
ApplicationContext acx=new ClassPathXmlApplicationContext("public-context.xml");
AtomikosDataSourceBean db=acx.getBean("dataSource");
Properties props=db.getXaProperties();
String user……


非常的好,很给力。 就是有一点点美中不足的,你忘记类型转换。
AtomikosDataSourceBean db = (AtomikosDataSourceBean) acx
.getBean("dataSource");
------其他解决方案--------------------