Monday, November 2, 2009

Project 7-1 Accessing the Database with Java


Team Fly 


Page 260



4. Checking for end of data.



5. Closing the cursor.



When writing Java database programs, a similar set of steps need to be performed:



1. Registering a driver.



2. Connecting to a database.



3. Executing a statement.



4. Fetching data. (The check for end of data is done during the fetch.)



5. Closing the resources.



Project 7-1 Accessing the Database with Java



JDBC is required for accessing databases from within Java. In this project, you will walk through the necessary JDBC steps for performing a simple query.



Step by Step


1. The first step in connecting to a database requires registering a driver. Oracle offers two different ways of registering a driver:




Class.forName(''oracle.jdbc.OracleDriver");
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());


2. Once a driver is registered, a connection needs to be made. Oracle data sources can be used to connect to a database, but data sources are more appropriately covered in a Java programming course. When connecting from outside, the database connection information is required.




Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@hostname:portnumber:sid",
"username", "password");


3. Now it's time to create the statement that will be run. This step takes you through the definition and execution of the SQL statement using Java. We include additional JDBC statements to execute statements. Statements that return records use result sets to process those records. You also need to keep in mind that variables that you have defined are compatible with your Oracle data. Now let's type in the commands that will define your



Team Fly 

No comments:

Post a Comment