How to insert BLOB/CLOB data in DB using JDBC ?

Use the following code to insert LOB data in DB using JDBC.

public static void insertLob() throws Exception
    {
            String dbUrl = "jdbc:oracle:thin:@<host_name>:<port_number>:<DB_Name>";
            Connection con = null;
            ResultSet rs = null;
            PreparedStatement psIns = null;
           
            try
            {
                    Class.forName("<Driver_Name>");

                    con = DriverManager.getConnection(dbUrl, "<userName>", "<passWord>");

                    psIns = con
                    .prepareStatement("INSERT INTO BLOB_FILE_TABLE (ID,FILE_NAME,FILE_CONTENT) VALUES (?,?,?)");

                    FileInputStream fs = new FileInputStream(new File("blob.txt"));
                    psIns.setInt(1, 100);
                    psIns.setString(2, "blob.txt");
                    psIns.setBinaryStream(3, fs);
                    psIns.execute();

            }
            catch (Exception e)
            {
                    e.printStackTrace();
            }
            finally
            {
                     if (con != null)
                    {
                            con.close();
                    }

                    if (psIns != null)
                    {
                            psIns.close();
                    }

                   
            }
 
}

Technology: 

Search