Building a Database

Example 18-3 shows a program, MakeAPIDB, that takes a list of class names and uses the Java Reflection API to build a database of those classes, the packages they belong to, and all methods and fields defined by the classes. Example 18-4 shows a program that uses the database created by this example.

MakeAPIDB uses the SQL CREATE TABLE statement to add three tables, named package, class, and member, to the database. The program then inserts data into those tables using INSERT INTO statements. The program uses the same INSERT INTO statements repeatedly, as it iterates though the list of class names. In this type of situation, you can often increase the efficiency of your insertions if you use PreparedStatement objects to execute the statements.

A prepared statement is essentially a blueprint for the statements you need to execute. When you send a SQL statement to the database, the database interprets the SQL and creates a template for executing the statement. If you are sending the same SQL statement repeatedly, only with different input parameters, the database still has to interpret the SQL each time. On database platforms that support prepared statements, you can eliminate this inefficiency by sending a prepared statement to the database before you actually make any calls to the database. The database interprets the prepared statement and creates its template just once. Then, when you execute the prepared statement repeatedly with different input parameters, ...

Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.