从config文件中读取数据库信息

13.1新建一个数据库实体类;

package com.eduask.util;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

//数据库的实体类;

public class Demo2 {

private static String url;

private static String user;

private static String pwd;

private static String driverClass;

public static Connection getconn(){

Connection conn=null;

try {

conn=DriverManager.getConnection(urluserpwd);

catch (SQLException e) {

System.out.println(e.getMessage());

}

return conn;

}

public String getUrl() {

return url;

}

public void setUrl(String url) {

this.url = url;

}

public String getUser() {

return user;

}

public void setUser(String user) {

this.user = user;

}

public String getPwd() {

return pwd;

}

public void setPwd(String pwd) {

this.pwd = pwd;

}

public String getDriverClass() {

return driverClass;

}

public void setDriverClass(String driverClass) {

try {

Class.forName(driverClass);

this.driverClass = driverClass;

catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

@Override

public String toString() {

return "Demo2 \n[url=" + url + "]\n[user=" + user + "]\n[pwd=" + pwd

+ "]\n[driverClass=" + driverClass + "]";

}

public Demo2(String url, String user, String pwd, String driverClass) {

super();

this.url = url;

this.user = user;

this.pwd = pwd;

this.driverClass = driverClass;

}

public Demo2() {

super();

// TODO Auto-generated constructor stub

}

}

13.2 新建一个TestDemo1的测试类;

package com.eduask.util;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDemo2 {

public static void main(String[] args) {

ClassPathXmlApplicationContext cx=new ClassPathXmlApplicationContext("util/demo2.xml");

Demo2 demo2=(Demo2) cx.getBean("demo2");

System.out.println(demo2);

}

}

13.3 新建一个config的源文件下的db.properties

url=jdbc:oracle:thin:@localhost:1521/orcl

user=scott

pwd=tiger

driverClass=oracle.jdbc.driver.OracleDriver

13.4 新建一个demo2.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:p="http://www.springframework.org/schema/p"  xmlns:util="http://www.springframework.org/schema/util"    xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/util    http://www.springframework.org/schema/util/spring-util-4.0.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

        ">

<context:property-placeholder location="classpath:db.properties"/>

<bean id="demo2" class="com.eduask.util.Demo2"

p:url="${url}"

p:user="${user}"

p:pwd="${pwd}"

p:driverClass="${driverClass}"

/>

</beans>

13.5 程序运行如下:

Demo2
[url=jdbc:oracle:thin:@localhost:1521/orcl]
[user=scott]
[pwd=tiger]
[driverClass=oracle.jdbc.driver.OracleDriver]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值