用xml连接数据库代码

package com.util.db;


import java.sql.Connection;
import java.sql.DriverManager;
//import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;


import com.util.db.DbInfo;


public class PosDB {

 /**
  * @param args
  *
  */
 
   private Connection conn = null;
   private Statement stmt = null;
   private ResultSet rs = null;
//   private PreparedStatement pstmt = null;
 
 
   public static Connection getCon(){
   Connection conn = null;
  
   try {
   Class.forName(DB_Info.getDbDriver());
   conn=DriverManager.getConnection(DB_Info.getDbUrl(),DB_Info.getDbName(),DB_Info.getDbPassword());
  } catch (Exception ex) {
   // TODO 自动生成 catch 块
   ex.printStackTrace();
   System.out.println("数据库连接错误!");
  }
          
   return conn;
   }
 
 
  
   public Statement getStmtread() {

      try {
        conn = getCon();
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                                    ResultSet.CONCUR_READ_ONLY);

      }
      catch (Exception e) {
        System.out.println("getStmtread");
        System.out.println(e.getMessage());
      }
      return stmt;
    }

 
   /**
    *
    *
    * getStmt()方法得到SQL 执行类
    *
    * */
   public Statement getStmt() {

     try {

       conn = getCon();
       stmt = conn.createStatement();
     }
     catch (Exception e) {
       System.out.println("getStmt");
       System.out.println(e.getMessage());
     }
     return stmt;
   }
   public void excute(String SQL) {
      //int k=0;

      try {
        conn = getCon();
        stmt = this.getStmtread();
        stmt.execute(SQL);
      }
      catch (Exception e) {
        System.out.println(e.getMessage());
      }
    }
  
   public boolean getUpdate(String SQL) {
      int k = 0;

      try {
        //conn=getCon();
        stmt = this.getStmtread();
        k = stmt.executeUpdate(SQL);
      }
      catch (Exception e) {
        System.out.println(e.getMessage());
      }
      if (k > 0) {
        return true;
      }
      else {
        return false;
      }
    }

  
   /**
    * getRes()方法用来得到查询结果
    *
    * */
    public ResultSet getRs(String SQL) {

     try {

       conn=getCon();
       stmt = this.getStmtread();
       rs = stmt.executeQuery(SQL);
     }
     catch (Exception e) {
       System.out.println(e.getMessage());

     }
     return rs;
   }

  

    public void close() {

     try {

       if (rs != null) {
         rs.close();
       }
     }
     catch (Exception e) {

     }
     try {
       if (stmt != null) {
         rs.close();
       }
     }
     catch (Exception e) {

     }
     try {
       if (conn != null) {
         conn.close();
       }
     }
     catch (Exception e) {
       System.out.println(e.getMessage());
     }
   }

 
 
 
 
 public static void main(String[] args) {
  
 // ReaderDbInfoXML configReader = new ReaderDbInfoXML("E://workspace2//LongBank_POS//src//com//util//db//dbInfoXML.xml");
  
  System.out.println(DbInfo.getDbUrl());
  System.out.println(DbInfo.getDbDriver());
  // TODO 自动生成方法存根

  PosDB db=new PosDB();
  try{
  ResultSet rs1=db.getRs("select * from login");
  
  while(rs1.next()){
   System.out.println(rs1.getInt("id"));
   System.out.println(rs1.getString("name"));
   System.out.println(rs1.getString("password"));
   
  }
  }catch(Exception ex){
   ex.printStackTrace();
  }

 }

}

 

 


public class ReaderDbInfoXML {
 
 public ReaderDbInfoXML(String xmlname){
  
  try{
   parseDbXML(xmlname);
  }catch(Exception ex){
   ex.printStackTrace();
  }
 }
 
 public void parseDbXML(String xmlurl) throws IOException{
  
      try{
      
          SAXBuilder builder = new SAXBuilder(false);

              
              
             Document doc = builder.build(xmlurl);            
             org.jdom.Element root = doc.getRootElement();
            
           
             DB_Info.setDbUrl(root.getChild("database").getChild("url").
                    getTextTrim()+"?useUnicode=true&characterEncoding=GBK");            
             DB_Info.setDbDriver(root.getChild("database").getChild(
                      "jdbcdriver").getTextTrim());
             DB_Info.setDbName(root.getChild("database").getChild(
                     "dbusername").getTextTrim());
             DB_Info.setDbPassword(root.getChild("database").getChild(
                     "dbpassword").getTextTrim());
             DB_Info.setXmlurl(root.getChild("xmlfile").getChild(
                     "xmlurl").getTextTrim());
             DB_Info.setMaxconnection(Integer.parseInt(root.getChild("database").getChild(
                "maxconnection").getTextTrim()));
             DB_Info.setLogfile(root.getChild("database").getChild("logfile").getTextTrim());
            
            System.out.println("xmlurl="+root.getChild("xmlfile").getChild(
               "xmlurl").getTextTrim());
           

           
           
            DB_Info.setDbUrlToServer(root.getChild("toserverdb").getChild("url").
                    getTextTrim()+"?useUnicode=true&characterEncoding=GBK");            
            DB_Info.setDbDriverToServer(root.getChild("toserverdb").getChild(
                      "jdbcdriver").getTextTrim());
            DB_Info.setDbNameToServer(root.getChild("toserverdb").getChild(
                     "dbusername").getTextTrim());
            DB_Info.setDbPasswordToServer(root.getChild("toserverdb").getChild(
                     "dbpassword").getTextTrim());
          
           
      }catch(JDOMException jdome){
    
      }

  
 }
 

      public static void main(String[] args)
    {
        new ReaderDbInfoXML("src/com/util/db/dbInfoXML.xml");
     }

}
 

 

 

 

 

 

dbInfoXML.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <database>

  <url>jdbc:mysql://192.168.10.63:3306/longbankpos</url>
  <jdbcdriver>org.gjt.mm.mysql.Driver</jdbcdriver>
  <dbusername>longbank</dbusername> 
  <dbpassword>longbankpos</dbpassword>
  <maxconnection>500</maxconnection> 
  <logfile>D:/posLog.txt</logfile>
 </database>
 
 <toserverdb>
      <url>jdbc:mysql://192.168.10.254:3306/longbankpos</url>
   <jdbcdriver>org.gjt.mm.mysql.Driver</jdbcdriver>
   <dbusername>longbank</dbusername> 
   <dbpassword>longbankpos</dbpassword>
   <maxconnection>500</maxconnection>
 </toserverdb>
 
 <xmlfile>
    <xmlurl>src/com/main/util/config.xml</xmlurl>
 </xmlfile>
</configuration>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值