JDBC Setup and Writing first program

Prerequisites:
SQL
Core Java

Installing and Configuring stuff…
Oracle Database Setup
1) Download and install Oracle . This will take so much time and also hard disk memory.
2) Brushup basic commands like creating,alter,insert,select, etc
3) you can even try downloading Toad(This tool will expire in 30 days) or Aqual Data Studio (this tool will provide free license for personal use) . In real time projects most of the companies use this kind of tools instead of plain old PL/SQL editor.
4) Repeat step2 with the new tool.

JDBC Drivers
1) Download jdbc drivers for Oracle9i… I installed Oracle9.2.0.1 and so I downloaded  ojdbc14.jar 
2)The next step is configuring DSN
Go to controlpanel->administrativetools->ODBC sources->userDSN tab
click on “add” button –> select oracle92
fill the fields and create userDSN . I filled with my name for Data Source Name and desc …
and TNS name is what we gave during oracle installation and finally userid “scott” password “tiger”)
3) To use JDBC in Eclipse IDE we have to add the jar files to project build path
Go to Eclise IDE –>create a new project –> right click project and open properties window –> java buildpath –> add external jars–> select the driver location(ojdb14.jar) .
With this we are all set to write JDBC programs in Eclipse IDE

Writing Fisrt JDBC program:
1) Import required packages
import java.sql.*; //for JDBC package
import java.math.* //math package

optional as these r not required for current program
oracle.jdbc.driver.*
and oracle.sql.*

2)Registering JDBC Drivers
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
3)Opening a Connection to a Database(using JDBC thin driver).
Use DriverManager.getConnection(url,username,password)
Url looks like this jdbc:oracle::thin/thick@DSN name:1521:SID
Connection conn=DriverManager.getConnection(“jdbc:oracle:thin:@lavanya:1521:lavanya”,”scott”,”tiger”);
4)Creating a Statement Object
Statement stmt = conn.createStatement();
5)Executing a Query will return a Result Set Object
ResultSet rset = stmt.executeQuery (“SELECT ename FROM emp”);
6)Processing the Result Set
while (rset.next())
System.out.println (rset.getString(1));

7)Closing the Result Set and Statement Objects
rset.close()
stmt.close();
8)Closing the Connection
conn.close();
thats it run the program …make sure u have emp table in database. If you copy the code in italic into Eclipse editor it will run for sure.

Assingments
1) Using JDBC try to insert,delete,update,select record from a database
2) Work on prepared statements.

Published in: on December 20, 2006 at 10:20 pm  Comments (4)  

The URI to TrackBack this entry is: https://klavanya.wordpress.com/2006/12/20/jdbc-setup-and-writing-first-program/trackback/

RSS feed for comments on this post.

4 CommentsLeave a comment

  1. Hello there! This article could not be written any
    better! Reading through this article reminds
    me of my previous roommate! He always kept preaching about this.
    I most certainly will forward this post to him.
    Pretty sure he’s going to have a very good read.
    I appreciate you for sharing!

  2. Thank you for some other magnificent post. The place else
    may just anybody get that type of info in such a perfect method of
    writing? I’ve a presentation subsequent week,
    and I am at the look for such info.

  3. Asking questions are genuinely good thing if you are not understanding something
    fully, however this piece of writing gives nice understanding even.

  4. Article writing is also a fun, if you be acquainted with
    then you can write or else it is complex to write.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: