Java
 Computer >> コンピューター >  >> プログラミング >> Java

JavaでCLOB型を文字列に変換する方法は?


CLOBは一般にCharacterLargeObjectの略で、SQL Clobは組み込みのデータ型であり、大量のテキストデータを格納するために使用されます。このデータ型を使用すると、最大2,147,483,647文字のデータを格納できます。

JDBC APIのjava.sql.Clobインターフェースは、CLOBデータ型を表します。 JDBCのClobオブジェクトはSQLロケーターを使用して実装されているため、SQL CLOB(データではない)への論理ポインターを保持します。

MySQL データベースは、TINYTEXT、TEXT、MEDIUMTEXT、およびLONGTEXTの4つの変数を使用して、このデータ型のサポートを提供します。

CLOBデータ型を文字列に変換するには

  • getClob()を使用してテーブルからClob値を取得します またはgetCharacterStream() PresparedStatementのメソッド インターフェイス。
Reader r = clob.getCharacterStream();
  • 取得した文字ストリームから各文字を1つずつ読み取り、 StringBuilderに追加します。 またはStringBuffer
int j = 0;
StringBuffer buffer = new StringBuffer();
int ch;
while ((ch = r.read())!=-1) {
   buffer.append(""+(char)ch);
}
System.out.println(buffer.toString());
j++;
  • 最後に、取得した文字列を表示または保存します。
System.out.println(buffer.toString());

technology_dataという名前のテーブルを作成しましょう 次のクエリを使用してMySQLデータベースで-

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

テーブルArticleの3番目の列には、CLOB型のデータが格納されます。

次のJDBCプログラムは、最初に、テキストファイル(その内容)を記事列(CLOBタイプ)に格納するtechnologies_dataテーブルに5つのレコードを挿入します。

次に、テーブルのレコードを取得し、記事の名前と内容を表示します。ここでは、取得したCLOBのデータを文字列に変換して表示しようとしています。

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.

  1. JavaマップをJSONに変換する方法

    JavaマップをJSONに変換する方法はいくつかあります。 Java配列とマップをJSONに、またはその逆に変換することは非常に一般的です。 この投稿では、JavaMapをJSONに変換するための3つの異なる例を見ていきます。 Jackson、Gson、org.jsonライブラリを使用します。 Jacksonを使用したJSONへのJavaマップ 次の例では、JacksonCoreとJacksonBindingを使用してJavaMapをJSONに変換します。 Jacksonライブラリを使用するには、最初にそれらをpom.xmlに追加する必要があります。 ファイル: <dependen

  2. Javaで文字列を比較する方法

    文字列が等しいかどうかを比較するには、Stringオブジェクトのequalsを使用する必要があります またはequalsIgnoreCase メソッド。 ==を使用すべきでない理由もわかります 文字列を比較する演算子。 文字列とequals()メソッドの比較 Javaで2つの文字列を比較する必要があり、文字列の大文字と小文字も気にする必要がある場合は、equals()を使用できます。 メソッド。 たとえば、次のスニペットは、文字列の2つのインスタンスが大文字小文字を含むすべての文字で等しいかどうかを判断します。 public class CompareTwoStrings { p