java 操作clob

之前在学校的时候做的都是练习,小儿科,遇到的情况完全都在自己的设想范围内。最近老是遇到字段溢出的情况,但是varchar2好像最长也只有4000个字符。所以不得不另辟蹊径,就找上了clob字段。

package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Writer;

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

import oracle.sql.CLOB;

public class ClobTest {
	 private static Connection conn;  
     
	    static {  
	        try {  
	            Class.forName("oracle.jdbc.driver.OracleDriver");  
	            conn = DriverManager.getConnection(  
	                    "jdbc:oracle:thin:@localhost:1521:orcl","wch","wch123");  
	        } catch (ClassNotFoundException e) {  
	            e.printStackTrace();  
	        } catch (SQLException e) {  
	            e.printStackTrace();  
	        }  
	    }  
	      
	    public static void main(String[] args) throws SQLException, IOException {  
	      //  testInsert();  
	       // testUpdate();  
	        testRead();  
	    }  
	  
	    private static void testInsert() throws SQLException {  
	        String sql = "insert into CLOBTEST values(1, empty_clob(),empty_clob())";  
	        Statement stm = conn.createStatement();  
	        stm.execute(sql);  
	    }  
	      
	    private static void testUpdate() throws SQLException, IOException {  
	        String sql = "select context,c2 from CLOBTEST where id = 1 for update";  
	        Statement stm = conn.createStatement();  
	        ResultSet rs = stm.executeQuery(sql);  
	        while (rs.next()) {  
	            oracle.sql.CLOB c = (CLOB) rs.getClob(1);  
	            Writer w = c.setCharacterStream(1);
	            w.write("wchwchwch\r\n");
	            w.write("哈哈哈\r\n哈哈哈ds", 2, 5);
	            w.flush();
	            w.close();
	            c = (CLOB) rs.getClob(2);
	            w = c.setCharacterStream(1);
	            w.write("dingsen");
	            w.flush();
	            w.close();
	        }
	        conn.commit(); 
	    }  
	      
	    private static void testRead() throws SQLException, IOException {  
	        String sql = "select context,c2 from CLOBTEST where id = 1";  
	        PreparedStatement pstm = conn.prepareStatement(sql);  
	        ResultSet rs = pstm.executeQuery();  
	        System.out.println("-----------------------------------");
	        while (rs.next()) {  
	            Clob clob = rs.getClob(1);  
	            BufferedReader r = new BufferedReader(clob.getCharacterStream());  
	            char[] cs = new char[10]; 
	            int total = 0;
	            StringBuilder sb = new StringBuilder();
	            while ((total = r.read(cs)) != -1) {  
	                sb.append(cs,0,total);
	            }  
	            System.out.println(sb.toString());
	        }  
	    }  
}

转载于:https://www.cnblogs.com/vvch/p/4027588.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,使用CLOB类型可以通过JDBC API来实现。以下是一个使用CLOB类型的示例代码: ```java import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Clob; public class ClobExample { public static void main(String[] args) { try { // 获取连接 Connection conn = getConnection(); // 创建表 createTable(conn); // 插入数据 insertData(conn); // 查询数据 queryData(conn); // 关闭连接 conn.close(); } catch (SQLException e) { e.printStackTrace(); } } // 获取数据库连接 public static Connection getConnection() throws SQLException { // TODO: 连接数据库 } // 创建表 public static void createTable(Connection conn) throws SQLException { String sql = "CREATE TABLE my_table (id INT PRIMARY KEY, content CLOB)"; PreparedStatement stmt = conn.prepareStatement(sql); stmt.executeUpdate(); stmt.close(); } // 插入数据 public static void insertData(Connection conn) throws SQLException { String sql = "INSERT INTO my_table (id, content) VALUES (?, ?)"; PreparedStatement stmt = conn.prepareStatement(sql); stmt.setInt(1, 1); // 创建一个Clob对象 Clob clob = conn.createClob(); // 将字符串写入Clob对象 clob.setString(1, "This is a CLOB example."); // 设置Clob参数 stmt.setClob(2, clob); // 执行SQL语句 stmt.executeUpdate(); stmt.close(); } // 查询数据 public static void queryData(Connection conn) throws SQLException { String sql = "SELECT * FROM my_table"; PreparedStatement stmt = conn.prepareStatement(sql); // 执行SQL语句 ResultSet rs = stmt.executeQuery(); // 遍历结果集 while (rs.next()) { int id = rs.getInt("id"); // 获取Clob对象 Clob clob = rs.getClob("content"); // 从Clob对象中读取字符串 String content = clob.getSubString(1, (int) clob.length()); System.out.println("id: " + id + ", content: " + content); } // 关闭结果集和语句 rs.close(); stmt.close(); } } ``` 在上面的示例代码中,我们使用了`java.sql.Clob`类来操作CLOB类型数据。具体来说,我们通过`Connection.createClob()`方法创建了一个Clob对象,并通过`Clob.setString()`方法将字符串写入该对象。然后,我们通过`PreparedStatement.setClob()`方法将Clob对象设置为SQL语句的参数。在查询数据时,我们通过`ResultSet.getClob()`方法获取Clob对象,并通过`Clob.getSubString()`方法从中读取字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值