Chinaunix首页 | 论坛 | 博客
  • 博客访问: 604970
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-06-24 12:34:20

 1.DB.java


点击(此处)折叠或打开

  1. package com.bn.util;
  2.  import java.sql.Connection;
  3.  import java.sql.DriverManager;
  4.  import java.sql.PreparedStatement;
  5.  import java.sql.ResultSet;
  6.  import java.sql.SQLException;
  7.  import java.sql.Statement;
  8.  import java.util.Iterator;
  9.  import java.util.Properties;
  10.  import org.dom4j.Document;
  11.  import org.dom4j.DocumentException;
  12.  import org.dom4j.Element;
  13.  import org.dom4j.io.SAXReader;
  14.  public class DB {
  15.  private static String dbServer;
  16.  private static String dbName;
  17.  private static String dbUser;
  18.  private static String dbPwd;


  19.  //java 项目 fhadmin.org
  20.  public void readXML(){
  21.      
  22.      SAXReader sr = new SAXReader();//获取读取xml的对象。
  23.     Document doc = null;
  24.      String path1 = String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""));
  25.      //System.out.println(path1);
  26.      path1 = path1 + "../DB.xml";
  27.      //System.out.println(path1);
  28.    try {
  29.     doc = sr.read(path1);
  30.    } catch (DocumentException e) {
  31.     // TODO Auto-generated catch block
  32.     e.printStackTrace();
  33.    }//得到xml所在位置。然后开始读取。并将数据放入doc中
  34.     Element el_root = doc.getRootElement();//向外取数据,获取xml的根节点。
  35.     Iterator it = el_root.elementIterator();//从根节点下依次遍历,获取根节点下所有子节点
  36.     
  37.      while(it.hasNext()){//遍历该子节点
  38.      
  39.       Object o = it.next();//再获取该子节点下的子节点
  40.      Element el_row = (Element)o;
  41.       String s = el_row.getText();
  42.       Iterator it_row = el_row.elementIterator();
  43.       int i =1;
  44.       while(it_row.hasNext()){//遍历节点
  45.       Element el_ename = (Element)it_row.next();//获取该节点下的所有数据。
  46.    
  47.        if(i == 1){
  48.         this.setDbServer(el_ename.getText());
  49.         //System.out.println(this.getDbServer());
  50.         //dbServer = el_ename.getText();
  51.        }
  52.        if(i == 2){
  53.         this.setDbName(el_ename.getText());
  54.          // System.out.println(this.getDbName());
  55.         //dbName = el_ename.getText();
  56.        }
  57.        if(i == 3){
  58.         this.setDbUser(el_ename.getText());
  59.          // System.out.println(this.getDbUser());
  60.         //dbUser = el_ename.getText();
  61.        }
  62.        if(i == 4){
  63.         this.setDbPwd(el_ename.getText());
  64.          // System.out.println(this.getDbPwd());
  65.         //dbPwd = el_ename.getText();
  66.        }
  67.       // System.out.println(i);
  68.        i++;
  69.        //System.out.println(el_ename.getText());
  70.       }
  71.       //System.out.println(o);
  72.      }
  73.      
  74.     }
  75.  static {
  76.    DB dbxml = new DB();
  77.    dbxml.readXML();
  78.    
  79.    try {
  80.     Class.forName("com.mysql.jdbc.Driver");
  81.    } catch (ClassNotFoundException e) {
  82.     e.printStackTrace();
  83.    }
  84.  }

  85.  public static Connection createConn(){
  86.    DB dbxml = new DB();
  87.    //String name = dbxml.dbName;
  88.    //System.out.println(name);
  89.    //System.out.println(dbxml.getDbServer() + dbxml.getDbName() + dbxml.getDbUser() + dbxml.getDbPwd());
  90.    Connection conn = null;
  91.    try {
  92.     conn = DriverManager.getConnection("jdbc:mysql://"+ dbxml.getDbServer() +":3306/"+ dbxml.getDbName() +"?user="+ dbxml.getDbUser() +"&password="+ dbxml.getDbPwd());
  93.     //conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bnss?user=root&password=root");
  94.    } catch (SQLException e) {
  95.     e.printStackTrace();
  96.    }
  97.    return conn;
  98.  }

  99.  public static PreparedStatement createPstmt(Connection conn, String sql){
  100.    PreparedStatement pstmt = null;
  101.    try {
  102.     pstmt = conn.prepareStatement(sql);
  103.    } catch (SQLException e) {
  104.     e.printStackTrace();
  105.    }
  106.    return pstmt;
  107.  }

  108.  public static void close(Connection conn){
  109.    if(conn == null)return;
  110.    try {
  111.     conn.close();
  112.     conn = null;
  113.    } catch (SQLException e) {
  114.     e.printStackTrace();
  115.    }
  116.  }

  117.  public static void close(Statement stmt){
  118.    try {
  119.     stmt.close();
  120.     stmt = null;
  121.    } catch (SQLException e) {
  122.     e.printStackTrace();
  123.    }
  124.  }

  125.  public static void close(ResultSet rs){
  126.    try {
  127.     rs.close();
  128.     rs = null;
  129.    } catch (SQLException e) {
  130.     e.printStackTrace();
  131.    }
  132.  }
  133.  public String getDbName() {
  134.    return dbName;
  135.  }
  136.  public void setDbName(String dbName) {
  137.    this.dbName = dbName;
  138.  }
  139.  public String getDbServer() {
  140.    return dbServer;
  141.  }
  142.  public String getDbUser() {
  143.    return dbUser;
  144.  }
  145.  public String getDbPwd() {
  146.    return dbPwd;
  147.  }
  148.  public void setDbServer(String dbServer) {
  149.    this.dbServer = dbServer;
  150.  }
  151.  public void setDbUser(String dbUser) {
  152.    this.dbUser = dbUser;
  153.  }
  154.  public void setDbPwd(String dbPwd) {
  155.    this.dbPwd = dbPwd;
  156.  }
  157.  }
2.ParseXML.java

点击(此处)折叠或打开

  1. package com.bn.util;
  2.  import java.util.Properties;
  3.  import javax.xml.parsers.ParserConfigurationException;
  4.  import javax.xml.parsers.SAXParser;
  5.  import javax.xml.parsers.SAXParserFactory;
  6.  import org.xml.sax.SAXException;
  7.  import java.net.URL;
  8.  
  9.  //java项目fhadmin.org
  10.  public class ParseXML{
  11.      //定义一个Properties 用来存放 dbhost dbuser dbpassword的值
  12.     private Properties props;
  13.      //这里的props
  14.      public Properties getProps() {
  15.          return this.props;
  16.      }
  17.      public void parse(String filename) {
  18.          //将我们的解析器对象化
  19.         ConfigParser handler = new ConfigParser();
  20.          //获取SAX工厂对象
  21.         SAXParserFactory factory = SAXParserFactory.newInstance();
  22.          factory.setNamespaceAware(false);
  23.          factory.setValidating(false);
  24.          //获取SAX解析
  25.         SAXParser parser=null;
  26.          try {
  27.              parser = factory.newSAXParser();
  28.          } catch (Exception e1) {
  29.              e1.printStackTrace();
  30.          }
  31.          URL confURL = null;
  32.          //得到配置文件myenv.xml所在目录. tomcat中是在WEB-INF/classes
  33.          //下例中BeansConstants是用来存放xml文件中配置信息的类,可以自己代替或定义
  34.         try{
  35.              confURL = ParseXML.class.getClassLoader().getResource(filename);
  36.  //只需要将我们所需要的XML文件名字输入进去就可以了!
  37.         }catch(Exception e){
  38.              System.out.print(e.toString());
  39.          }
  40.          try
  41.          { //将解析器和解析对象myenv.xml联系起来,开始解析
  42.             parser.parse(confURL.toString(), handler);
  43.              //获取解析成功后的属性 以后 我们其他应用程序只要调用本程序的props就可以提取出属性名称和值了
  44.             props = handler.getProps();
  45.          }catch(Exception e){
  46.              System.out.println(e.toString());
  47.          }finally{
  48.              factory=null;
  49.              parser=null;
  50.              handler=null;
  51.          }
  52.      }
  53.  }
3.ConfigParser.java
点击(此处)折叠或打开


  1. package com.bn.util;
  2.  import org.xml.sax.Attributes;
  3.  import org.xml.sax.helpers.DefaultHandler;
  4.  import org.xml.sax.SAXException;
  5.  import java.util.Properties;

  6. //使用DefaultHandler的好处 是 不必陈列出所有方法,
  7. //java项目fhadmin.org
  8. public class ConfigParser extends DefaultHandler {
  9.      ////定义一个Properties 用来存放 dbhost dbuser dbpassword的值
  10.     private Properties props;
  11.      private String currentSet;
  12.      private String currentName;
  13.      private StringBuffer currentValue = new StringBuffer();
  14.      //构建器初始化props
  15.      public ConfigParser() {
  16.          this.props = new Properties();
  17.      }
  18.      public Properties getProps() {
  19.          return this.props;
  20.      }
  21.      //定义开始解析元素的方法. 这里是将<xxx>中的名称xxx提取出来.
  22.      public void startElement(String uri, String localName, String qName, Attributes attributes)throws SAXException {
  23.          currentValue.delete(0, currentValue.length());
  24.          this.currentName =qName;
  25.      }
  26.      //这里是将<xxx></xxx>之间的值加入到currentValue
  27.      public void characters(char[] ch, int start, int length) throws SAXException {
  28.          currentValue.append(ch, start, length);
  29.      }
  30.      //在遇到</xxx>结束后,将之前的名称和值一一对应保存在props中
  31.     public void endElement(String uri, String localName, String qName) throws SAXException {
  32.          props.put(qName.toLowerCase(), currentValue.toString().trim());
  33.      }
  34.  }


package com.bn.util;
 import org.xml.sax.Attributes;
 import org.xml.sax.helpers.DefaultHandler;
 import org.xml.sax.SAXException;
 import java.util.Properties;

//使用DefaultHandler的好处 是 不必陈列出所有方法,
//java项目
public class ConfigParser extends DefaultHandler {
     ////定义一个Properties 用来存放 dbhost dbuser dbpassword的值
    private Properties props;
     private String currentSet;
     private String currentName;
     private StringBuffer currentValue = new StringBuffer();
     //构建器初始化props
     public ConfigParser() {
         this.props = new Properties();
     }
     public Properties getProps() {
         return this.props;
     }
     //定义开始解析元素的方法. 这里是将中的名称xxx提取出来.
     public void startElement(String uri, String localName, String qName, Attributes attributes)throws SAXException {
         currentValue.delete(0, currentValue.length());
         this.currentName =qName;
     }
     //这里是将之间的值加入到currentValue
     public void characters(char[] ch, int start, int length) throws SAXException {
         currentValue.append(ch, start, length);
     }
     //在遇到结束后,将之前的名称和值一一对应保存在props中
    public void endElement(String uri, String localName, String qName) throws SAXException {
         props.put(qName.toLowerCase(), currentValue.toString().trim());
     }
 }
阅读(1195) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~