Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connect to Local Oracle 9i Database

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I want to connect to Local install of an Oracle 9i database for testing. Newbie to Java.
I just downloaded NetBeans IDE 3.6 and see the JDBC ODBC Bridge

I have Oracle installed and can connect to it using SQL> in DOS and also Access using the ODBC driver.

It wants a database URL. Can this be done locally?
... does not work
the ODBC name used in Access Oracle9

DougP, MCP, A+
 
In its most simple form, below is how to connect to Oracle.
You must have the classes12.jar in your CLASSPATH.
Do not use the JDBC-ODBC bridge, as it is slower than using Oracle's JDBC driver.
"SID" is your database SID (database name).

Code:
Connection conn = null;
try {
  Class.forName("oracle.jdbc.driver.OracleDriver");
  conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:SID, "userName", "password");
} catch (Exception e) {
  e.printStackTrace(System.err);
}

--------------------------------------------------
Free Database Connection Pooling Software
 
typo, change :

conn = DriverManager.getConnection("jdbc:eek:racle:thin:mad:localhost:1521:SID, "userName", "password");


to :

conn = DriverManager.getConnection("jdbc:eek:racle:thin:mad:localhost:1521:SID", "userName", "password");


--------------------------------------------------
Free Database Connection Pooling Software
 
I have not tried it yet anyway

After I connect how do it pass a SQL statement like

Select * From MyTable;

And return the results in a recordset ???

DougP, MCP, A+
 
You probably have JDK/SDK 1.4. You have everything you need to do JDBC, trust me.

--------------------------------------------------
Free Database Connection Pooling Software
 
Error missing Class. I'm trying this in the DOS prompt
here is a dump of that:
Code:
C:\Java\HelloWorld>javac or.java
or.java:1: 'class' or 'interface' expected
Connection conn = null;
^
or.java:2: 'class' or 'interface' expected
try {
^
2 errors

Do I need to add a CLASS PATH somewhere?

DougP, MCP, A+
 
Errmmm - you cannot just cut'n'paste some code, slap it in a file called "or.java" and hope it works.

I would seriously recommend you take a basic Java tutorial - you will have no chance with JDBC if you don't know how to build a basic class.

This is a bit close to what you should have :

Code:
import java.sql.*;
public class TestConnection {

  public void test() {
    Connection conn = null;
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
      conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:SID", "userName", "password");
      System.out.println("Got connection : " +conn);
     } catch (Exception e) {
      e.printStackTrace(System.err);
     }
  }

  public static void main(String args[]) {
     TestConnection tc = new TestConnection();
     tc.test();
  }

}

I would really take a tutorial though - here is one :





--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top