Project

General

Profile

« Previous | Next » 

Revision 11

Added by Matt Jones about 25 years ago

added foreign key to parentnodeid

View differences:

DBElement.java
34 34
     * Writes this element and all of its children out to the 
35 35
     * given database connection 
36 36
     */
37
    public void writeXmlToDB(Connection conn) throws IOException {
37
    public void writeXmlToDB(Connection conn, int parentID) 
38
		throws IOException {
38 39
/*
39 40
        if (attributes != null)
40 41
	    attributes.writeXml(conn);
41 42
*/
42 43
        int maxid=0;
44
        int newid=0;
43 45
        Statement stmt;
44 46
        try {
45 47
          stmt = conn.createStatement();
......
65 67
        } catch (SQLException e) {
66 68
          System.out.println("Error getting id: " + e.getMessage());
67 69
        }
70
        
71
        // assign a new ID number
72
        newid = maxid + 1;
68 73

  
69 74
        try {
70
          PreparedStatement pstmt = conn.prepareStatement(
71
                "INSERT INTO xml_elements(nodeid, nodename) values (?, ?)");
75
          PreparedStatement pstmt;
76
          if (parentID != 0) {
77
            pstmt = conn.prepareStatement(
78
                "INSERT INTO xml_elements(nodeid, nodename, parentnodeid) " + 
79
		"VALUES (?, ?, ?)");
80
          } else {
81
            pstmt = conn.prepareStatement(
82
                "INSERT INTO xml_elements(nodeid, nodename) " + 
83
		"VALUES (?, ?)");
84
          }
72 85

  
73
          // Add an element as element number 1500
74
          pstmt.setInt(1, maxid+1); // The first ? is for NODEID
86
          // Bind the values to the query
87
          pstmt.setInt(1, newid); // The first ? is for NODEID
75 88
          pstmt.setString(2, getTagName());// The second ? is for NODENAME
89
          if (parentID != 0) {
90
            pstmt.setInt(3, parentID);
91
          }
76 92
          // Do the insertion
77 93
          pstmt.execute();
78 94
          pstmt.close();
......
82 98
        }
83 99

  
84 100
	if (hasChildNodes())
85
	    writeChildrenXmlToDB(conn);
101
	    writeChildrenXmlToDB(conn, newid);
86 102

  
87
        System.out.println("ID: " + maxid + "\tTag: " + getTagName());
103
        System.out.println("ID: " + newid + "\tTag: " + getTagName());
88 104
    }
89 105

  
90
    public void writeChildrenXmlToDB(Connection conn) throws IOException {
106
    public void writeChildrenXmlToDB(Connection conn, int parentID) 
107
		throws IOException {
91 108

  
92 109
      NodeList children = getChildNodes();
93 110
      if (children != null) {
94 111
        for (int i = 0; i < children.getLength(); i++) {
95 112
          if (children.item(i) instanceof ElementNode) {
96
            ((DBElement)children.item(i)).writeXmlToDB(conn);
113
            ((DBElement)children.item(i)).writeXmlToDB(conn, parentID);
97 114
          } else {
98 115
            System.out.println("    Text Node skipped.");
99 116
          }
DBWriter.java
64 64

  
65 65
    	root = (DBElement)doc.getDocumentElement();
66 66
	root.normalize();
67
        root.writeXmlToDB(conn);
67
        root.writeXmlToDB(conn, 0);
68 68
    }
69 69

  
70 70
    public static void main (String argv [])

Also available in: Unified diff