Oracle Java Application Tutorial
Oracle Java Application Tutorial
In this tutorial I will guide you through the steps to create your own Java Database Application using an Oracle Database. To create a Java Application you will need at least some version of Java on the machine that you are working on. To make this a more practical effort you should also use a Java IDE. To create a Database Application you will also need a database. This tutorial will use the “Oracle Database 10g Express Edition”. This database provides all the functionality of a full commercial Oracle Database for an individual working on a local machine work free of charge.
1) Setup Decisions
Evaluate the machine that you will use for your project.
i. Do you have the correct Java?
You need a Java SDK (standard development kit) and a Java Application Server.
The most commonly available and supported components are generally in Java 2.
If you do not have Java on your machine download J2EE from sun which will install the current updated Java 2 environment including the SDK and an Application Server.
J2EE link:
ii. Do you have an Oracle Database?
If you do:
You will need the server name and port number for it as well as a user name and password.
If you don’t:
Download “Oracle Database 10g Express Edition” from Oracle and install it. Using the installed administrator account create a user account with appropriate permissions.
“Oracle Database 10g Express Edition” link:
iii. Do you have a Java IDE?
You can choose to write your java classes by hand in a text editor and compile them to create your application. With modern IDEs freely available this is a pretty senseless waste of time.
In this tutorial I will show you the main steps to create your Oracle Java Application using the NetBeans 5.5 Java IDE. If you have NetBeans on your machine then just follow the steps shown. If you don’t have it, download it here:
NetBeans 5.5.1 link:
(If you have your own favorite Java IDE then you can also try to replicate the steps in this tutorial in it.)
iv. Do you have the appropriate Java Oracle drivers?
These come in the form of java libraries (packages) that can be imported in your java programs. Example (after the first line in your code which is the package declaration, after your import statements you can define your classes):
import oracle.sql.*;
For our purposes the “Oracle Database 10g Express Edition” database can be driven with the following libraries:
import java.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.*;
import oracle.sql.*;
Check to see if you can import these libraries with your IDE because it may have been included with your Java environment. (Another reason why IDEs are good.)
If not, then download an implementation of these libraries in the form of a jar file and add a reference to it in your project before you try importing them in your code.
Oracle JDBC library link:
(Get the ojdbc.jar file (should be around 1,378,346 bytes))
Now you have all the components you need to implement any idea as a Java Oracle Database Application. (All free and available with public support to play with!)
In this tutorial, our idea is a way for us, as a startup bookstore, to manage our inventory.
2) Implementation Steps
Our example intends to demonstrate how to complete SQL transactions between a Java application and our Oracle database.
The extent of the specification to start us on this tutorial project is the following:
This Tutorial Project Specification:
We will make a simple database of book entries that we will search by title in our application. Our books database will consist of one table; with the following fields:
| |Title |Edition |Publisher |Copyright |Price |
| | | | | | |
Step 1: Create Example Books Database
1. Use the Oracle Database administrator with the user account created for yourself to create a books item table:
Logged in as user: SYS with password from installation
[pic]
Create a user – it should be self-explanatory so I will skip further image cuts.
2. Fill in example data into our new books database:
Log in as the user you just created.
Choose the Object Browser.
Create new Table
Add all the columns from the specification from above.
Add one more called Unique_ID and set it to be your primary key.
(To do this you have make sure that it cannot be null;
Then choose: “Populated from a new sequence”)
For a primary key select your extra unique id designated column.
Under constraints make sure that it is unique.
Create new table
Add dummy data
Here is what our example database table looks like:
[pic]
Step 2: Create our Java Application Project
1. Create new Java Application Project
Choose Project
Choose a General – Java Application
[pic]
Name and Location
Choose a name for your project and uncheck the Create Main class option.
[pic]
Adjust location to a place you know so if you have to come back to your work then you know how. (In a school lab you might want to set this location to a shared drive where you have access to a folder.)
2. Design User Interface and Project Components
Step 1: Add the components you need to your project
In my example I named my project, OraJavaBooksClient. (Name your own.)
For our application we will use a user interface, a data interface, and an error component. (Aside from the Main which is required by all Java applications.) These are:
BooksClientFrame.java
DataHandler.java
Error.java
Main.java
[pic]
For our user interface I used some JLabels, a JList, and a JTextArea. You can design your own interface by selecting the components you want from the Palette and dragging it to the design area. By right clicking on a component in your design area you can also let the IDE generate the methods associated for that control and place it into your code. (This is only a method definition that is empty.)
An empty shell actionPerformed method used by our Submit button can be generated by selecting Events and then Action from the items right click menu.
[pic]
This is our Error form that displays different error messages from our application.
[pic]
The following is the code for our application components:
Java Code:
BooksClientFrame.java
Refer to file called BooksClientFrame.java that came along with this tutorial.
Error.java
Refer to file called Error.java that came along with this tutorial.
3. Database Connectivity
Here I encourage you to make your own designs in the implementation of your version of this example. To connect to data in a database from an application you will need the following elements:
• A connection string – specific to our drivers and database (user/pass)
• A connection object – which we can close or open
• An SQL data-reader
In my example I created a DataHandler object, which takes care of connectivity and gives me an interface using the above elements.
For our Books database interface we use the DataHandler class. For our demonstration we need at least a way to get a connection and a way to read the data. getDBConnection() and DBRead(string stmt) fulfills this purpose. getDBConnection() setups a connection and DBRead(string stmt) returns an SQL result set object based on the “stmt” argument, which in our case is an SQL command made by the user interface form object (our BooksClientFrame.java class).
Here is the code for our DataHandler project package item (our database interface class):
DataHandler.java
Refer to file called DataHandler.java that came along with this tutorial.
Once we have all the pieces we build the solution with the green triangle button on the IDE.
3) Debug and Demo
Build Application:
In NetBeans just press the green triangle button (without the red square in front of it; which is the debug build version – I recommend trying this though, placing breakpoints in your code and using the IDE’s debugging mode to step through your code line by line (if user command is next then the debugger halts and waits for you to enter something into your application). This allows you to check variables values in an actively running instance and see what you are doing.)
Fix Build & Debug:
The highlighted statement in the editor (where the arrow is) is where execution is halted and you have full control of the IDE in debug mode (explore this if you have time). As you can see the “Watches” tab is highlighted in which I added by hand the rs.getString(1). During the first instance of the program halting by my breakpoint the value of it can be seen. Using the blue square buttons you can step through execution. Pressing the continue button continues execution until it hits the next instance of our breakpoint or some other breakpoint in our code.
[pic]
Running the Application:
Our java application after load (a build and a run; which is what the green triangle does):
[pic]
As you can see the application displays all the items currently in our Books Oracle database upon load.
Our java application after a selected input submission:
[pic]
As you can see it only displays the items with that the inputted name, “How to Program C#”. The only visible result here is that you can see that there are 3 of those items in the database.
(In a more real world scenario you would be interested in displaying other things as well along with other functionality.)
4) Review
There is one serious potential structural problem. This is due to an omission in our tutorial example. If this example were expanded into a larger application, the developer might run into hard to understand run-time errors. Can you guess what this omission could be in our tutorial?
(The page after the Appendix section of this tutorial will have your answer.)
Appendix
The answer to the question from the Review section above is we are not closing our database connection after we use it, explicitly. Can you figure out where it needs to go?
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- oracle java application tutorial
- introduction claros data management hach
- database connection string to mysql database on a
- use case specification import schema from database
- oracle imaging and process management services
- test case import schema from oracle database
- java database connectivity
- accessing oracle using ado in delphi sunvisor
Related searches
- java collections tutorial with examples
- download java tutorial pdf
- core java tutorial pdf
- java application example
- java application examples pdf
- java application source code
- java programming tutorial for beginners
- java tutorial hello
- java programming tutorial pdf
- java basic tutorial pdf
- java programming language tutorial pdf
- java programming tutorial pdf free