欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

一种处理大型XML文件并将其存入数据库的方法

最编程 2024-07-23 12:32:36
...
/*配置文件解析类,很简单,不说了*/ 
public  class ConnectionProvider  extends Object { 
   public  static  final  boolean DEBUG =  true

   protected OracleConnection connection; 

   protected XMLDocument connectionDefinition; 

   public  static  final String CONNECTION =  "Connection"
   public  static  final String DRIVER =  "Driver"
   public  static  final String HOSTNAME =  "Hostname"
   public  static  final String PORT =  "Port"
   public  static  final String SID =  "SID"
   public  static  final String SERVICENAME =  "ServiceName"
   public  static  final String SERVERMODE =  "Server"
   public  static  final String SCHEMA =  "Schema"
   public  static  final String PASSWORD =  "Password"
   public  static  final String POOL =  "Pool"
   public  static  final String THIN_DRIVER =  "thin"
   // public static final String OCI_DRIVER = "oci8"; 

   public  static  final String DEFAULT_CONNECTION_DEFINITION =  "c:\\temp\\connection.xml"
   public  static  final String DEFAULT_DRIVER = THIN_DRIVER; 
   public  static  final String DEFAULT_HOSTNAME =  "localhost"
   public  static  final String DEFAULT_PORT =  "1521"
   public  static  final String DEFAULT_SERVERMODE =  "DEDICATED"

   public  static  final String TARGET_DIRECTORY =  "targetDirectory"

   protected PrintStream log; 

   public ConnectionProvider() { 

  } 

   public  void initializeConnection()  throws SAXException, IOException, 
      SQLException { 
     this.initializeConnection(System.out); 
  } 

   public  void initializeConnection(PrintStream log)  throws SAXException, 
      IOException, SQLException { 
    DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver()); 
     this.log = log; 
    loadConnectionSettings(); 
     this.connection = openConnection(); 
  } 

   public ConnectionProvider getConnectionProvider() { 
     return  this
  } 

   public  void initalizeConnection(String connectionLocation, PrintStream log) 
       throws SAXException, IOException, SQLException { 
    DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver()); 
     this.log = log; 
    loadConnectionSettings(connectionLocation); 
     this.connection = openConnection(); 
  } 

   public  void setLogger(PrintStream log) { 
     this.log = log; 
  } 

   private  void setConnectionSettings(XMLDocument doc) { 
     this.connectionDefinition = doc; 
  } 

   private  void dumpConnectionSettings()  throws IOException { 
    StringWriter sw =  new StringWriter(); 
    PrintWriter pw =  new PrintWriter(sw); 
     this.connectionDefinition.print(pw); 
    pw.close(); 
    sw.close(); 
  } 

   public OracleConnection getConnection()  throws SQLException { 
     return  this.connection; 
  } 

   public  void closeConnection(Connection conn)  throws Exception { 
     if (isPooled()) { 
      conn.close(); 
    } 
  } 

   public Connection getConnection(String schema, String passwd) 
       throws Exception { 
     if (isPooled()) { 
       return (OracleOCIConnection)  this.getConnection(schema, passwd); 
    }  else { 
       return  this.connection; 
    } 
  } 

   public String getSetting(String nodeName) { 
     return getSetting(nodeName,  null); 
  } 

   public String getSetting(String nodeName, String defaultValue) { 
    XMLElement root = (XMLElement)  this.connectionDefinition 
        .getDocumentElement(); 
    NodeList children = root.getChildrenByTagName(nodeName); 
     if (children.getLength() != 0) { 
      Element element = (Element) children.item(0); 
      Text text = (Text) element.getFirstChild(); 
       if (text !=  null) { 
         return text.getData(); 
      } 
    } 
     return defaultValue; 
  } 

   protected String getDriver() { 
     return getSetting(DRIVER, DEFAULT_DRIVER); 
  } 

   protected String getHostname() { 
     return getSetting(HOSTNAME, DEFAULT_HOSTNAME); 
  } 

   protected String getPort() { 
     return getSetting(PORT, DEFAULT_PORT); 
  } 

   protected String getServerMode() { 
     return getSetting(SERVERMODE, DEFAULT_SERVERMODE); 
  } 

   protected String getServiceName() { 
     return getSetting(SERVICENAME); 
  } 

   protected String getSID() { 
     return getSetting(SID); 
  } 

   protected  boolean isPooled() { 
    String usePool = getSetting(POOL, Boolean.FALSE.toString()); 
     return !usePool.equalsIgnoreCase(Boolean.FALSE.toString()); 
  } 

   protected String getSchema() { 
     return getSetting(SCHEMA); 
  } 

   protected String getPassword() { 
     return getSetting(PASSWORD); 
  } 

   public  void loadConnectionSettings() 

上一篇: 实操指南:在开发阶段如何利用SQLE进行SQL审计 - 一看就会的效果展示

下一篇: 用SQL Server理解与实现E-R(实体-关系)模型指南

推荐阅读