Project

General

Profile

« Previous | Next » 

Revision 19

Added by Matt Jones over 24 years ago

added attribute persistence to DB code

View differences:

DBSAXElement.java
27 27
    public DBSAXElement (Connection conn, String tagname, 
28 28
                         long parent_id) {
29 29
      this.conn = conn;
30
      this.element_id = assignElementID();
31 30

  
32 31
      this.tagname = tagname;
33 32
      this.parent_id = parent_id;
......
36 35
      writeElementToDB();
37 36
    };
38 37
    
39
    private long assignElementID() {
40
        long maxid=0;
38
    /** creates SQL code and inserts new element into DB connection */
39
    private void writeElementToDB() {
40
        try {
41
          conn.setAutoCommit(false);
42
          PreparedStatement pstmt;
43
          if (parent_id != 0) {
44
            pstmt = conn.prepareStatement(
45
                "INSERT INTO xml_elements(nodeid, nodename, parentnodeid) " +
46
                "VALUES (null, ?, ?)");
47
          } else {
48
            pstmt = conn.prepareStatement(
49
                "INSERT INTO xml_elements(nodeid, nodename) " +
50
                "VALUES (null, ?)");
51
          }
52

  
53
          // Bind the values to the query
54
          pstmt.setString(1, getTagName());// The second ? is for NODENAME
55
          if (parent_id != 0) {
56
            pstmt.setLong(2, parent_id);
57
          }
58
          // Do the insertion
59
          pstmt.execute();
60
          pstmt.close();
61
          this.element_id = getAssignedElementID();
62
          conn.commit();
63
          conn.setAutoCommit(true);
64
        } catch (SQLException e) {
65
          System.out.println(e.getMessage());
66
        }
67
    }
68

  
69
    /** look up the assigned element id from DB connection */
70
    private long getAssignedElementID() {
71
        long assigned_id=0;
41 72
        Statement stmt;
42 73
        try {
43 74
          stmt = conn.createStatement();
44
          stmt.execute("SELECT MAX(nodeid) FROM xml_elements");
75
          stmt.execute("SELECT xml_elements_id_seq.currval FROM dual");
45 76
          try {
46 77
            ResultSet rs = stmt.getResultSet();
47 78
            try {
48 79
              boolean tableHasRows = rs.next();
49 80
              if (tableHasRows) {
50 81
                try {
51
                  maxid = rs.getInt(1);
82
                  assigned_id = rs.getLong(1);
52 83
                } catch (SQLException e) {
53 84
                  System.out.println("Error with getInt: " + e.getMessage());
54 85
                }
......
64 95
          System.out.println("Error getting id: " + e.getMessage());
65 96
        }
66 97

  
67
        // assign a new ID number
68
        return (maxid + 1);
98
        // assign the new ID number
99
        return assigned_id;
69 100
    }
70 101

  
71
    private void writeElementToDB() {
72
        try {
73
          PreparedStatement pstmt;
74
          if (parent_id != 0) {
75
            pstmt = conn.prepareStatement(
76
                "INSERT INTO xml_elements(nodeid, nodename, parentnodeid) " +
77
                "VALUES (?, ?, ?)");
78
          } else {
79
            pstmt = conn.prepareStatement(
80
                "INSERT INTO xml_elements(nodeid, nodename) " +
81
                "VALUES (?, ?)");
82
          }
83

  
84
          // Bind the values to the query
85
          pstmt.setLong(1, element_id); // The first ? is for NODEID
86
          pstmt.setString(2, getTagName());// The second ? is for NODENAME
87
          if (parent_id != 0) {
88
            pstmt.setLong(3, parent_id);
89
          }
90
          // Do the insertion
91
          pstmt.execute();
92
          pstmt.close();
93

  
94
        } catch (SQLException e) {
95
          System.out.println(e.getMessage());
96
        }
97
    }
98

  
99 102
    // used by JTree to display this node
100 103
    public String toString ()
101 104
    {
......
131 134
    /** Add a new attribute to this element, or set its value */
132 135
    public void setAttribute(String attName, String attValue) {
133 136
      if (attName != null) {
137
        // Enter the attribute in the hash table
134 138
        attributes.put(attName, attValue);
139
 
140
        // And enter the attribute in the database
141
        try {
142
          PreparedStatement pstmt = conn.prepareStatement(
143
              "INSERT INTO xml_attributes(attributeid, nodeid, " +
144
              "attributename, attributevalue) " +
145
              "VALUES (null, ?, ?, ?)");
146

  
147
          // Bind the values to the query
148
          pstmt.setLong(1, getElementID());
149
          pstmt.setString(2, attName);
150
          pstmt.setString(3, attValue);
151
 
152
          // Do the insertion
153
          pstmt.execute();
154
          pstmt.close();
155
        } catch (SQLException e) {
156
          System.out.println(e.getMessage());
157
        }
158

  
135 159
      } else {
136 160
        System.err.println("Attribute name must not be null!");
137 161
      }

Also available in: Unified diff