Using XML in a Java Context - Summary
(Page 5 of 5 )
Today, you learned about working with data stored in popular database formats such as Microsoft Access and MySQL. Using either Java Database Connectivity (JDBC) or a combination of JDBC and ODBC, you can incorporate existing data-storage solutions into your Java programs.
You can connect to several different relational databases in your Java programs by using JDBC or ODBC and Structured Query Language (SQL), a standard language for reading, writing, and managing a database.
You also learned the basics of another popular format for data representation: Extensible Markup Language (XML).
In many ways Extensible Markup Language is the data equivalent of the Java language. It liberates data from the software used to create it and the operating system the software ran on, just as Java can liberate software from a particular operating system.
By using a class library such as the open source XML Object Model (XOM) library, you can easily create and retrieve data from an XML file.
A big advantage to representing data using XML is that you will always be able to get that data back. If you decide to move the data into a relational database or some other form, you can easily retrieve the information.
You also can transform XML into other forms such as HTML through a variety of technology, both in Java and through tools developed in other languages.
Q&A
Q. Can the JDBC-ODBC bridge driver be used in an applet?
A. The default security in place for applets does not allow the JDBC-ODBC bridge to be used because the ODBC side of the bridge driver employs native code rather than Java. Native code can't be held to the security restrictions in place for Java, so there's no way to ensure that this code is secure.
JDBC drivers that are implemented entirely in Java can be used in applets, and they have the advantage of requiring no configuration on the client computer
Q. Why is Extensible Markup Language called XML instead of EML?
A. None of the founders of the language appears to have documented the reason for choosing XML as the acronym. The general consensus in the XML community is that it was chosen because it "sounds cooler" than EML. Before anyone snickers at that distinction, Sun Microsystems chose the name Java for its programming language using the same criteria, turning down more technical-sounding alternatives such as DNA and WRL.
There is a possibility that the founders of XML were trying to avoid confusion with a programming language called EML (Extended Machine Language), which predates Extensible Markup Language.
Quiz
Review today's material by taking this three-question quiz.
Questions
What does a Statement object represent in a database program?
A connection to a database
A database query written in Structured Query Language
A data source
Which Java class represents SQL statements that are compiled before they are executed?
Statement
PreparedStatement
ResultSet
When all the start element tags, end element tags, and other markup are applied consistently in a document, what adjective describes the document?
Validating
Parsable
Well-formed
Answers
b. The class, part of the java.sql package, represents an SQL statement.
b. Because it is compiled, PreparedStatement is a better choice when you're going to execute the same SQL query numerous times.
c. For data to be considered XML, it must be well-formed.
Certification Practice
The following question is the kind of thing you could expect to be asked on a Java programming certification test. Answer it without looking at today's material or using the Java compiler to test the code.
Given:
public class ArrayClass {
public static ArrayClass newInstance() {
count++;
return new ArrayClass();
}
public static void main(String arguments[]) {
new ArrayClass();
}
int count = -1;
}
Which line in this program prevents it from compiling successfully?
count++;
return new ArrayClass();
public static void main(String arguments[]) {
int count = -1;
The answer is available on the book's Web site at http://www.java21days.com. Visit the Day 20 page and click the Certification Practice link.
Exercises
To extend your knowledge of the subjects covered today, try the following exercises:
Modify the CoalTotals application to pull fields from the Country Oil Totals table instead of the Coal table.
Create two applications: one that retrieves records from a database and produces an XML file that contains the same information and a second application that reads data from that XML file and displays it.
Where applicable, exercise solutions are offered on the book's Web site at http://www.java21days.com.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter 20 of the book Sams Teach Yourself Java 2 in 21 Days, 4th Edition, written by Rogers Cadenhead and Laura Lemay (Sams; ISBN: 0672326280). Check it out today at your favorite bookstore. Buy this book now.
|
|