![]() |
![]() |
![]() |
![]() |
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.
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 |
![]() |
![]() |
![]() |
![]() |