首页  >  文章  >  Java  >  在Java中如何将CLOB类型转换为字符串?

在Java中如何将CLOB类型转换为字符串?

王林
王林转载
2023-09-11 18:09:021731浏览

在Java中如何将CLOB类型转换为字符串?

CLOB 通常代表字符大型对象,SQL Clob 是一种内置数据类型,用于存储大量文本数据。使用此数据类型,您最多可以存储 2,147,483,647 个字符的数据。

JDBC API 的 java.sql.Clob 接口表示 CLOB 数据类型。由于 JDBC 中的 Clob 对象是使用 SQL 定位器实现的,因此它保存一个指向 SQL CLOB(而不是数据)的逻辑指针。

MySQL 数据库提供了对此数据类型的支持使用四个变量,即 TINYTEXT、TEXT、MEDIUMTEXT 和 LONGTEXT。

将 CLOB 数据类型转换为字符串

  • 从表中检索 Clob 值使用 PreparedStatement 接口的 getClob()getCharacterStream() 方法。
Reader r = clob.getCharacterStream();
  • 从检索到的字符流中一一读取每个字符,并将其附加到 StringBuilderStringBuffer 中。
int j = 0;
StringBuffer buffer = new StringBuffer();
int ch;
while ((ch = r.read())!=-1) {
   buffer.append(""+(char)ch);
}
System.out.println(buffer.toString());
j++;
  • 最后将获取到的String进行显示或存储。
System.out.println(buffer.toString());

示例

让我们使用以下查询在 MySQL 数据库中创建一个名为 technologies_data 的表 -

CREATE TABLE Technologies (Name VARCHAR(255), Type VARCHAR(255), Article LONGTEXT);

表 Article 的第三列存储 CLOB 类型的数据。

以下 JDBC 程序最初在存储文本文件(其内容)的 Technologies_data 表中插入 5 条记录到 Article 列(CLOB)类型)。

然后,它检索表的记录,并显示文章的名称和内容。在这里,我们尝试将检索到的 CLOB 数据转换为 String 并显示它。

import java.io.FileReader;
import java.io.Reader;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class ClobToString {
   public static void main(String args[]) throws Exception {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/sampledatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating a Statement object
      Statement stmt = con.createStatement();
      //Inserting values
      String query = "INSERT INTO Technologies_data VALUES (?, ?, ?)";
      PreparedStatement pstmt = con.prepareStatement(query);
      pstmt.setString(1, "JavaFX");
      pstmt.setString(2, "Java Library");
      FileReader reader = new FileReader("E:\images\javafx_contents.txt");
      pstmt.setClob(3, reader);
      pstmt.execute();
      pstmt.setString(1, "CoffeeScript");
      pstmt.setString(2, "Scripting Language");
      reader = new FileReader("E:\images\coffeescript_contents.txt");
      pstmt.setClob(3, reader);
      pstmt.execute();
      pstmt.setString(1, "Cassandra");
      pstmt.setString(2, "NoSQL Database");
      reader = new FileReader("E:\images\cassandra_contents.txt");
      pstmt.setClob(3, reader);
      pstmt.execute();
      //Retrieving the data
      ResultSet rs = stmt.executeQuery("select * from Technologies_data");
      System.out.println("Contents of the table are: ");
      while(rs.next()) {
         System.out.println("Article: "+rs.getString("Name"));
         Clob clob = rs.getClob("Article");
         Reader r = clob.getCharacterStream();
         StringBuffer buffer = new StringBuffer();
         int ch;
         while ((ch = r.read())!=-1) {
            buffer.append(""+(char)ch);
         }
         System.out.println("Contents: "+buffer.toString());
         System.out.println(" ");
      }
   }
}

输出

Connection established......
Contents of the table are:
Article: JavaFX
Contents: JavaFX is a Java library using which you can develop Rich Internet Applications. By using Java technology, these applications have a browser penetration rate of 76%.
Article: CoffeeScript
Contents: CoffeeScript is a lightweight language based on Ruby and Python which transcompiles (compiles from one source language to another) into JavaScript. It provides better syntax avoiding the quirky parts of JavaScript, still retaining the flexibility and beauty of the language.
Article: Cassandra
Contents: Apache Cassandra is a highly scalable, high-performance distributed database designed to handle large amounts of data across many commodity servers,
providing high availability with no single point of failure. It is a type of NoSQL database. Let us first understand what a NoSQL database does.

以上是在Java中如何将CLOB类型转换为字符串?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除