Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to retrieve String from a BLOB field

Posted on 2012-08-20
5
Medium Priority
?
366 Views
Last Modified: 2012-10-08
I have Database MySQL.

I have a  BLOB column in a table.  column name  is QUERY .

I have put a text  in this column.

Now I want to retrieve this text string in java.

In java  if I do ..

rs.getBlob("QUERY").toString()  ...does not  give me the text string.

how do I get the text string ?  Whats the correct approach ?
0
Comment
Question by:cofactor
5 Comments
 
LVL 5

Expert Comment

by:indrajitmahajan
ID: 38311301
Hi...
You can do it in MySQL..
Just write below query:
CONVERT(column USING utf8)
0
 
LVL 26

Expert Comment

by:Tomas Helgi Johannsson
ID: 38311762
Hi!

Take a look at this example and see if that can give you an idea. :)

import java.io.File;
import java.io.FileWriter;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.io.IOException;
import java.io.StringWriter;

public class Main {
  private static String url = "jdbc:mysql://localhost/testdb";

  private static String username = "java";

  private static String password = "welcome";

  public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection(url, username, password);
    PreparedStatement stmt = conn.prepareStatement("SELECT name, description, data FROM documents ");
    ResultSet resultSet = stmt.executeQuery();
    while (resultSet.next()) {
      String name = resultSet.getString(1);
      String description = resultSet.getString(2);    
      Reader reader = resultSet.getCharacterStream(3);
      StringWriter writer = new StringWriter();
      char[] buffer = new char[1];
      while (reader.read(buffer) > 0) {
        writer.write(buffer);
      }
      System.out.println("Stringwriter: " + writer.toString());  
      
      writer.close();
    }
    conn.close();
  }
}

Open in new window

Regards,
    Tomas Helgi
0
 
LVL 7

Expert Comment

by:humanonomics
ID: 38312180
Try the following:

Blob blob = rs.getBlob("Query");
	            DataInputStream inputStream = null;
	            byte[] dataBuffer = new byte[(int) f.length()];
	            inputStream = new DataInputStream(blob.getBinaryStream());
	            
	            inputStream.readFully(dataBuffer);
	            
	            String blobString = new String(dataBuffer);

Open in new window

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1420 total points
ID: 38313492
byte[] dataBuffer = new byte[(int) f.length()];

Open in new window

should be
byte[] dataBuffer = new byte[(int) blob.length()];

Open in new window


But it's probably much easier to do

String query = new String(rs.getBytes("Query")); // possibly specify encoding

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38475609
:)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question