Project

General

Profile

1
/**
2
 *        Name: DBSAXDocument.java
3
 *     Purpose: A Class that writes an XML document main data
4
 *   Copyright: 2000 Regents of the University of California and the
5
 *              National Center for Ecological Analysis and Synthesis
6
 *     Authors: Jivka Bojilova
7
 *
8
 *     Version: '$Id: DBSAXDocument.java 74 2000-05-05 00:37:58Z jones $'
9
 */
10

    
11
package edu.ucsb.nceas.metacat;
12

    
13
import java.sql.*;
14
import java.io.IOException;
15
import java.util.Hashtable;
16
import java.util.Enumeration;
17

    
18
/** 
19
 * A Class that writes an XML document main data to a database connection
20
 */
21
public class DBSAXDocument {
22

    
23
    private Connection		conn;
24
    private long            rootnodeid;
25
    private String          docname;
26
    private String          doctype;
27

    
28
    /** 
29
     * Construct a new document instance
30
     *
31
     * @param conn the JDBC Connection to which all information is written
32
     * @param rootnodeid - sequence id of the root node in the document
33
     * @param docname - the name of DTD, i.e. the name immediately following the DOCTYPE keyword - should be the root element name.
34
     * (Oracle's and IBM parsers are not aware if it is not the root element name)
35
     * @param doctype - Public ID of the DTD, i.e. the name immediately following the PUBLIC keyword in DOCTYPE declaration.
36
     *
37
     */
38
    public DBSAXDocument (Connection conn, long rootnodeid, String docname, String doctype)
39
    {
40
      this.conn = conn;
41
      this.rootnodeid = rootnodeid;
42
      this.docname = docname;
43
      this.doctype = doctype;
44
      writeDocumentToDB();
45
      System.out.println("DBSAXDocument.writeDocumentToDB");
46
    }
47
    
48
    /** creates SQL code and inserts new document into DB connection */
49
    private void writeDocumentToDB() {
50
        try {
51
          conn.setAutoCommit(false);
52
          PreparedStatement pstmt;
53
          pstmt = conn.prepareStatement(
54
                "INSERT INTO xml_documents (docid, rootnodeid, docname, doctype) " +
55
                "VALUES (null, ?, ?, ?)");
56

    
57
          // Bind the values to the query
58
          pstmt.setLong(1, rootnodeid);
59
          pstmt.setString(2, docname);
60
          pstmt.setString(3, doctype);
61
          // Do the insertion
62
          pstmt.execute();
63
          pstmt.close();
64
          conn.commit();
65
          conn.setAutoCommit(true);
66
        } catch (SQLException e) {
67
          System.out.println(e.getMessage());
68
        }
69
    }
70

    
71
}
(7-7/29)