Home Database Mysql Tutorial Java对Oracle中Clob类型数据的读取和写入

Java对Oracle中Clob类型数据的读取和写入

Jun 07, 2016 pm 05:30 PM

Java对Oracle中Clob数据类型是不能够直接插入的,但是可以通过流的形式对clob类型数据写入或者读取,网上代码并不算特别多,讲的

Java对Oracle中Clob数据类型是不能够直接插入的,但是可以通过流的形式对clob类型数据写入或者读取,网上代码并不算特别多,讲的也不是很清楚,我对网上资料进行了整理和总结,具体看代码:

写入clob数据

import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


public class TestClobIn {
  public static void main(String args[]){
  String data="this is a long passage!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
  Writer outStream = null;
 //通过JDBC获得数据库连接
  try {
  Class.forName("oracle.jdbc.driver.OracleDriver");
  Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ewins", "scott", "tiger");
  con.setAutoCommit(false);
  Statement st = con.createStatement();
  //插入一个空对象empty_clob(),这个是必须的
  st.executeUpdate("insert into TESTCLOB(ID, NAME, CLOBATTR)values(2,'thename', empty_clob())");
  //锁定数据行进行更新,注意“for update”语句,这里不用for update锁定不可以插入clob
  ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1 for update");
  if (rs.next())
  {
  //得到java.sql.Clob对象后强制转换为oracle.sql.CLOB
  oracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob("CLOBATTR");
  outStream = clob.getCharacterOutputStream();
  //data是传入的字符串,定义:String data
  char[] c = data.toCharArray();
  outStream.write(c, 0, c.length);
  }
  outStream.flush();
  outStream.close();
  con.commit();
  con.close();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  }
}

读取clob数据

import java.io.InputStream;
import java.io.Reader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;


public class TestClobOut {
  public static void main(String args[]){
  String data;
  Reader inStream=null;
  //获得数据库连接
  Connection con = ConnectionFactory.getConnection();//ConnectionFactory类是另外定义的,不必纠结
  con.setAutoCommit(false);
  Statement st = con.createStatement();
  //不需要“for update”
  ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1");
  if (rs.next())
  {
  java.sql.Clob clob = rs.getClob("CLOBATTR");
  inStream = clob.getCharacterStream();
  char[] c = new char[(int) clob.length()];
  inStream.read(c);
  //data是读出并需要返回的数据,类型是String
  data = new String(c);
  inStream.close();
  }
  inStream.close();
  con.commit();
  con.close();
  }
}

对比我们可以看出,无论出库入库,都要对clob数据类型进行查询操作,写入clob数据相对来说更复杂一点,需要先插入empty_clob()值,然后使用带“for update”的查询语句锁定更新行,,最后实例化输出流并对clob类型字段数据进行写入操作;读取clob相对轻松一些,利用getCharacterStream方法得到输入流,从数据库中clob字段下,直接将数据读取出来。

linux

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

Reduce the use of MySQL memory in Docker

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

How do you alter a table in MySQL using the ALTER TABLE statement?

How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

How to solve the problem of mysql cannot open shared library

What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

What is SQLite? Comprehensive overview

Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

Run MySQl in Linux (with/without podman container with phpmyadmin)

Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

Running multiple MySQL versions on MacOS: A step-by-step guide

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? Mar 18, 2025 pm 12:00 PM

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)?

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

How do I configure SSL/TLS encryption for MySQL connections?

See all articles