
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to convert a CLOB type to String in Java?
CLOB stands for Character Large Object in general, an SQL Clob is a built-in datatype and is used to store large amount of textual data. Using this datatype, you can store data up to 2,147,483,647 characters.
The java.sql.Clob interface of the JDBC API represents the CLOB datatype. Since the Clob object in JDBC is implemented using an SQL locator, it holds a logical pointer to the SQL CLOB (not the data).
MySQL database provides support for this data type using four variables namely, TINYTEXT, TEXT, MEDIUMTEXT and, LONGTEXT.
To convert CLOB data type to string
- Retrieve the Clob value from a table using the getClob() or getCharacterStream() method of the PresparedStatement interface.
Reader r = clob.getCharacterStream();
- Read each character one by one from the retrieved Stream of characters and append them to the StringBuilder or 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++;
- Finally, display or stored the obtained String.
System.out.println(buffer.toString());
Example
Let us create a table with name technologies_data in MySQL database using the following query −
CREATE TABLE Technologies (Name VARCHAR(255), Type VARCHAR(255), Article LONGTEXT);
The third column of the table Article stores the data of type CLOB.
Following JDBC program initially inserts 5 records in the technologies_data table storing text file (contents of it) it to the article column (CLOB type).
Then, it retrieves the records of the table and, displays the name and contents of the article. Here, we are trying to converting the data of the retrieved CLOB into String and display it.
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(" "); } } }
Output
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.
- Related Articles
- Convert a String to a double type number in Java
- Java Program to convert a Primitive Type Value to a String
- Java Program to convert a String to a float type Number
- How to convert string type value to array type in JavaScript?
- How to Convert string to float type in Golang?
- How to convert an enum type variable to a string in C++?
- How to convert a String to an int in Java
- How to convert a String to an enum in Java?
- How to convert a double value to String in Java?
- How to convert InputStream object to a String in Java?
- How to convert int to String in java?
- How to convert String to Date in java?
- How to Convert String to Object in Java?
- Java Program to convert a string into a numeric primitive type using Integer.valueOf()
- How to convert from string to date data type in MongoDB?
