实现功能:
1,在XML文件里配置数据库连接(能修改数据库连接相关参数,如用户名等)
2,同时配置两个数据库连接
3,除连接数数据库参数外,其他参数的传送
参考资料:
xml 数据库连接信息 配置与读取(dom4j) - hzh的日志 - 网易博客
http://blog.163.com/woshihezhonghua@126/blog/static/1271436362010510253725/
具体代码:
1,sys-config.xml
需要的jar包:sqljdbc4.jar,ojdbc14.jar
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@111.111.1.11:1521/orcl
201
300
006
10
10
6
10
com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc:sqlserver://222.222.0.222:1433;DatabaseName=1111
b
b
2,将要传递的参数实例化
OrclJdbcInfo.java
package utils;
public class OrclJdbcInfo {
private String orcldriverName;
private String orclurl;
private String orclusername;
private String orclpassword;
private String orcldays;
private String orclmandt;
private String orclpasesize;
private String orclrefreshTime;
private String orclpageRefresh;
// 注解,能在编译期间发现错误
@Override
public String toString()
{
// TODO Auto-generated method stub
return this.getClass().getName() + "{orcldriverName=" + this.orcldriverName
+ ",orclurl=" + this.orclurl + ",orclusername=" + this.orclusername
+ ",orclpassword=" + this.orclpassword + ",orcldays="+this.orcldays
+",orclmandt="+this.orclmandt+",orclpagesize="+this.orclpasesize
+",orclrefreshtime="+this.orclrefreshTime+",orclpageRefresh="+this.orclpageRefresh+"}";
}
public String getOrclrefreshTime() {
return orclrefreshTime;
}
public void setOrclrefreshTime(String orclrefreshTime) {
this.orclrefreshTime = orclrefreshTime;
}
public String getOrclpageRefresh() {
return orclpageRefresh;
}
public void setOrclpageRefresh(String orclpageRefresh) {
this.orclpageRefresh = orclpageRefresh;
}
public String getOrclpasesize() {
return orclpasesize;
}
public void setOrclpasesize(String orclpasesize) {
this.orclpasesize = orclpasesize;
}
public String getOrcldriverName() {
return orcldriverName;
}
public void setOrcldriverName(String orcldriverName) {
this.orcldriverName = orcldriverName;
}
public String getOrclurl() {
return orclurl;
}
public void setOrclurl(String orclurl) {
this.orclurl = orclurl;
}
public String getOrclusername() {
return orclusername;
}
public void setOrclusername(String orclusername) {
this.orclusername = orclusername;
}
public String getOrclpassword() {
return orclpassword;
}
public void setOrclpassword(String orclpassword) {
this.orclpassword = orclpassword;
}
public String getOrcldays() {
return orcldays;
}
public void setOrcldays(String orcldays) {
this.orcldays = orcldays;
}
public String getOrclmandt() {
return orclmandt;
}
public void setOrclmandt(String orclmandt) {
this.orclmandt = orclmandt;
}
}
JdbcInfo.java
package utils;
public class JdbcInfo {
private String driverName;
private String url;
private String username;
private String password;
// 注解,能在编译期间发现错误
@Override
public String toString() {
// TODO Auto-generated method stub
return this.getClass().getName() + "{driverName=" + this.driverName
+ ",url=" + this.url + ",username=" + this.username
+ ",password=" + this.password + "}";
}
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
ConfigReader.java
需要的jar包:dom4j-1.6.1.jar
package utils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/*
* 讀取xml配置文件sys-config.xml
* 採用單例模式
*/
public class ConfigReader {
private static ConfigReader instance = new ConfigReader();
private Document doc;
private JdbcInfo jdbcInfo;
private OrclJdbcInfo orclJdbcInfo;
private ConfigReader() {
try {
doc = new SAXReader().read(Thread.currentThread() .getContextClassLoader().getResourceAsStream("sys-config.xml"));
jdbcInfo = new JdbcInfo();
orclJdbcInfo = new OrclJdbcInfo();
Element driverNameElt = (Element)doc.selectObject("/config/db-info/driver-name");
Element urlElt = (Element)doc.selectObject("/config/db-info/url");
Element usernameElt = (Element)doc.selectObject("/config/db-info/username");
Element passwordElt = (Element)doc.selectObject("/config/db-info/password");
jdbcInfo.setDriverName(driverNameElt.getStringValue());
jdbcInfo.setUrl(urlElt.getStringValue());
jdbcInfo.setUsername(usernameElt.getStringValue());
jdbcInfo.setPassword(passwordElt.getStringValue());
Element orcldriverNameElt = (Element)doc.selectObject("/config/db-info/orcldriver-name");
Element orclurlElt = (Element)doc.selectObject("/config/db-info/orclurl");
Element orclusernameElt = (Element)doc.selectObject("/config/db-info/orclusername");
Element orclpasswordElt = (Element)doc.selectObject("/config/db-info/orclpassword");
Element orcldaysElt = (Element)doc.selectObject("/config/db-info/orcldays");
Element orclmandtElt = (Element)doc.selectObject("/config/db-info/orclmandt");
Element orclpasesizeElt = (Element)doc.selectObject("/config/db-info/orclpasesize");
Element orclrefreshTimeElt = (Element)doc.selectObject("/config/db-info/orclrefreshTime");
Element orclpageRefreshElt = (Element)doc.selectObject("/config/db-info/orclpageRefresh");
orclJdbcInfo.setOrcldriverName(orcldriverNameElt.getStringValue());
orclJdbcInfo.setOrclurl(orclurlElt.getStringValue());
orclJdbcInfo.setOrclusername(orclusernameElt.getStringValue());
orclJdbcInfo.setOrclpassword(orclpasswordElt.getStringValue());
orclJdbcInfo.setOrcldays(orcldaysElt.getStringValue());
orclJdbcInfo.setOrclmandt(orclmandtElt.getStringValue());
orclJdbcInfo.setOrclpasesize(orclpasesizeElt.getStringValue());
orclJdbcInfo.setOrclrefreshTime(orclrefreshTimeElt.getStringValue());
orclJdbcInfo.setOrclpageRefresh(orclpageRefreshElt.getStringValue());
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static ConfigReader getInstance() {
return instance;
}
public JdbcInfo getjdbcInfo(){
return jdbcInfo;
}
public OrclJdbcInfo getorclJdbcInfo(){
return orclJdbcInfo;
}
//用於測試是否成功讀取數據
public static void main(String[] args) {
System.out.println(ConfigReader.getInstance().getjdbcInfo());
System.out.println(ConfigReader.getInstance().getorclJdbcInfo());
}
}
4,实现类
DBUtil.java
package utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServlet;
import entity.Sap;
/**
* 数据库工具类
*
* @author Administrator
*
*/
public class DBUtil extends HttpServlet{
/**
* 取得数据库连接
*
* @return
*/
//map方法封装值
public static Map getConnection() {
Map map = new HashMap() ;
Connection orclConn = null;
Connection conn = null;
try {
JdbcInfo jdbcInfo = ConfigReader.getInstance().getjdbcInfo();
Class.forName(jdbcInfo.getDriverName());
conn = DriverManager.getConnection(jdbcInfo.getUrl(), jdbcInfo.getUsername(), jdbcInfo.getPassword());
OrclJdbcInfo orclJdbcInfo = ConfigReader.getInstance().getorclJdbcInfo();
Class.forName(orclJdbcInfo.getOrcldriverName());
orclConn = DriverManager.getConnection(orclJdbcInfo.getOrclurl(), orclJdbcInfo.getOrclusername(), orclJdbcInfo.getOrclpassword());
map.put("orcldays",orclJdbcInfo.getOrcldays());
map.put("orclmandt",orclJdbcInfo.getOrclmandt());
map.put("orclpasesize",orclJdbcInfo.getOrclpasesize());
map.put("orclrefreshTime",orclJdbcInfo.getOrclrefreshTime());
map.put("orclpageRefresh",orclJdbcInfo.getOrclpageRefresh());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
map.put("orclConn", orclConn);
map.put("conn", conn);
return map;
}
public static void close(PreparedStatement pstmt) {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void commit(Connection conn) {
if (conn != null) {
try {
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void rollback(Connection conn) {
if (conn != null) {
try {
conn.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void setAutoCommit(Connection conn, boolean autoCommit) {
if (conn != null) {
try {
conn.setAutoCommit(autoCommit);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//测试是否连接成功
public static void main(String[] args) {
DBUtil.getConnection();
System.out.println("---------ok---------");
}
}