I am not sure about other OS'es, but with Windows this is what you need
to do.
1) Setup the Data Source Name (DSN) in the Control Panel. Go to the
"ODBC Data Sources" in the Control Panel (found under "Administrative
Tools" on Win2K/XP) Select "Add" and choose the driver. You can use
either the "Microsoft ODBC for Oracle" driver or the "Oracle ODBC
Driver" (which you will have only if you have oracle software installed
on your system) Enter the Data Source Name, (can be any name you are
comfortable using) the username (you can use scott/tiger or whatever
username/password you have) and the server (server address if the
database is on a remote machine or leave empty if the database is on the
same computer). Once you add this DSN, you will see it listed in the
main ODBC Data Source window.
2) Code your Java program to use JDBC for connecting to the database and
working with it.
First import the java.sql package into your program.
Create a String object with the url "jdbc:odbc:dsn" where dsn is the
name of the DS you created above.
Optionally, create Strings to store usernames and password.
Let Java load the driver you are going to use using the Class.forName()
method.
Register the driver with the DriverManager class and open a connection
to the Oracle database.
Create Statement(used for SQL statements) , ResultSet(for holding the
results of a query)
I have included a simple program here.
import java.sql.*;
class sqldemo {
public static void main(String[] args) throws SQLException {
int stNo; // For storing the EmpNo
String stName; //For storing the EmpName
Connection conn;
Statement stmt;
ResultSet rs; //To store results from Oracle (returns a
table)
// Load the driver. This uses the JDBC-ODBC Bridge driver. There
are more drivers for you to use with Oracle. This is the most basic.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Register with DriverManager and open a connection to Oracle
conn=DriverManager.getConnection("jdbc:odbc:empDB","scott","tiger");
// Create a SQL Statement to be sent
stmt=conn.createStatement();
// Pass a query to Oracle
rs=stmt.executeQuery("select * from emp"); // No semicolon
after the SQL statement
System.out.println("Empno "+"Name");
// Display the results
while(rs.next()) {
stNo=rs.getInt(1); //Converts the first column to
type int
stName=rs.getString(2); // Second column converted to
String
System.out.println(stNo+"\t"+stName);
}
}
}
If you want to execute SQL statements such as CREATE, ALTER, INSERT,
etc., use the Stmt.executeUpdate() method.
Example:
Stmt.executeUpdate("Create table test(testID number(3), name
varchar(20));
Of course, this is merely scratching the surface, so if you want to
learn more, try out a book. Almost every other book on Java should deal
with JDBC.