天天看点

读取oracle的连接信息

12.1 新建一个数据库实体类Demo1
 package com.eduask.util;
 import java.sql.Connection;
 import java.sql.DriverManager;
 //jdbc
 public class Demo1 {
 private static String url;
 private static String user;
 private  static String pwd;
 private  static String driverClass;
 public Demo1(){
 
 }
 public static Connection getConn(){
 Connection conn=null;
 try {
 conn=DriverManager.getConnection(url,user,pwd);
 } catch (Exception e) {
 // TODO: handle exception
 }
 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);
 } catch (ClassNotFoundException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 this.driverClass = driverClass;
 }
 @Override
 public String toString() {
 System.out.println("url="+url);
 System.out.println("user="+user);
 System.out.println("pwd="+pwd);
 return "";
 }
 public Demo1(String url, String user, String pwd, String driverClass) {
 super();
 this.url = url;
 this.user = user;
 this.pwd = pwd;
 this.driverClass = driverClass;
 }
 }
 12.2 新建一个TestDemo1测试类;
 package com.eduask.util;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 public class TestDemo1 {
 public static void main(String[] args) {
 ClassPathXmlApplicationContext cx=new ClassPathXmlApplicationContext("util/demo1.xml");
 Demo1 demo1=(Demo1) cx.getBean("demo1");
 System.out.println(demo1);
 }
 }
 12.3新建一个demo1.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​​
         ">
 <bean id="demo1" class="com.eduask.util.Demo1"
 p:url="jdbc:oracle:thin:@localhost:1521/orcl"
 p:user="scott"
 p:pwd="tiger"
 p:driverClass="oracle.jdbc.driver.OracleDriver"
 />
 </beans>
 12.4 程序运行如下:
 url=jdbc:oracle:thin:@localhost:1521/orcl
 user=scott
 pwd=tiger