首頁 > Java > java教程 > 主體

在Java中如何將CLOB類型轉換為字串?

王林
發布: 2023-09-11 18:09:02
轉載
1668 人瀏覽過

在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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!