⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdao.ja

📁 java操作xml文件的各种代码
💻 JA
字号:
import java.sql.*;
import java.util.*;
import java.lang.reflect.*;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.*;

public class JDAO {
  private Document doc;

  public JDAO(String xmlFile) {
    try{
      DOMParser parser = new DOMParser();
      parser.parse(xmlFile);
      doc = parser.getDocument();
  } catch( Exception e ){
      e.printStackTrace(System.err);
    }
  }

  public Vector parseResultSet(ResultSet rs){
    //将传入的ResultSet的字段名称作一个对应表
    HashMap hm = new HashMap();
    try{
      ResultSetMetaData rsmd = rs.getMetaData();
      for(int i=1; i<=rsmd.getColumnCount(); i++)
        hm.put(rsmd.getColumnName(i), rsmd.getColumnClassName(i));
    } catch ( SQLException sqle ){
      sqle.printStackTrace(System.err);
    }
    //使用Reflection来调用相关的函数
    Vector vRows = new Vector();
    Element e = doc.getDocumentElement();
    Class c = null;
    Object o = null;
    try{
      c = Class.forName(e.getAttribute("CLASS").toString());
    } catch( Exception ex ){
      System.out.print("无法建立对象:");
      ex.printStackTrace(System.err);
    }

    NodeList nl = e.getElementsByTagName("Columns");
    try{
    while(rs.next()){
      try{
        o = c.newInstance();
      } catch( Exception ex ){
        System.out.print("无法建立对象:");
        ex.printStackTrace(System.err);
      }
      for(int i=0; i<nl.getLength(); i++){
        Element current_ele = (Element)nl.item(i);
        String colName = current_ele.getAttribute("NAME");
        String methodName = current_ele.getAttribute("METHOD");

        String className = (String)hm.get(colName);
        Class  paramTypes[] = new Class[1];
        Object arg[] = new Object[1];

        if(className.equals("java.lang.String")){
          paramTypes[0] = java.lang.String.class;
          arg[0] = rs.getString(colName);
        }else if(className.equals("int")){
          paramTypes[0]=int.class;
          arg[0] = new Integer(rs.getInt(colName));
        } else if(className.equals("java.sql.Timestamp")){
          paramTypes[0] = java.sql.Timestamp.class;
          arg[0] = rs.getTimestamp(colName);
        }// end of if
        try{
          Method m = c.getMethod(methodName, paramTypes);
          m.invoke(o, arg);
        } catch(Exception ex){
          ex.printStackTrace(System.out);
        }
      }// end of for
      vRows.addElement(o);
    }// end of while
    } catch( SQLException sqle ){
        sqle.printStackTrace(System.err);
    }
    return vRows;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -