JDBC从文件中读取内容写入CLOB字段的例子

  public void insertClob() throws Exception {
  String content = String.valueOf("");
  try {
   BufferedReader in = new BufferedReader(
     new FileReader("c:/config.xml"));
   String str;
   while ((str = in.readLine()) != null) {
    content = content.concat(str);
   }
   in.close();
  } catch (IOException e) {
  }
  Statement statement = null;

  ResultSet resultset = null;
  Connection connection = null;
  try {
   String user = "ahcwtest";
   String password = "ahcwtest";
   String driver = "oracle.jdbc.driver.OracleDriver";
   String url = "jdbc:oracle:thin:@192.168.130.245:1521:orcl";

   Class.forName(driver).newInstance();

   connection = DriverManager.getConnection(url, user, password);

   // 设置不自动提交

   connection.setAutoCommit(false);

   // 创建数据库操作语句
   
   statement = connection.createStatement();
   

   // 定义SQL语句
   statement.executeUpdate("update A set B = EMPTY_CLOB()");
   
   String strSQL = "SELECT B FROM A WHERE ROWNUM = 1 FOR UPDATE";

   resultset = statement.executeQuery(strSQL);

   CLOB contents = null;

   while (resultset.next()) {

    // 取出CLOB对象

    contents = (oracle.sql.CLOB) resultset.getClob(1);

   }

   Writer out = contents.getCharacterOutputStream();

   out.write(content);

   out.flush();

   out.close();

   // 数据库提交

   connection.commit();

  } catch (Exception e) {

   e.printStackTrace();

  } finally {

  }

 }

 

-----------------------------------

读取clob的内容到一个文件中

 public void readClob() throws Exception {
  String content = String.valueOf("");
  // try {
  // BufferedReader in = new BufferedReader(
  // new FileReader("c:/test.dat"));
  // String str;
  // while ((str = in.readLine()) != null) {
  // content = content.concat(str);
  // }
  // in.close();
  // } catch (IOException e) {
  // }
  Statement statement = null;

  ResultSet resultset = null;
  Connection connection = null;
  try {
   String user = "ahcwtest";
   String password = "ahcwtest";
   String driver = "oracle.jdbc.driver.OracleDriver";
   String url = "jdbc:oracle:thin:@192.168.130.245:1521:orcl";

   Class.forName(driver).newInstance();

   connection = DriverManager.getConnection(url, user, password);

   // 设置不自动提交

   connection.setAutoCommit(false);

   // 创建数据库操作语句

   statement = connection.createStatement();

   // 定义SQL语句

   String strSQL = "SELECT B  FROM A WHERE ROWNUM = 1";

   resultset = statement.executeQuery(strSQL);

   oracle.sql.CLOB contents = null;

   while (resultset.next()) {

    // 取出CLOB对象

    contents = (oracle.sql.CLOB) resultset.getClob("B");

   }

   BufferedReader a = new BufferedReader(contents.getCharacterStream());

   String str = "";

   while ((str = a.readLine()) != null) {
    content = content.concat(str);
   }
   
   BufferedWriter out = new BufferedWriter(new FileWriter("C:/tttt.txt"));
   out.write(content);
   out.close();
   

  } catch (Exception e) {

   e.printStackTrace();

  } finally {
   statement.close();
   connection.close();

  }

 }

 

你可能感兴趣的:(JDBC从文件中读取内容写入CLOB字段的例子)