Installing and running the Java Data Base Connectivity (JDBC) with SimpleApp.java
This is designed for eclipse so I recommend downloading it from eclipse.org
- Create a new project (in eclipse)
- File → New → Java Project
- Type in a name (one word, no spaces, no special characters)
- below, I named it ‘JavaDB1’
- Click the Finish button
- Drag SimpleApp.java into the project
- On the left side of eclipse is the ‘Package Explorer’
- Click on the right arrow in from of the project name ‘JavaDB1’ (see above)
- Open up the until you see the ‘src’ (source) folder. Then drag ‘SimpleApp.java’ to the src folder. It may ask you if you want to move or copy the files. Copy is fine.
- Double-click on SimpleApp.java and the file will appear in the editor.
- Add the External JAR files
- from the menu, go to Project → Properties
- In the “Properties for project_name” configuration box (where project_name is the name of your project. Here, mine is called ‘JavaDB1’, so it reads “Properties for JavaDB1”, click on the ‘Libraries’ tab.
- Select ‘Java Build Path’ in the left column.
- Click on the ‘Add External JARs…’ button on the right.
- A new dialog box will open.
- Select the file ‘derby.jar’. If there are problems running the program, add ‘derbyclient.jar’. If problems continue, add ‘derbynet.jar’, ‘derbyrun.jar’ and ‘derbytools.jar’. However, ‘derby.jar’s should be all that is needed.
- Click the ‘OK’ button
- Run the program
- Click on the Green ‘run’ button. Eclipse does the compile and run as a single step.
- If all goes well, the program will display the following
SimpleApp starting in embedded mode
Loaded the appropriate driver
Connected to and created database derbyDBCreated table location
Inserted 1956 Webster
Inserted 1910 Union
Updated 1956 Webster to 180 Grand
Updated 180 Grand to 300 Lakeshore
Verified the rows
Dropped table location
Committed the transaction
Derby shut down normally
SimpleApp finished - And that’s it!
- The database itself
- In the code, there is this line
s.execute("create table location(num int, addr varchar(40))");</li>
- This line creates a table named ‘location’ with two values, an integer named ‘num’ and
a string of up to 40 characters named ‘addr’. - These 3 lines insert ‘1956’ as integer number 1 and ‘Webster St.’ as the string. The last statement executes the SQL command.
psInsert.setInt(1, 1956); psInsert.setString(2, "Webster St."); psInsert.executeUpdate();</li>
- The update commands are as follows:
psUpdate.setInt(1, 180); psUpdate.setString(2, "Grand Ave.");
- And that is how SQL works. Now, with a few changes, you can include the course project into this.
- In the code, there is this line