Project

General

Profile

« Previous | Next » 

Revision 24

Added by Matt Jones about 24 years ago

completed basic DBReader functionality. Can now read XML from DB using DBReader

View differences:

DBReader.java
81 81
  public String readDocument(long nodeid) {
82 82
    StringBuffer doc = new StringBuffer();
83 83

  
84
    System.out.println("\nGetting document with nodeid: " + nodeid + "\n");
85
    //BasicElement element = readNodeFromDB(nodeid);
86 84
    ReaderElement element = new ReaderElement(conn, nodeid);
87
    doc.append(element);
85
    doc.append("<?xml version=\"1.0\"?>\n");
86
    doc.append(element.toString());
88 87

  
89 88
    return (doc.toString());
90 89
  }
91

  
92
  /** look up the assigned element id from DB connection */
93
  private BasicElement readNodeFromDB(long nodeid) {
94
      long element_id=0;
95
      long nodeparentid=0;
96
      String nodetype=null;
97
      String nodename=null;
98
      String nodedata=null;
99

  
100
      PreparedStatement pstmt;
101
      try {
102
        pstmt = 
103
          conn.prepareStatement("SELECT nodeid,nodeparentid,nodetype, " +
104
                  "nodename,nodedata FROM xml_nodes WHERE nodeid = ?");
105
        // Bind the values to the query
106
        pstmt.setLong(1, nodeid);
107

  
108
        pstmt.execute();
109
        try {
110
          ResultSet rs = pstmt.getResultSet();
111
          try {
112
            boolean tableHasRows = rs.next();
113
            if (tableHasRows) {
114
              try {
115
                element_id = rs.getLong(1);
116
                nodeparentid = rs.getLong(2);
117
                nodetype = rs.getString(3);
118
                nodename = rs.getString(4);
119
                nodedata = rs.getString(5);
120
              } catch (SQLException e) {
121
                System.out.println("Error with getInt: " + e.getMessage());
122
              }
123
            }
124
          } catch (SQLException e) {
125
            System.out.println("Error with next: " + e.getMessage());
126
          }
127
        } catch (SQLException e) {
128
          System.out.println("Error with getrset: " + e.getMessage());
129
        }
130
        pstmt.close();
131
      } catch (SQLException e) {
132
        System.out.println("Error getting id: " + e.getMessage());
133
      }
134

  
135
      BasicElement element = null;
136
      if (nodetype.equals("ELEMENT")) {
137
        element = new BasicElement(element_id,nodename,nodeparentid);
138
        element.appendContent(nodedata);
139
      }
140

  
141
      // Get a list of child nodes and recursively retrieve them as well
142
      try {
143
        pstmt = 
144
          conn.prepareStatement("SELECT nodeid,nodeparentid,nodetype, " +
145
                                "nodename,nodedata FROM xml_nodes " +
146
                                "WHERE nodeparentid = ?");
147
        // Bind the values to the query
148
        pstmt.setLong(1, nodeid);
149

  
150
        pstmt.execute();
151
        try {
152
          ResultSet rs = pstmt.getResultSet();
153
          try {
154
            boolean tableHasRows = rs.next();
155
            if (tableHasRows) {
156
              try {
157
                element_id = rs.getLong(1);
158
                nodeparentid = rs.getLong(2);
159
                nodetype = rs.getString(3);
160
                nodename = rs.getString(4);
161
                nodedata = rs.getString(5);
162
              } catch (SQLException e) {
163
                System.out.println("Error with getInt: " + e.getMessage());
164
              }
165
            }
166
          } catch (SQLException e) {
167
            System.out.println("Error with next: " + e.getMessage());
168
          }
169
        } catch (SQLException e) {
170
          System.out.println("Error with getrset: " + e.getMessage());
171
        }
172
        pstmt.close();
173
      } catch (SQLException e) {
174
        System.out.println("Error getting id: " + e.getMessage());
175
      }
176

  
177

  
178
      return element;
179
  }
180 90
}

Also available in: Unified diff