Java Reference
In-Depth Information
stream.append("\n </LAYER>");
return
stream.toString();
}
}
Class
DBConnector
loads the list of thematic databases and the list of avail-
able tables from the GIS database. It offers methods to iterate through these
lists and to retrieve selected records from the thematic databases. It uses
class
DBListRecord
, class
DBTableRecord
and class
DBValueRecord
(see
Figure 15.11 on p. 417) to store a record extracted from the list of databases,
the list of tables and the selected thematic table respectively.
import
java.sql.*;
import
java.util.*;
public class
DBConnector {
// records extracted from "Databases" and from "Tables"
private static
ArrayList dbList
#
new
ArrayList();
private static
ArrayList tableList
#
new ArrayList();
public static
void openDataBase() {
try
{
// Load the jdbc-odbc bridge driver
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
String dbName
#
"jdbc:odbc:Gis";
Connection con
#
DriverManager.getConnection (dbName);
// Create a Statement object so we can submit
// SQL statements to the driver
Statement stmt
#
con.createStatement ();
// Extract the information about available databases
String query
#
"SELECT ID, Name, Description"
!
"FROM Databases";
ResultSet rs
#
stmt.executeQuery (query);
// Load data, fetching until end of the result set
dbList.removeAll(dbList);
while
(rs.next())
// Loop through each column, getting the column data
dbList.add(
new
DBListRecord(rs.getLong("ID"),
rs.getString("Name"),rs.getString("Description")));
// Close the result set
rs.close();
// Extract the information about available tables
query
#
"SELECT ID, IDDataBase, Name, Description, "
!
"Key, Label FROM Tables";
rs
#
stmt.executeQuery (query);
// Load data, fetching until end of the result set
tableList.removeAll(tableList);
while
(rs.next())
// Loop through each column, getting the column data