Professional Java.JDK.5.Edition (Wrox)
.pdfChapter 4
try { Class.forName(“org.gjt.mm.mysql.Driver”); Connection conn =
DriverManager.getConnection(“jdbc:mysql://localhost/annotationtest”,
“”, “”);
PreparedStatement preparedStmt =
conn.prepareStatement(“SELECT * FROM PERSON WHERE FILENAME = ?”); preparedStmt.setString( 1, filename.toString());
ResultSet result = preparedStmt.executeQuery(); ArrayList list = new ArrayList();
while (result.next()) {
AnnotationPersonRecord person = new AnnotationPersonRecord(); person.setFirstName(result.getString(2)); person.setMiddleName(result.getString(3)); person.setLastName(result.getString(4)); person.setDob(result.getString(5)); person.setComments(result.getString(6)); person.setFilename(result.getString(7)); person.setAnnotationStart(result.getString(8)); person.setAnnotationEnd(result.getString(9)); list.add(person);
}
if (list != null) {
for (int i=0; i < list.size(); i++) { AnnotationPersonRecord element =
(AnnotationPersonRecord)list.get(i);
}
AnnotationPrint.printArrayS( list );
}
}catch (Exception e) { logger.info(“Exception: “ + e.toString());
}
}
}
The createDocument(ResultSet rs) method works in conjunction with the XMLAction class to export annotation data to an XML file upon user request. Libraries are used from the dom4j package to craft an XML file artifact with the annotation text. The addElement method adds a new Element node to the topic branches of the document data structure:
static public org.dom4j.Document createDocument(ResultSet rs) {
org.dom4j.Document document = org.dom4j.DocumentHelper.createDocument(); org.dom4j.Element root = document.addElement( “Annotation” )
.addAttribute(“text”, “Default annotation”)
.addAttribute(“value”, “default”);
try {
org.dom4j.Element topic = null; int item=0;
while (rs.next()) { ++item;
topic = root.addElement( “person” ).addAttribute( “value”, “item” ).addAttribute( “text”, String.valueOf(item));
206