![]() |
![]() |
![]() |
![]() |
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.
- Select Goto declaration code from the Code menu in the Composer.
- 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.)
Next, we will connect to the database server in the constructor of our class.
- Press the Code Sourcerer button.
- Choose 'File operations...' and press Next.
- Choose 'Create new file object from pathname...' and press Next.
- 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.
- Press the Code Sourcerer button.
- Choose 'File operations...' and press Next.
- Choose 'Get the directory from a File object...' and press Next.
- Leave the default value of 'theFile' and press Done.
- Leave the default value of 'dir' and press Ok. The Code Sourcerer will produce the following line of code.
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.
- Click once on the JTextField named 'query' to view its properties.
- Choose its Listeners page.
- Check 'Listen for Action events'. Select the new Action page which appears.
- Type the following code.
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 |
![]() |
![]() |
![]() |
![]() |