Java source sample (Oracle Database + java) to SELECT and display CLOBs

I wrote it while referring to the following site. 彡 (゜) (゜) Wai java is unfamiliar, so there is a forgiveness and 彡 (-) (-)

SELECT statement sample http://java-reference.com/java_db_select.html

Interface clob https://docs.oracle.com/javase/jp/8/docs/api/java/sql/Clob.html

The table definition is as follows (゜) (゜)

SQL> DESC LOB_TBL
 Name          Null?    Type
 ------------- -------- ----------------------------
 LOB_ID        NOT NULL NUMBER
 LOB_DATA               CLOB

The sample of java source is as follows (゜) (゜)

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Clob;

public class LOBSelectSample {
    public static void main(String[] args) throws Exception{
        //initialize
        System.out.println("Init...");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Clob cl = null;

        //DB connection info
        String path = "jdbc:oracle:thin:@localhost:1521:orcl";  //path
        String id = "xxxxxxxx";  //ID
        String pw = "yyyyyyyy";  //password

        try{
            //JDBC Driver
            Class.forName("oracle.jdbc.driver.OracleDriver");

            //DB Connect
            System.out.println("Connect...");
            conn = DriverManager.getConnection(path, id, pw);

            //SELECT
            System.out.println("Select...");
            ps = conn.prepareStatement("SELECT * FROM LOB_TBL");
            rs = ps.executeQuery();

            //Column count
            int colCount = rs.getMetaData().getColumnCount();
            System.out.println("Get Column:" + colCount);

            //Output
            while (rs.next()) {
                System.out.print(rs.getInt("LOB_ID"));
                System.out.println();
                cl = rs.getClob("LOB_DATA");
                if (cl != null) {
                    System.out.print(cl.getSubString(1, (int)cl.length()));
                    //System.out.print(cl.getSubString(1, 2097152));
                }
                System.out.println();
            }
        } catch(Exception ex) {
            //Exception
            ex.printStackTrace();  //Error
        } finally {
            //Close
            if(rs != null) rs.close();
            if(ps != null) ps.close();
            if(conn != null) conn.close();
        }
    }
}

Compile, Han silently raw java! 彡 (゜) (゜)

javac -classpath .:${ORACLE_HOME}/jdbc/lib/ojdbc8.jar LOBSelectSample.java

Execution command, Han is silent t (ry

java -classpath .:${ORACLE_HOME}/jdbc/lib/ojdbc8.jar LOBSelectSample

As a result of the execution, the SQL text that was stuck in the LOB_DATA column came out. 彡 (^) (^)

Init...
Connect...
Select...
Get Column:2
1

2
with …
:
(Omission)
:
… from dual

Recommended Posts

Java source sample (Oracle Database + java) to SELECT and display CLOBs
Java code sample to acquire and display DBLINK source and destination data in Oracle Database using DBLINK
[For beginners] Minimum sample to display RecyclerView in Java
Android app to select and display images from the gallery
Sample code to get the values of major SQL types in Java + Oracle Database 12c
[Java] How to display Wingdings
Sample code to parse date and time with Java SimpleDateFormat
Source to display character array with numberPicker in Android studio (Java)
Sample to read and write LibreOffice Calc fods file in Java 2021
Java 9 new features and sample code
BloomFilter description and implementation sample (JAVA)
How to pass Oracle Java Silver
Sample code to get key JDBC type values in Java + H2 Database
Sample code to serialize and deserialize Java Enum enums and JSON in Jackson
I want to display images with REST Controller of Java and Spring!
[Java] How to get the current date and time and specify the display format
Sample to display (head-up) notifications on Android
Java implementation to create and solve mazes
[Java] How to output and write files!
[Java] Flow from source code to execution
[Introduction to Java] Variable declarations and types
Sample to unzip gz file in Java
Java to C and C to Java in Android Studio