TOC PREV NEXT INDEX



Using JDBC (Java Database Connectivity)


We will now add a few lines of code which will connect our application to a database using JDBC. In this application we will use the SimpleTextTM Database Server and JDBC Driver. (The SimpleText database is a free database server and JDBC implementation from Thought, Inc. written completely in Java. SimpleText is included with this tutorial so that the tutorial will operate consistently everywhere. We wish to thank Thought, Inc. for allowing us to redistribute SimpleText.)

To start we will need some variables to store data.

  1. Select Goto declaration code from the Code menu in the Composer.
  2. Type the following code. (All of the text after the "//" is a comment. It is provided to help you understand the code. You don't have to type it if you don't want to.)

// import various standard Java classes
import java.sql.*;
import java.util.*;

Connection db; // The JDBC connection
Statement statement; // an SQL statement
ResultSet rs; // an SQL ResultSet
ResultSetMetaData meta; // meta data describing the results
Vector data; // storage for the results

Next, we will connect to the database server in the constructor of our class.

  1. Select Goto constructor code from the Code menu in the Composer.
  2. Type the following code.

try {
// Load the database driver
new jdbc.SimpleText.SimpleTextDriver();
  1. Press the Code Sourcerer button.
  2. Choose 'File operations...' and press Next.
  3. Choose 'Create new file object from pathname...' and press Next.
  4. Press the Browse button. Browse to /Project/Tutorial4/Databases/ADDRESS.SBF in the Simplicity install directory. Press Done. The Code Sourcerer will produce the appropriate code.
  5. Press the Code Sourcerer button.
  6. Choose 'File operations...' and press Next.
  7. Choose 'Get the directory from a File object...' and press Next.
  8. Leave the default value of 'theFile' and press Done.
  9. Leave the default value of 'dir' and press Ok. The Code Sourcerer will produce the following line of code.
String dir = theFile.getParent();

  1. Type the following code.
Properties prop = new Properties();
prop.put("Directory",dir);

// Connect to the database
db = DriverManager.getConnection("jdbc:SimpleText",prop);
statement = db.createStatement();
} catch ( SQLException e ) {
}
  1. Choose 'Initialize Class' from the Program menu to initialize the code you just typed.

We want to allow the user to type any valid SQL query in the JTextField and display the results of the query in the JTable.

  1. Click once on the JTextField named 'query' to view its properties.
  2. Choose its Listeners page.
  3. Check 'Listen for Action events'. Select the new Action page which appears.
  4. Type the following code.

// try to execute the query. If it fails, notify the user.
try {
// execute whatever the user typed in query
rs = statement.executeQuery(query.getText());
meta = rs.getMetaData(); // get the Meta Data
data = new Vector(); // initialize the data storage
while( rs.next() ) {
for(int i=0;i<meta.getColumnCount();i++) {
// store the data in row order
data.addElement(rs.getString(i+1));
}
}
} catch ( Exception e ) {
getToolkit().beep(); // bad query
System.out.println("Bad SQL query: "+e.getMessage());
data = null; // indicate failure
}
// tell the table to update its contents
((AbstractTableModel)table.getModel())
.fireTableStructureChanged();

Test this code by typing "select * from address" in the TextField, and then press enter. If any error occurs, a message will displayed.


Data Representations, Inc.
http://www.datarepresentations.com
support@datarepresentations.com
sales@datarepresentations.com
TOC PREV NEXT INDEX