KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > RowInserterRule


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 import java.util.Map JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 /**
21  * See Main.java.
22  */

23 public class RowInserterRule extends org.apache.commons.digester.Rule {
24
25     private java.sql.Connection JavaDoc conn;
26     
27     public RowInserterRule(java.sql.Connection JavaDoc conn) {
28         this.conn = conn;
29     }
30     
31     /**
32      * This method is invoked when the start tag for an xml element representing
33      * a database row is encountered. It pushes a new Row instance onto the
34      * digester stack (rather like an ObjectCreateRule) so that column data
35      * can be stored on it.
36      */

37     public void begin(String JavaDoc namespace, String JavaDoc name, org.xml.sax.Attributes JavaDoc attrs) {
38         digester.push(new Row());
39     }
40     
41     /**
42      * This method is invoked when the end tag for an xml element representing
43      * a database row is encountered. It pops a fully-configured Row instance
44      * off the digester stack, accesses the object below it on the stack (a
45      * Table object) to get the tablename, then does an SQL insert). Actually,
46      * here we just print out text rather than do the sql insert, but the
47      * real implementation should be fairly simple.
48      * <p>
49      * Note that after this rule completes, the row/column information is
50      * <i>discarded</i>, ie this rule performs actions <i>as the input is
51      * parsed</i>. This contrasts with the more usual way digester is used,
52      * which is to build trees of objects for later use. But it's a perfectly
53      * valid use of Digester.
54      */

55     public void end(String JavaDoc namespace, String JavaDoc name) {
56         Row row = (Row) digester.pop();
57         Table table = (Table) digester.peek();
58
59         // Obviously, all this would be replaced by code like:
60
// stmt = conn.prepareStatement();
61
// stmt.setString(n, value);
62
//
63
// Many improvements can then be implemented, such as using the
64
// PreparedStatement.getParameterMetaData method to retrieve
65
// retrieve parameter types, etc.
66

67         StringBuffer JavaDoc colnames = new StringBuffer JavaDoc();
68         StringBuffer JavaDoc colvalues = new StringBuffer JavaDoc();
69         
70         for(Iterator JavaDoc i = row.getColumns().iterator(); i.hasNext();)
71         {
72             Row.Column column = (Row.Column) i.next();
73             
74             if (colnames.length() > 0)
75             {
76                 colnames.append(", ");
77                 colvalues.append(", ");
78             }
79         
80             colnames.append("'");
81             colnames.append(column.getName());
82             colnames.append("'");
83             
84             colvalues.append("'");
85             colvalues.append(column.getValue());
86             colvalues.append("'");
87         }
88
89         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
90         buf.append("insert into ");
91         buf.append(table.getName());
92         buf.append(" (");
93         buf.append(colnames.toString());
94         buf.append(") values (");
95         buf.append(colvalues.toString());
96         buf.append(")");
97         
98         // here the prepared statement would be executed....
99
System.out.println(buf.toString());
100     }
101 }
102
Popular Tags