Setting Up JDBC & Odbc
Setting Up JDBC & Odbc
Setting Up JDBC & Odbc
1. Install Oracle and make sure it is running ok. (ie. you can connect to Oracle by sqlplus)
3. Typically, the JDBC driver file you should download is named class12.zip.
If you want to know the details of the JDBC drivers on the Oracle site. Here is some info.
JDBC drivers can be classified into 4 types, ranging from pure Java driver to thin Java/heavy native
driver. The class12.zip driver we chosen is the pure Java type. And for different type, there are different
drivers for different Oracle versions and JDK versions. Pick the one that match your environment. I
assume we are using Java 2, so we should use the class12.zip driver.
1. If you get an error message similar to this: The system cannot find the file
specified,
That means that you haven’t told the JDBC where you store your file. Assume
that the path to reach your file(s) is C:\cs157aProject\, you have to reset your
CLASSPATH as the following:
C:\sjsu\cs157a\jdbc\classes12.zip;C:\cs157aProject\
Refer to step 4 to reset your CLASSPATH. If you still have problem of compiling
your program, restart your computer.
Note: Once you setup the CLASSPATH, all your java programs have to store
under the same directory to compile, no matter you use JDBC or not.
That obviously means your user name or password is not correct. scott/tiger is the
default login to Oracle database, but you may have changed it while in Oracle
installation.
41. Once the DbTest program runs correctly, you could start coding your own Java Database
program. To make a good looking project, you should have knowledge of Java GUI
programming such as Swing, AWT or Applet. Here we only get into details of the
database programming portion.
1. To connect through JDBC, the first thing you need is to load the JDBC driver to
your program. Here is how you do it:
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
This loaded driver enables you to make a connection to Oracle database.
3. Once the connection is made, you may want to start issuing your SQL statement
to Oracle within your code. You may then create a java.sql.Statement object by
writing:
Statement stmt = conn.createStatement();
4. Using the Statement object, you can then construct your SQL statement and
execute it. The result of a SQL statement will be assigned to a ResultSet object.
String sqlString = "SELECT * FROM SUPPLIER";
ResultSet rset = stmt.executeQuery(sqlString);
5. If your SQL statement runs successfully, you will be able to walk through the
obtained ResultSet object to get result details. Here is one example:
6. int numCols = 4; // there are 4 columns in SUPPLIER table
7. while (rset.next())
8. {
9. // walk through each row
10. for (int i = 1; i<=numCols; i++)
11. {
12. // print each columen
13. System.out.println(rset.getString(i) + " ");
14. }
15. System.out.println();
}