oracle.otnsamples.jdbc.ClobManipulationIn10g (Java2HTML)
package oracle.otnsamples.jdbc;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;
import oracle.jdbc.OracleDriver;
import java.io.IOException; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader;
import java.util.Properties;
public class ClobManipulationIn10g {
private Connection conn = null;
private String url = null; private String user = null; private String password = null;
private Properties props = new Properties();
private String fileName = null;
public ClobManipulationIn10g(String fileName) { this.fileName = fileName; }
public static void main(String[] args) throws SQLException {
ClobManipulationIn10g clobManipulationIn10g = new ClobManipulationIn10g(args[0]);
DriverManager.registerDriver(new OracleDriver());
String dbUrl = "jdbc:oracle:thin:@<database host machine>:<port>:<SID>"; clobManipulationIn10g.url = dbUrl;
clobManipulationIn10g.user = "scott";
clobManipulationIn10g.password = "tiger";
clobManipulationIn10g.props.put("user", clobManipulationIn10g.user ); clobManipulationIn10g.props.put("password", clobManipulationIn10g.password); clobManipulationIn10g.props.put("SetBigStringTryClob", "true");
clobManipulationIn10g.checkTables();
clobManipulationIn10g.insertClob(); clobManipulationIn10g.selectClob();
}
private void insertClob() throws SQLException {
PreparedStatement pstmt = null;
try { if ((conn==null)||conn.isClosed()){ conn = DriverManager.getConnection( this.url, this.props ); }
String sql = "INSERT INTO clob_tab VALUES(?)";
String str = this.readFile();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,str);
pstmt.executeUpdate();
} catch (SQLException sqlex) { System.out.println("SQLException while connecting and inserting into " + "the database table: " + sqlex.toString()); } catch (Exception ex) { System.out.println("Exception while connecting and inserting into the" + " database table: " + ex.toString()); } finally { if (pstmt!=null) pstmt.close(); if (conn!=null) conn.close(); } }
private void selectClob() throws SQLException {
PreparedStatement pstmt = null;
ResultSet rset = null; try { if ((conn==null)||conn.isClosed()){ conn = DriverManager.getConnection( this.url, this.props ); }
String sqlCall = "SELECT clob_col FROM clob_tab"; pstmt= conn.prepareStatement(sqlCall);
rset = pstmt.executeQuery();
String clobVal = null;
while (rset.next()) { clobVal = rset.getString(1); System.out.println("CLOB length: "+clobVal.length()); }
} catch (SQLException sqlex) { System.out.println("SQLException while connecting and querying the " + "database table: " + sqlex.toString()); } catch (Exception ex) { System.out.println("Exception while connecting and querying the " + "database table: " + ex.toString()); } finally { if (rset !=null) rset.close(); if (pstmt!=null) pstmt.close(); if (conn!=null) conn.close(); } }
private void checkTables() {
Statement stmt = null; ResultSet rset = null; try {
if ((conn==null)||conn.isClosed()){ conn = DriverManager.getConnection( this.url, this.props ); }
stmt = conn.createStatement();
rset = stmt.executeQuery(" SELECT table_name FROM user_tables "+ " WHERE table_name = 'CLOB_TAB' ");
if (!rset.next()) { stmt.executeUpdate(" CREATE TABLE clob_tab(clob_col CLOB)"); }
} catch (SQLException sqlEx) { System.out.println("Could not create table clob_tab : " +sqlEx.toString()); } finally { try { if( rset != null ) rset.close(); if( stmt != null ) stmt.close(); if (conn!=null) conn.close(); } catch(SQLException ex) { System.out.println("Could not close objects in checkTables method : " +ex.toString()); } } }
private String readFile() throws FileNotFoundException, IOException{
BufferedReader br = new BufferedReader(new FileReader(fileName)); String nextLine = ""; StringBuffer sb = new StringBuffer(); while ((nextLine = br.readLine()) != null) {
sb.append(nextLine); } String clobData = sb.toString();
return clobData; } }
|