Home
Testing the SQLJ program
To create a test program to invoke the SQLJ program, do these steps:
![]()
In the Package Explorer right-click the package itso.bank.data.sqlj and select New Æ Java Class.
![]()
Enter TestSQLJ as the class name. Select public static void main(String[] args) and click Finish. Copy/paste the following code to TestSQLJ (Example | 1-5). The TestSQLJ.java code can be found in C:\7672code\database\samples.
Example 11-5 TestSQLJ.java
package itso.bank.data.sqlj;import java.math.BigDecimal;public class TestSQLJ {public static void main(String[] args) {try {CustomerAccountInfo info = new CustomerAccountInfo();info.execute(new BigDecimal(10000)); // minimum balance displayedwhile (info.next()) {System.out.println("Customer name: " + info.getCUSTOMER_TITLE()+ " " + info.getCUSTOMER_FIRST_NAME() + " "+ info.getCUSTOMER_LAST_NAME());System.out.println("Account ID: " + info.getACCOUNT_ID() +" Balance: " + info.getACCOUNT_BALANCE());System.out.println("---------------------------------------");}info.close();} catch (Exception e) {e.printStackTrace();}}}
![]()
![]()
We have to add the Derby JDBC driver library to the project build path:
![]()
Right-click project RAD75SQLJ and select Properties.
![]()
In the Java Build Path, Libraries tab, click Add External JARs and select derby.jar, which is in <RAD_HOME>\runtimes\base_v7\derby\lib. Click OK.
![]()
Derby accepts only one database connection at a time. Switch to the Data perspective. In the Database Explorer right-click the ITSOBANK connection and select Disconnect.
![]()
In the Java perspective, right-click TestSQLJ.java Æ Run As Æ Java Application.
![]()
The result is displayed in the console: Retrieve some data from the database.
Customer name: Mr Brian Hainey
Account ID: 005-555002 Balance: 72213.41
---------------------------------------
Customer name: Mr Ueli Wahli
Account ID: 000-000001 Balance: 66666.66
---------------------------------------
Customer name: Ms Pinar Ugurlu
Account ID: 002-222001 Balance: 65484.23
---------------------------------------
Customer name: Mr Juan Napoli
Account ID: 004-444003 Balance: 23156.46
---------------------------------------
Customer name: Mr Henry Cui
Account ID: 001-111001 Balance: 12545.67
---------------------------------------
......
Note: If you get the following exception:
Failed to start database 'C:/7672code/database/derby/ITSOBANK'This means that the Derby database is locked by another connection. In the Database Explorer, check whether the ITSOBANK connection is still active. If so, disconnect the connection, or restart the workbench.
ibm.com/redbooks