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

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-03-17 09:25:33


点击(此处)折叠或打开


  1. 1.DB.java

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


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

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

  104.  public static PreparedStatement createPstmt(Connection conn, String sql){
  105.    PreparedStatement pstmt = null;
  106.    try {
  107.     pstmt = conn.prepareStatement(sql);
  108.    } catch (SQLException e) {
  109.     e.printStackTrace();
  110.    }
  111.    return pstmt;
  112.  }

  113.  public static void close(Connection conn){
  114.    if(conn == null)return;
  115.    try {
  116.     conn.close();
  117.     conn = null;
  118.    } catch (SQLException e) {
  119.     e.printStackTrace();
  120.    }
  121.  }

  122.  public static void close(Statement stmt){
  123.    try {
  124.     stmt.close();
  125.     stmt = null;
  126.    } catch (SQLException e) {
  127.     e.printStackTrace();
  128.    }
  129.  }

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







  163. =======================================================
  164. 2.ParseXML.java

  165.  package com.bn.util;
  166.  import java.util.Properties;
  167.  import javax.xml.parsers.ParserConfigurationException;
  168.  import javax.xml.parsers.SAXParser;
  169.  import javax.xml.parsers.SAXParserFactory;
  170.  import org.xml.sax.SAXException;
  171.  import java.net.URL;

  172. /**
  173.  * 说明:java读取xml
  174.  * 作者:FH Admin
  175.  * from:fhadmin.cn
  176.  */
  177.  public class ParseXML{
  178.      //定义一个Properties 用来存放 dbhost dbuser dbpassword的值
  179.     private Properties props;
  180.      //这里的props
  181.      public Properties getProps() {
  182.          return this.props;
  183.      }
  184.      public void parse(String filename) {
  185.          //将我们的解析器对象化
  186.         ConfigParser handler = new ConfigParser();
  187.          //获取SAX工厂对象
  188.         SAXParserFactory factory = SAXParserFactory.newInstance();
  189.          factory.setNamespaceAware(false);
  190.          factory.setValidating(false);
  191.          //获取SAX解析
  192.         SAXParser parser=null;
  193.          try {
  194.              parser = factory.newSAXParser();
  195.          } catch (Exception e1) {
  196.              e1.printStackTrace();
  197.          }
  198.          URL confURL = null;
  199.          //得到配置文件myenv.xml所在目录. tomcat中是在WEB-INF/classes
  200.          //下例中BeansConstants是用来存放xml文件中配置信息的类,可以自己代替或定义
  201.         try{
  202.              confURL = ParseXML.class.getClassLoader().getResource(filename);
  203.  //只需要将我们所需要的XML文件名字输入进去就可以了!
  204.         }catch(Exception e){
  205.              System.out.print(e.toString());
  206.          }
  207.          try
  208.          { //将解析器和解析对象myenv.xml联系起来,开始解析
  209.             parser.parse(confURL.toString(), handler);
  210.              //获取解析成功后的属性 以后 我们其他应用程序只要调用本程序的props就可以提取出属性名称和值了
  211.             props = handler.getProps();
  212.          }catch(Exception e){
  213.              System.out.println(e.toString());
  214.          }finally{
  215.              factory=null;
  216.              parser=null;
  217.              handler=null;
  218.          }
  219.      }
  220.  }
  221. ===================================
  222. 3.ConfigParser.java

  223. package com.bn.util;
  224.  import org.xml.sax.Attributes;
  225.  import org.xml.sax.helpers.DefaultHandler;
  226.  import org.xml.sax.SAXException;
  227.  import java.util.Properties;

  228.  //使用DefaultHandler的好处 是 不必陈列出所有方法,
  229. public class ConfigParser extends DefaultHandler {
  230.      ////定义一个Properties 用来存放 dbhost dbuser dbpassword的值
  231.     private Properties props;
  232.      private String currentSet;
  233.      private String currentName;
  234.      private StringBuffer currentValue = new StringBuffer();
  235.      //构建器初始化props
  236.      public ConfigParser() {
  237.          this.props = new Properties();
  238.      }
  239.      public Properties getProps() {
  240.          return this.props;
  241.      }
  242.      //定义开始解析元素的方法. 这里是将<xxx>中的名称xxx提取出来.
  243.      public void startElement(String uri, String localName, String qName, Attributes attributes)throws SAXException {
  244.          currentValue.delete(0, currentValue.length());
  245.          this.currentName =qName;
  246.      }
  247.      //这里是将<xxx></xxx>之间的值加入到currentValue
  248.      public void characters(char[] ch, int start, int length) throws SAXException {
  249.          currentValue.append(ch, start, length);
  250.      }
  251.      //在遇到</xxx>结束后,将之前的名称和值一一对应保存在props中
  252.     public void endElement(String uri, String localName, String qName) throws SAXException {
  253.          props.put(qName.toLowerCase(), currentValue.toString().trim());
  254.      }
  255.  }


阅读(508) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~