Link to home
Start Free TrialLog in
Avatar of angel7170
angel7170Flag for United States of America

asked on

Read CLOB data from Oracle using JAVA

Hello,

I have this attached java program that basically reads data from an oracle table which has a CLOB column. In this case, it is "Document" column. What I need is to parse the CLOB into different values.

For example, the output from a the "document" column is attached in the screenshot. I need to find the value for code "APN" which is 737969  and store it in a variable so it can inserted into another oracle table.  In some cases there are multiple "APN" and I need to concatenate them into one by separating commas.

How do I get that in a java code to do it?

Please assist.
Thank you

import java.io.File;
import java.io.FileWriter;
import java.io.Reader;
import java.sql.*;

public class ClobRead {
	private static String url = "jdbc:oracle:thin:@xxxxxxxx:xxxxxxx:xxxxxxxxx";
    private static String username = "xxxxxxxxxx";
    private static String password = "xxxxxxxxxxx!";

    public static void main(String[] args) throws Exception {
        Connection conn = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection(url, username, password);

            String sql = "select wku, source, document from document where wku like '03552246' ";
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet resultSet = stmt.executeQuery();
            while (resultSet.next()) {
                String wku = resultSet.getString(1);
                System.out.println("WKU        = " + wku);
                String source = resultSet.getString(2);
                System.out.println("SOURCE = " + source);
                               
                String doc = resultSet.getString(3);
                System.out.println("DOCUMENT = " + doc);
                               
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null && !conn.isClosed()) {
                conn.close();
            }
        }
    }
}

Open in new window

Capture.PNG
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You could use "regexp_substr" on that CLOB variable (e.g. '^APN.*\d{6}' or similar)...
:)