XML配置数据库文件的连接其实是个很简单的问题,为什么到现在才写出来主要是昨天在网上看了别人写的,然后一直陷入其中,最后发现不能自拔 所以今天决定自己完成 ,,,,现将代码与思路贴出来供大家一起学习
XML配置数据库的连接主要技术点的博客;
JDBC编程 : JDBC连接数据库
DOM解析XML: DOM解析XML文件
SAX解析XML :SAX解析XML
难点:判断数据库的连接有很多种方式,SAX采用的事结束文本读取的时候判断,DOM采用的事全部解析完了再判断
XML文档 Config.xml
<?xml version="1.0" encoding="utf-8" ?> <!-- 连接数据库的配置文件 --> <sqlConnection> <sql id="oeacle11g"> <driver>oracle.jdbc.driver.OracleDriver</driver> <url>jdbc:oracle:thin:@127.0.0.1:1521:orcl</url> <use>scott</use> <pwd>tiger</pwd> </sql> <sql id="sql2005"> <driver>com.microsoft.sqlserver.jdbc.SQLServerDriver.class</driver> <url>jdbc:sqlserver://localhost:1433;databaseName=newer</url> <use>sa</use> <pwd>123</pwd> </sql> </sqlConnection>
DOM解析和SAX解析XML文档的属性类;
package xml配置数据库连接;
/**
* 配置数据库的连接属性
*
* @author Administrator
*
*/
public class ConfigSql {
public String id;
public String url;
public String driver;
public String use;
public String pwd;
@Override
public String toString() {
// TODO Auto-generated method stub
return id + " _" + driver+ "_" + url + "_ " + use + "_" + pwd;
}
}
1, DOM技术解析XML文档
package xml配置数据库连接;
import java.sql.DriverManager;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* DOM解析XML文档
*
* @author Administrator
*
*/
public class DOMParse {
// 创建队列保存对象
private static ArrayList<ConfigSql> liststu = new ArrayList<ConfigSql>();
// 设置对象
static ConfigSql stu = null;
public static void main(String[] args) throws Exception {
long l1 = System.currentTimeMillis();
String path = "C:\\Users\\Administrator\\Desktop\\xml\\Config.xml";
// 1,实例化一个用来生产DOM解析器的工厂对象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// 2使用工厂得到一个DOM解析器对象
DocumentBuilder builder = factory.newDocumentBuilder();
// 3,给定制定的xml文件
Document doc = builder.parse(path);
pausexml(doc);
// 遍历队列
for (int i = 0; i < liststu.size(); i++) {
ConfigSql stu = liststu.get(i);
System.out.println(stu);
if("oeacle11g".equals(stu.id)){
System.out.println(stu.id);
try {
// 加载驱动类
Class.forName(stu.driver);
// 定义字符串
String dri = stu.url;
// 连接数据库
DriverManager.getConnection(dri, stu.use, stu.pwd);
System.out.println("连接上了数据库");
} catch (Exception e) {
e.printStackTrace();
}
}
}
long l2 = System.currentTimeMillis();
long end = l2 - l1;
System.out.println("解析时间:" + end + "ms");
}
// 解析xml的方法
public static void pausexml(Node node) {
// 获得所有的子节点,得到所有子节点的节点队列
NodeList list = node.getChildNodes();
// 遍历队列,依次取出
for (int i = 0; i < list.getLength(); i++) {
Node node1 = list.item(i);
// 遍历节点的名称和值
String str = node1.getNodeName();
// System.out.println(val);
// 判断Node1是不是元素
if (node1 instanceof Element) {
if (str.equals("sql")) {
// 创建对象
stu = new ConfigSql();
// 将创建的对象放到队列中
liststu.add(stu);
// 获得该节点元素的属性
NamedNodeMap nnm = node1.getAttributes();
// 遍历节点中的元素
for (int j = 0; j < nnm.getLength(); j++) {
Node nodelist = nnm.item(j);
// 根据遍历出来的元素得到该节点元素属性的名字和值
String name = nodelist.getNodeName();
stu.id = nodelist.getNodeValue();
}
}
if (str.equals("driver")) {
stu.driver = node1.getTextContent();
}
if (str.equals("url")) {
// 如果标签是sex
stu.url = node1.getTextContent();
// System.out.println(sex + ">>>>>>>>>>>>>>>>>>>>");
}
if (str.equals("use")) {
stu.use = node1.getTextContent();
// System.out.println(cla + "<<<<<<<<<<<<<<<");
}
if (str.equals("pwd")) {
stu.pwd = node1.getTextContent();
// System.out.println(cla + "<<<<<<<<<<<<<<<");
}
}
pausexml(node1);
}
}
}
运行结果;
oeacle11g _oracle.jdbc.driver.OracleDriver_jdbc:oracle:thin:@127.0.0.1:1521:orcl_ scott_tiger
oeacle11g
连接上了数据库
sql2005 _com.microsoft.sqlserver.jdbc.SQLServerDriver.class_jdbc:sqlserver://localhost:1433;databaseName=newer_ sa_123
解析时间:770ms
2,SAX解析xml文档连接数据库;
package xml配置数据库连接;
import java.sql.DriverManager;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* SAX解析 1.创建解析工厂 2,创建解析工厂实例 3,工厂实例来解析XML(本地文件,文件解析器)
*
* @author Administrator
*/
public class SAXDemo {
ArrayList<ConfigSql> list;
ConfigSql stu;
String strValue;
String id ;
public static void main(String[] args) throws Exception {
SAXDemo demo = new SAXDemo();
demo.sefa();
}
public void sefa() throws Exception {
// 开始时间
long l1 = System.currentTimeMillis();
// 创建工厂实例
SAXParserFactory factory = SAXParserFactory.newInstance();
// 创建解析工厂
SAXParser parser = factory.newSAXParser();
String f = "C:\\Users\\Administrator\\Desktop\\xml\\Config.xml";
MyDefaultHandler hb = new MyDefaultHandler();
// 解析XML
parser.parse(f, hb);
long l2 = System.currentTimeMillis();
long end = l2 - l1;
// 1818ms
System.out.println("解析时间:" + end + "ms");
}
/**
* 创建外部类,解析XML startDocument开始读取文本 startElement开始读取元素
*/
class MyDefaultHandler extends DefaultHandler {
// 开始读取文本
public void startDocument() throws SAXException {
System.out.println("开始解析..");
list = new ArrayList<ConfigSql>();
}
// 结束文本读取
public void endDocument() throws SAXException {
for(int i= 0 ;i<list.size();i++){
ConfigSql strs= list.get(i);
// System.out.println(sql);
// }
// // 遍历队列
// for (ConfigSql strs : list) {
// System.out.println(strs.use);
//如果id是oracle11g就连接oracle
if("oeacle11g".equals(strs.id)){
System.out.println("准备连接数据库");
try{
//加载驱动类
Class.forName(strs.driver);
//定义字符串
String dri =strs.url ;
//连接数据库
DriverManager.getConnection(dri, strs.use, strs.pwd);
System.out.println("连接上了字符串");
}catch(Exception e){
e.printStackTrace();
}
}
//如果id是sql2005就连接sql2005
else if("sql2005".equals(strs.id)){
}
}
}
// 开始读取元素
public void startElement(String uri, String localName, String qName,
Attributes attr) throws SAXException {
if (qName.equals("sql")) {
stu = new ConfigSql();
list.add(stu);
id = attr.getValue("id");
stu.id =id;
}
}
// 结束元素的读取
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equals("driver")) {
stu.driver = strValue;
}
if (qName.equals("url")) {
stu.url = strValue;
}
if (qName.equals("use")) {
stu.use = strValue;
}
if (qName.equals("pwd")) {
stu.pwd = strValue;
}
}
// 读取字符串
public void characters(char ch[], int start, int length)
throws SAXException {
// 创建字符串
strValue = new String(ch, start, length);
}
}
}
运行结果:
开始解析..
准备连接数据库
连接上了字符串
解析时间:815ms