Database Tutorial

Course Tutorial Site

Site Admin

Oracle and Java

with 15 comments

This demonstrates how to create an Java infrastructure for reading and writing large text files to an Oracle database. The example provides:

  • A FileIO.jar library that lets you enter Oracle connection parameters through a JOptionPane, and a customized JFileChooser to filter and read source files from the file system.
  • A ojdbc6.jar file, which is Oracle’s library for JDBC communication with the Oracle Databases.

The steps to compiling and testing this code are qualified below:

  1. Download and install the Java Software Development Kit (JSDK) for Java 6.
  2. Create a C:\JavaTest folder on Windows, or a /JavaTest directory from some mount point of your choice.
  3. Download and position the ojdbc6.jar and FileIO.jar files in the JavaTest directory.
  4. Create a batch file to source your environment path (%PATH% on Windows and $PATH on Linux or Mac OS X) and the two Java Archive (JAR) files. A sample batch file is noted below:
set PATH=C:\Program Files\Java\jdk1.6.0_07\bin;%PATH%
set CLASSPATH=C:\JavaDev\Java6\ojdbc6.jar;C:\JavaDev\Java6\FileIO.jar;.

You can run this file by simply typing the files first name. On Linux or Mac OS X, you first need to grant it privileges with the chmod command as 755.

  1. Copy the WriteReadCLOB.java code from the bottom of this posting and also put it into the JavaTest directory.
  2. Compile the WriteReadCLOB.java source code with the javac utility, as shown below:
javac WriteReadCLOB.java

After you compile it, you should run it as follows:

java WriteReadCLOB
  1. Before running the code, you’ll need to seed (INSERT) a row that meets the desired hard coded criteria. It requires an ITEM_TITLE value of 'The Lord of the Rings - Fellowship of the Ring' and an ITEM_SUBTITLE of 'Widescreen Edition' in the ITEM table.
  2. When it runs, you’ll see the following tabbed JOptionPane.

You need to enter the following values before clicking the OK button:

  • Host: The hostname of your machine.
  • Port: The port that the Oracle Listener is running on (the default value is 1521).
  • Database: The Oracle TNS Alias, which is orcl for the full database sample database or xe for the Oracle Database 10g Express Edition.
  • UserID: The user (schema) name where you’ve created an ITEM table.
  • Password: The password for the user’s account.

In the JFileChooser, select a text file to upload to the database.

You should see what you uploaded displayed in a JFrame.

Written by michaelmclaughlin

October 8th, 2009 at 9:28 pm

Posted in

15 Responses to 'Oracle and Java'

Subscribe to comments with RSS or TrackBack to 'Oracle and Java'.

  1. My JFrame shows a blank white image when I upload a file… so far I have tried uploading a .jpg file and a .txt file. I guess I’m expecting to see them in the JFrame, but all I see is white. Is there a specific file type I am to upload during step 8?

    Cortney

    15 Jul 10 at 5:29 pm

  2. set PATH=C:\Program Files\Java\jdk1.6.0_07\bin;%PATH%
    set CLASSPATH=C:\JavaDev\Java6\ojdbc6.jar;C:\JavaDev\Java6\FileIO.jar;.

    i dont think the period at the end of this is needed

    Chaz

    15 Jul 10 at 11:35 pm

  3. It would be useful to know that you need to type javac WriteReadCLOB.java into a command prompt. Some users out there are new to java and batch files.

    Chaz

    15 Jul 10 at 11:40 pm

  4. C:\Users\Xiao Peng>javac WriteReadCLOB.java
    ‘javac’ is not recognized as an internal or external command,operable program or batch file.

    I get this error, and i installed the java program correctly and here is my bat file contents:

    set PATH=C:\Program Files\Java\jdk1.6.0_21\bin\;%PATH%
    set CLASSPATH=C:\JavaTest\ojdbc6.jar;C:\JavaTest\FileIO.jar;

    Any suggestions?

    Chaz

    16 Jul 10 at 12:11 am

  5. The two links at the top of the lab are broken. The tags need the src attribute needs to be changed to a href.

    Williamm

    2 Dec 10 at 1:44 pm

  6. It appears that the links to download the FileIO.jar and the ojdbc6.jar files are broken. unless i missed where i need to download them from. Thanks

    Peter G

    3 Dec 10 at 4:46 pm

  7. To get the files is not very easy. With Google Chrome, right clicking the name of the file doesn’t give the option of view selection source. It would be much more efficient to link directly to the download files.

    Michael Strawn

    8 Dec 10 at 4:16 pm

  8. the links to FileIO.jar ojdbc6.jar are broken, could you fix them?

    brad

    9 Dec 10 at 2:05 pm

  9. In part 8 it says “select a file to upload to the database” but it does not say which file to select. What files should we try to upload?

    Natalie Staber

    6 Dec 11 at 9:29 pm

  10. You should choose a text file. Text has been added.

    michaelmclaughlin

    22 Oct 14 at 1:44 am

  11. There were fixed.

    michaelmclaughlin

    22 Oct 14 at 2:02 am

  12. It’s not resolving to a correct %PATH% location where the javac executable can be found.

    michaelmclaughlin

    22 Oct 14 at 2:13 am

  13. You mean like Step #6?

    michaelmclaughlin

    22 Oct 14 at 2:14 am

  14. The period allows Java to find other classes in the present working directory, which is critical when they’re not in the same package (and you’re file layout doesn’t support the packages format.

    michaelmclaughlin

    22 Oct 14 at 2:15 am

  15. You should upload a text file not an image file. That’s generally the cause of what you’re seeing.

    michaelmclaughlin

    22 Oct 14 at 2:20 am

Leave a Reply