Revision 8
Added by Matt Jones over 25 years ago
DBElement.java | ||
---|---|---|
16 | 16 |
import org.w3c.dom.DOMException; |
17 | 17 |
import org.w3c.dom.Element; |
18 | 18 |
import org.w3c.dom.Node; |
19 |
import org.w3c.dom.NodeList; |
|
19 | 20 |
import org.w3c.dom.ProcessingInstruction; |
20 | 21 |
import org.w3c.dom.Text; |
21 | 22 |
|
23 |
import java.sql.*; |
|
24 |
import java.io.IOException; |
|
22 | 25 |
|
23 |
public class DBElement extends ElementNode |
|
24 |
{ |
|
26 |
|
|
27 |
public class DBElement extends ElementNode { |
|
28 |
|
|
25 | 29 |
public DBElement () { |
26 | 30 |
super(); |
27 | 31 |
}; |
28 | 32 |
|
29 |
public String getJunk() { |
|
30 |
return "This is " + getTagName() + " junk!"; |
|
33 |
/** |
|
34 |
* Writes this element and all of its children out to the |
|
35 |
* given database connection |
|
36 |
*/ |
|
37 |
public void writeXmlToDB(Connection conn) throws IOException { |
|
38 |
/* |
|
39 |
if (attributes != null) |
|
40 |
attributes.writeXml(conn); |
|
41 |
*/ |
|
42 |
int maxid=0; |
|
43 |
Statement stmt; |
|
44 |
try { |
|
45 |
stmt = conn.createStatement(); |
|
46 |
stmt.execute("SELECT MAX(nodeid) FROM xml_elements"); |
|
47 |
try { |
|
48 |
ResultSet rs = stmt.getResultSet(); |
|
49 |
try { |
|
50 |
boolean tableHasRows = rs.next(); |
|
51 |
if (tableHasRows) { |
|
52 |
try { |
|
53 |
maxid = rs.getInt(1); |
|
54 |
} catch (SQLException e) { |
|
55 |
System.out.println("Error with getInt: " + e.getMessage()); |
|
56 |
} |
|
57 |
} |
|
58 |
} catch (SQLException e) { |
|
59 |
System.out.println("Error with next: " + e.getMessage()); |
|
60 |
} |
|
61 |
} catch (SQLException e) { |
|
62 |
System.out.println("Error with getrset: " + e.getMessage()); |
|
63 |
} |
|
64 |
stmt.close(); |
|
65 |
} catch (SQLException e) { |
|
66 |
System.out.println("Error getting id: " + e.getMessage()); |
|
67 |
} |
|
68 |
|
|
69 |
try { |
|
70 |
PreparedStatement pstmt = conn.prepareStatement( |
|
71 |
"INSERT INTO xml_elements(nodeid, nodename) values (?, ?)"); |
|
72 |
|
|
73 |
// Add an element as element number 1500 |
|
74 |
pstmt.setInt(1, maxid+1); // The first ? is for NODEID |
|
75 |
pstmt.setString(2, getTagName());// The second ? is for NODENAME |
|
76 |
// Do the insertion |
|
77 |
pstmt.execute(); |
|
78 |
pstmt.close(); |
|
79 |
|
|
80 |
} catch (SQLException e) { |
|
81 |
System.out.println(e.getMessage()); |
|
82 |
} |
|
83 |
|
|
84 |
if (hasChildNodes()) |
|
85 |
writeChildrenXmlToDB(conn); |
|
86 |
|
|
87 |
System.out.println("ID: " + maxid + "\tTag: " + getTagName()); |
|
31 | 88 |
} |
32 | 89 |
|
90 |
public void writeChildrenXmlToDB(Connection conn) throws IOException { |
|
91 |
|
|
92 |
NodeList children = getChildNodes(); |
|
93 |
if (children != null) { |
|
94 |
for (int i = 0; i < children.getLength(); i++) { |
|
95 |
if (children.item(i) instanceof ElementNode) { |
|
96 |
((DBElement)children.item(i)).writeXmlToDB(conn); |
|
97 |
} else { |
|
98 |
System.out.println(" Text Node skipped."); |
|
99 |
} |
|
100 |
} |
|
101 |
} |
|
102 |
} |
|
103 |
|
|
33 | 104 |
// used by JTree to display this node |
34 | 105 |
public String toString () |
35 | 106 |
{ |
... | ... | |
40 | 111 |
value.append ('>'); |
41 | 112 |
return value.toString (); |
42 | 113 |
} |
43 |
|
|
44 |
// TreeNode method implementations |
|
45 |
/* |
|
46 |
public TreeNode getChildAt (int n) { |
|
47 |
//System.out.println ("DBElement.getChildAt()"); |
|
48 |
Node node = item (n); |
|
49 |
|
|
50 |
if (node instanceof TreeNode) |
|
51 |
return (TreeNode) node; |
|
52 |
// Text, ProcessingInstruction ... |
|
53 |
return new MaskedNode (node); |
|
54 |
} |
|
55 |
*/ |
|
56 |
|
|
57 |
// |
|
58 |
// In this case we really _do_ want to show all the internal |
|
59 |
// nodes, so we really ought to use custom TreeNode and |
|
60 |
// ProcessingInstruction classes as we build. |
|
61 |
// |
|
62 |
/* |
|
63 |
static class MaskedNode implements TreeNode { |
|
64 |
private Node real; |
|
65 |
MaskedNode (Node e) { real = e; } |
|
66 |
|
|
67 |
public int getChildCount () { return 0; } |
|
68 |
public TreeNode getChildAt (int n) { return null; } |
|
69 |
public TreeNode getParent () |
|
70 |
{ return (TreeNode) real.getParentNode (); } |
|
71 |
public int getIndex (TreeNode node) { return -1; } |
|
72 |
public boolean getAllowsChildren () { return false; } |
|
73 |
public boolean isLeaf () { return true; } |
|
74 |
public Enumeration children () { return null; } |
|
75 |
|
|
76 |
public String toString () { |
|
77 |
if (real instanceof Text) |
|
78 |
return real.toString (); // XXX constructor |
|
79 |
if (real instanceof ProcessingInstruction) |
|
80 |
return real.toString (); // XXX unreasonable result |
|
81 |
return "... ? ..."; |
|
82 |
} |
|
83 |
} |
|
84 |
*/ |
|
85 |
/* |
|
86 |
public int getChildCount () { |
|
87 |
return getLength (); |
|
88 |
} |
|
89 |
|
|
90 |
public TreeNode getParent () { |
|
91 |
return (TreeNode) getParentNode (); |
|
92 |
} |
|
93 |
|
|
94 |
public int getIndex (TreeNode node) { |
|
95 |
return -1; // XXX implement |
|
96 |
} |
|
97 |
|
|
98 |
public boolean getAllowsChildren () { |
|
99 |
return true; |
|
100 |
} |
|
101 |
|
|
102 |
public boolean isLeaf () { |
|
103 |
return !hasChildNodes (); |
|
104 |
} |
|
105 |
|
|
106 |
public Enumeration children () { |
|
107 |
//System.out.println ("DBElement.children()"); |
|
108 |
|
|
109 |
throw new RuntimeException ("NYI"); |
|
110 |
} |
|
111 |
*/ |
|
112 | 114 |
} |
DBWriter.java | ||
---|---|---|
38 | 38 |
public class DBWriter |
39 | 39 |
{ |
40 | 40 |
private XmlDocument doc; |
41 |
private Connection conn; |
|
41 |
private Connection conn = null;
|
|
42 | 42 |
|
43 | 43 |
// Constructer to init the class |
44 | 44 |
public DBWriter(String argv[]) |
45 | 45 |
throws SAXException, SQLException, IOException, ClassNotFoundException { |
46 | 46 |
|
47 |
// |
|
48 |
// Load the document, using the appropriate custom |
|
49 |
// DOM elements. |
|
50 |
// |
|
51 |
XmlDocument doc = createDocument ( |
|
52 |
Resolver.createInputSource (new File (argv [1])), |
|
53 |
new FileInputStream (argv [0]), |
|
54 |
new ErrorPrinter () |
|
55 |
); |
|
56 |
|
|
57 |
DBElement root; |
|
58 |
|
|
59 |
root = (DBElement)doc.getDocumentElement (); |
|
60 |
root.normalize (); |
|
61 |
System.out.println(root.getJunk()); |
|
62 |
|
|
47 |
// Open a connection to the database |
|
63 | 48 |
conn = openDBConnection( |
64 | 49 |
"oracle.jdbc.driver.OracleDriver", |
65 | 50 |
"jdbc:oracle:thin:@penelope.nceas.ucsb.edu:1526:DEV", |
66 | 51 |
"jones", "kinkaj0u"); |
67 | 52 |
|
53 |
// THIS IS TEMPORARY |
|
68 | 54 |
// Prepare a statement to cleanup the emp table |
55 |
/* |
|
69 | 56 |
Statement stmt = conn.createStatement (); |
70 | 57 |
try { |
71 | 58 |
stmt.execute ("delete from xml_elements where nodeid = 1500"); |
72 | 59 |
} catch (SQLException e) { |
73 | 60 |
// Ignore an error here |
74 | 61 |
} |
75 |
|
|
62 |
*/ |
|
63 |
// THIS IS TEMPORARY |
|
76 | 64 |
// Prepare to insert new names in the Elements table |
65 |
/* |
|
77 | 66 |
PreparedStatement pstmt = conn.prepareStatement( |
78 | 67 |
"insert into xml_elements (nodeid, nodename) values (?, ?)"); |
79 | 68 |
|
80 |
// Add an element as employee number 1500
|
|
69 |
// Add an element as element number 1500
|
|
81 | 70 |
pstmt.setInt (1, 1500); // The first ? is for NODEID |
82 | 71 |
pstmt.setString (2, "eml-variable");// The second ? is for NODENAME |
83 | 72 |
// Do the insertion |
84 | 73 |
pstmt.execute (); |
74 |
*/ |
|
75 |
|
|
76 |
// |
|
77 |
// Load the document, using the appropriate custom |
|
78 |
// DOM elements. |
|
79 |
// |
|
80 |
XmlDocument doc = createDocument ( |
|
81 |
Resolver.createInputSource (new File (argv [1])), |
|
82 |
new FileInputStream (argv [0]), |
|
83 |
new ErrorPrinter () |
|
84 |
); |
|
85 |
|
|
86 |
DBElement root; |
|
87 |
|
|
88 |
root = (DBElement)doc.getDocumentElement(); |
|
89 |
root.normalize(); |
|
90 |
root.writeXmlToDB(conn); |
|
85 | 91 |
} |
86 | 92 |
|
87 | 93 |
// |
Makefile | ||
---|---|---|
11 | 11 |
#java -classpath ".$(SEP)$(CPATH)" \ |
12 | 12 |
#main ./book-order.xml |
13 | 13 |
|
14 |
# java -classpath ".$(SEP)$(CPATH)" \
|
|
15 |
# DBWriter DBElement.props ./book-order.xml
|
|
14 |
#java -classpath ".$(SEP)$(CPATH)" \
|
|
15 |
#DBWriter DBElement.props ./book-order.xml
|
|
16 | 16 |
|
17 | 17 |
clean: |
18 | 18 |
-rm -f *.class Log |
Also available in: Unified diff
element insertions added to db