Project

General

Profile

« Previous | Next » 

Revision 13

added attribute saving to DB

View differences:

DBElement.java
15 15

  
16 16
import org.w3c.dom.DOMException;
17 17
import org.w3c.dom.Element;
18
import org.w3c.dom.Attr;
18 19
import org.w3c.dom.Node;
19 20
import org.w3c.dom.NodeList;
21
import org.w3c.dom.NamedNodeMap;
20 22
import org.w3c.dom.ProcessingInstruction;
21 23
import org.w3c.dom.Text;
22 24

  
......
36 38
     */
37 39
    public void writeXmlToDB(Connection conn, int parentID) 
38 40
		throws IOException {
39
/*
40
        if (attributes != null)
41
	    attributes.writeXml(conn);
42
*/
41

  
43 42
        int maxid=0;
44 43
        int newid=0;
45 44
        Statement stmt;
......
97 96
          System.out.println(e.getMessage());
98 97
        }
99 98

  
100
	if (hasChildNodes())
99
        System.out.println("ID: " + newid + "\tTag: " + getTagName());
100

  
101
        // Now save this node's attributes
102
	writeAttributesToDB(conn, newid);
103

  
104
        // Now save this node's children
105
	if (hasChildNodes()) {
101 106
            try {
102 107
	      writeChildrenXmlToDB(conn, newid);
103 108
            } catch (SQLException e) {
104 109
              System.out.println(e.getMessage());
105 110
            }
111
        }
106 112

  
107
        System.out.println("ID: " + newid + "\tTag: " + getTagName());
108 113
    }
109 114

  
110 115
    public void writeChildrenXmlToDB(Connection conn, int parentID) 
......
131 136
      }
132 137
    }
133 138

  
139
    /**
140
     * Writes this element's attributes out to the 
141
     * given database connection 
142
     */
143
    public void writeAttributesToDB(Connection conn, int nodeid) 
144
		throws IOException {
145
        NamedNodeMap attributes = getAttributes();
146

  
147
        if (attributes != null) {
148
            for (int i=0; i < attributes.getLength(); i++) {
149
               String name = ((Attr)attributes.item(i)).getName();
150
               String value = ((Attr)attributes.item(i)).getValue();
151
               System.out.println("    Saving attribute: " + name + "=" + value);
152

  
153
               int maxid=0;
154
               int newid=0;
155
               Statement stmt;
156
               try {
157
                 stmt = conn.createStatement();
158
                 stmt.execute("SELECT MAX(attributeid) FROM xml_attributes");
159
                 try {
160
                   ResultSet rs = stmt.getResultSet();
161
                   try {
162
                     boolean tableHasRows = rs.next();
163
                     if (tableHasRows) {
164
                       try {
165
                         maxid = rs.getInt(1);
166
                       } catch (SQLException e) {
167
                         System.out.println("Error with getInt: " + e.getMessage());
168
                       }
169
              }
170
                   } catch (SQLException e) {
171
                     System.out.println("Error with next: " + e.getMessage());
172
                   }
173
                 } catch (SQLException e) {
174
                   System.out.println("Error with getrset: " + e.getMessage());
175
                 }
176
                 stmt.close();
177
               } catch (SQLException e) {
178
                 System.out.println("Error getting id: " + e.getMessage());
179
               }
180
        
181
               // assign a new ID number
182
               newid = maxid + 1;
183
       
184
               try {
185
                 PreparedStatement pstmt;
186
                 pstmt = conn.prepareStatement(
187
                  "INSERT INTO xml_attributes(attributeid, nodeid, " +
188
                  "attributenumber, attributename, attributevalue) " +
189
		   "VALUES (?, ?, ?, ?, ?)");
190
       
191
                 // Bind the values to the query
192
                 pstmt.setInt(1, newid); 		// ATTRIBUTEID
193
                 pstmt.setInt(2, nodeid);		// NODEID
194
                 pstmt.setInt(3, i+1); 			// ATTRIBUTENUMBER
195
                 pstmt.setString(4, name);		// ATTRIBUTENAME
196
                 pstmt.setString(5, value);		// ATTRIBUTEVALUE
197

  
198
                 // Do the insertion
199
                 pstmt.execute();
200
                 pstmt.close();
201
       
202
               } catch (SQLException e) {
203
                 System.out.println(e.getMessage());
204
               }
205
            }
206
        }
207
    }
208

  
134 209
    // used by JTree to display this node
135 210
    public String toString ()
136 211
    {

Also available in: Unified diff