Вы находитесь на странице: 1из 32

##################>### #############################

%########################### ###!
###"###

#'` ###################0####bjbj{P{P##################
###4b###:###:##(############################################################
#############t#######t###t#######t#######t#######t#######t###############N###4####
8#######8#######8#######8##T###Z8##l##########I#####8##L####9#######9#######9##
#####9######M:######M:######M:######TI######VI######VI######VI######VI######VI#####
#VI##$###uK##h###M#####zI######################t#######:######################M:
######M:######:######:######zI##############t#######t########9###############9##/
###I######<######<######<######:#####t########9######t########9######TI######
########<######################################################:######TI#########
#####<######<#####F##h###t#######t#############################################
##################G#######9######8##

###|
"{##########8######;######G##############H#####I##0###I######$G#####sN###
###b<#####sN##
###G##############################################################################
sN##############t#######G#####M:######a:######<######o:##

###{:##S###################################M:######M:######M:######zI######zI######
################################<##
###################################M:######M:######M:######I######:######:######
:######:###############################
###8############################8###################J#######t#######t#######t
#######t#######t#######t#############

###################################################################################
###################################################################################
###################################################################################
###################################################################################
###################################################################################
###################################################################################
#################################################################################Or
acle Java Application TutorialIn 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. Setup DecisionsEvaluate the machine that you
will use for your project. 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:#HYPERLINK
"https://sdlc5c.sun.com/ECom/EComActionServlet;jsessionid=55115726FB21F17C8D1652ADA
1F4EE06"#https://sdlc5c.sun.com/ECom/EComActionServlet;jsessionid=55115726FB21F17C8
D1652ADA1F4EE06#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 dont:
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:#HYPERLINK
"http://www.oracle.com/technology/software/products/database/xe/index.html"#http://
www.oracle.com/technology/software/products/database/xe/index.html# 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 dont have it,
download it here:NetBeans 5.5.1 link:#HYPERLINK
"http://www.netbeans.info/downloads/index.php"#http://www.netbeans.info/downloads/i
ndex.php# (If you have your own favorite Java IDE then you can also try to
replicate the steps in this tutorial in it.)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:#HYPERLINK
"http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/jdbc101040.ht
ml"#http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/jdbc101040
.html#
(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.Implementation StepsOur 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##<item>#######Step 1: Create Example Books
DatabaseUse 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#Create a user it should be self-explanatory so I will skip further
image cuts.Fill in example data into our new books database:Log in as the user you
just created.Choose the Object Browser.Create new TableAdd 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 dataHere is what our example database table looks like:#Step 2: Create
our Java Application ProjectCreate new Java Application ProjectChoose ProjectChoose
a General Java Application#Name and LocationChoose a name for your project and
uncheck the Create Main class option.#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.)Design
User Interface and Project ComponentsStep 1: Add the components you need to your
projectIn 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.javaDataHandler.javaError.javaMain.java#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.#This is our Error form that displays different error
messages from our application.#The following is the code for our application
components:Java Code:BooksClientFrame.javaRefer to file called
BooksClientFrame.java that came along with this tutorial.Error.javaRefer to file
called Error.java that came along with this tutorial.Database ConnectivityHere 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 openAn SQL data-readerIn 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.javaRefer 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.Debug and DemoBuild
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 IDEs 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.#
Running the Application:Our java application after load
(a build and a run; which is what the green triangle does):#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:#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.)ReviewThere 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?
###################################################################################
###################################################################################
###################################################################################
########################################################################"########
#
##9
##E
##]
##g
##i
##j
##k
##p
##y
##z
##{
##|
##
##
##
###### ###>###L###U###V#######

##vfWKWKWKWK#############h1C#CJ##OJ##QJ##aJ#####h#i##hi##CJ##OJ##
QJ##aJ#####ha##hi##>*#CJ##OJ##QJ##aJ####hi###hi##CJ##OJ##QJ##aJ#####hi##CJ##O
J##QJ##aJ#####h###hW##5#CJ##OJ##QJ##aJ####h###h#i#5#CJ##OJ##QJ##aJ####hA#Y#
CJ##OJ##QJ##aJ#####h&)#CJ##OJ##QJ##aJ#####hW##CJ##OJ##QJ##aJ#####hwG#CJ##OJ##QJ#
#aJ#####h'##CJ##OJ##QJ##aJ###$#hSz##h'##CJ##OJ##QJ##aJ##mH

#sH

######!###"###j
##k
##{
##|
##
##
##"###p####

##!

##

##

##

###########l##m##n##|
############################################################################
#############################################################################
#############################################################################
###########################################p#^p#gdi#######$#a$#gda#######
^#gdi#####
&##F##gdi######gdi######gdi#####
&##F##gd'######gd'#######$#a$#gd'########0####################################
###################################################################################
###################################################################################
###################################################################################
###################################################################################
###################################################################################
#######################################################

###

###

##

##!

##"

##

##

##

##

##

##

#####l##n##%###Q###R########################

#####################################,######0###
qee#####h&)#CJ##OJ##QJ##aJ#####h#i##ha#C
J##OJ##QJ##aJ###
#h+y##ha#0J##CJ##OJ##QJ##aJ#####h*#CJ##OJ##QJ##aJ#####ha##hi##>*#CJ##OJ##QJ
##aJ####ha##hi##CJ##OJ##QJ##aJ###
#ha##ha#0J##CJ##OJ##QJ##aJ#####hkA####j#####hkA#U####hi##CJ##OJ##QJ##aJ#####
h#i##hi##CJ##OJ##QJ##aJ#####ha#CJ##OJ##QJ##aJ##'|##
%###Q#########################/##################>###Q###g#########7##
##########################################################################
#############################################################################
############################################################################
############################################################################p
#^p#gd)J#######^#gda####
&##F##gdi#######p#^p#gdi######gda#######^#gdi####0#################
#6##################(###=###>###g#####################6###7###L###q####
####################]###^############
}
}}uqu`u}

###
#h+y##h&f#0J##CJ##OJ##QJ##aJ#####hkA####j#####hkA#U####h&f#CJ##OJ##QJ##aJ###'#
hSz##h)J#6#CJ##OJ##QJ##aJ##mH

#sH

###h)J##hi##6#CJ##OJ##QJ##aJ####h)J##h)J#6#CJ##OJ##QJ##aJ####h)J#CJ##OJ##QJ#
#aJ#####ha##hi##>*#CJ##OJ##QJ##aJ####h###hi##CJ##OJ##QJ##aJ#####ha#CJ##OJ##
QJ##aJ#####hi##CJ##OJ##QJ##aJ##
%#############################################################
##########~########################|
o_S##############hiP7#CJ##OJ##QJ##aJ#####h###h##5#CJ##OJ##QJ##aJ####h##5
#CJ##OJ##QJ##aJ####h###hi##5#CJ##OJ##QJ##aJ####h###h##CJ##OJ##QJ##aJ#####h
&)#CJ##OJ##QJ##aJ#####hi##CJ##OJ##QJ##aJ#####hi###hi##CJ##OJ##QJ##aJ###

#h&f#6#]####h###h&f#6#CJ##OJ##QJ##aJ####h&f#CJ##OJ##QJ##aJ#####h##CJ##OJ#
#QJ##aJ#####h#i##h&f#CJ##OJ##QJ##aJ####################################
########################################################################
#############################################################################
##############################################################################
###################################################################################
#########################################$##d#######$#If####a$#gdA#Y#######^#
gd#####
&##F##gd######gd######gdi######gdi#################################
#########q##########################5###Y###Z###t###u###D###a###b###d#######
###########vj^j^#####################h/#CJ##OJ##QJ##aJ#
####h#D#CJ##OJ##QJ##aJ#####h+I##0J##OJ##QJ##^J#####h#D#0J##OJ##QJ##^J#####h+I##CJ
##OJ##QJ##aJ#####h.##hv*{#CJ##OJ##QJ##aJ###
#jT####h#t###hA#Y#U##mH##nH##u#####hY@##CJ##OJ##QJ##aJ#####hv*{#CJ##OJ##QJ##aJ###

#hA#Y##hci####hA#Y##hci#CJ##OJ##QJ##aJ#####hiP7#CJ##OJ##QJ##aJ#####h.#CJ##OJ##Q
J##aJ##!
###############8############'############'############'######################$
##d#######$#If####a$#gdA#Y###kd#####$##$#If#####l############################
##d####k####r################################################################
###################################################
t####0#####################################################6################
#############################################################
########4#######4###
#l#a##ytA#Y######################################################'#######
#############kd####$##$#If#####l##############################d####k####r###
##############################################################################
##################################
t####0#####################################################6################
#############################################################
########4#######4###
#l#a##ytA#Y#####$##d#######$#If####a$#gdA#Y#############q###r###s###t###u###v
###w#################4###5###Z###u############6###d#########################
############################################################################
#############################################################################
#############################################################################
###################
######^#gd+I########gd+I######gd.#####gdv*{#######^#gd.#####gdv*{##
##
&##F##gd.#####gdi#######$#a$#gd.###d#####################+######.###/###0###1###2###3###4###5###a######################################
############################################################################
#############################################################################
############################################################################
###################################################################gdY#####
&##F##gd#e#####gd#e#######gd+I########gd/#######gd+I###########+###,######.###5###`###a##############################'###
} dWH

####################################################h*##hY##CJ##OJ##QJ##aJ####
#h*#6#CJ##OJ##QJ##aJ##0#j####hA#Y##hA

Вам также может понравиться