JDBC in Eclipse…

1) First we need to download JDBC drivers. I select ojdbc14.jar under Oracle9.2.0.1.
2)Creating a DSN, for this goto Controlpannel->administrative tools->ODBC->userDSN->add ->enter the DSN name: cnya(myname)
                  TSN name: orcl(what we gave at the time of oracle installation)-> OK.
3)In Eclipse, after creating a new project -> right click on projname-> properties -> select Java Buildpath-> Add External Jars-> enter the downloaded jar file path -> Ok
It adds the external jdbc jars to our project.
4)Loading and Connecting to OracleDriver(thin driver).
I use thin driver bcoz it is fast comparatively with the remaining drivers in our local machine.

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

Connection con=DriverManager.getConnection(“jdbc:oracle:thin:@IPAddress/localhost:1521:SID”,”scott”,”tiger”);

for me SID = orcl ( we gave this at the time oracle installation)
5) for JDBC-ODBC bridge driver
  Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
  Connection con=DriverManager.getConnection(“jdbc:odbc:DSN name”,”scott”,”tiger”);
6) First JDBC Program, Ex.
  try
  {
  DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  Connection con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:orcl”,”scott”,”tiger”);
  Statement stmt=con.createStatement();
  ResultSet rs=stmt.executeQuery(“select * from emp”);
  while(rs.next())
  {
  out.println(rs.getInt(1));
  out.println(rs.getString(2));
  }
  }
  catch(Exception e)
  { e.printStackTrace(); }

It will display all the emp details….

About these ads

About vchaithanya

Software Engineer
This entry was posted in Database. Bookmark the permalink.

6 Responses to JDBC in Eclipse…

  1. Prashant says:

    Hi Chaitanya

    thanks a ton for the information

    i was trying to connect using Thin driver for a week now and finally your post helped me solve my problem..

    I was getting the error:
    java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver

    I added the following instead of loading drivers using class.forName and it worked:

    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

    thanks again

  2. ankit says:

    Hello,
    Thanks for the tutorial. I was now able to connect eclipse to database.

    However, I am only to connect using ODBC method you mentioned.

    When i use thin Driver method, a run time exception occured. Since, thin driver is the most preferred method for web applications. I gives it a little more preference.

    But thanks for your tutorial. Working on eclipse sometimes provides a lot of headache.

  3. premraj says:

    Hello,
    Thanks a lot for your tutorial. it helped to setup jdbc driver file.

  4. Dani says:

    Hi,vchaithanya!
    I hope you are still checking your blog. I realy want to thank you for your most appreciated tips regarding JDBC in Eclipse. Believe it or not, I have been trying to fix this for almost a week now. I have searched the web for a tip or two like never before. When I was about to give up, I came across your tips. And guess what, all the problems I have had to deal with in Eclipse and JDBC are now gone! My java applikation is doing exactly what it is supposed to do thanks to your tips. I wish you all the best and success in your endeavours.

    Do you have a website dedicated just for JDBC and Eclipse? I would like to learn more about this subject. My desire is to develope a full fledged java application in Eclipse and Oracle DB as data sorce. Thank you once more! Dani.

    Ps. Sorry for my poor english!

  5. naveen says:

    thanks a lot

  6. saravanan says:

    Hai chaithanya

    Thanks for the tutorial. I was now able to connect eclipse to database.

    However, I am only to connect using ODBC method you mentioned.

    thanks alot

    very useful for my jdbc with eclipse

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