Discussion Locked

java.sql.Blob

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am unable to store the contents of a blob data (from mySQL database) in a java.sql.Blob
object. When I tried to print the data in the java.sql.Blob object (msgBlob) in the program
given below, only "null" is being printed. Please help. Thank you.

Blob msgBlob = rs.getBlob("fileName");
java.io.InputStream in = msgBlob.getBinaryStream();
byte b;
while ((in.read()) > -1)
{
b = (byte)in.read();
System.out.println(b);
}

I tried another way (shown below) but only "null" is being printed:

Blob msgBlob = rs.getBlob("fileName");
long len = msgBlob.length();
int leng = (int)len;
byte [] data = msgBlob.getBytes(1, leng);

for (int i = 0; i < leng; i++)
{
byte b = data;
System.out.println(b);
}

7 replies

by okujava

hi!
the second example wouldn' compile, because you try to assign byte b = data; where data is an array of byte.
try the following in the first example:

byte b = 0;
while((b = in.read())!= -1){
System.out.println(b);
}

in the second example, try:

for (int i = 0; i < leng; i++)
{
byte b = data[i];
System.out.println(b);
}

it shouldn't print "null", because byte's default value is 0.
if the code isn't right, paste the original code in the comment of this question...
cheers

by okujava

the line
byte [] data = msgBlob.getBytes(1, leng);
would generate an exception too. it should be:
byte [] data = msgBlob.getBytes(0, leng);

by saju_v

Hi!

Thanks a lot for the reply. I had tried both the ways that you advised but it is still
printing null. I had tried another way (the full code is shown below):

public static void main(String[] args)
{
String u_id = "";
String line;

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:myodbc3-test", "saju_v", "saju");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from user where user_id='manojb'");
while (rs.next())
{
InputStream in = rs.getAsciiStream("fileName");
u_id = rs.getString("user_id");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
System.out.println(u_id);
while((line = br.readLine()) != null)
{
System.out.println(line);
}
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
When I run the above program it is printing the variable u_id correctly but it is printing
ajunk value - "546865726520617265206E6F206E6577206D6573736167657320666F7220796F752E" for
the variable line instead of printing "There are no new messages for you." The fileName
field is of type blob in MySQL database. There seems to be some problemwith the way the
data is being retrieved from the database. Is any MySQL driver like
oracle.jdbc.driver.OracleDriver available? Please help.

Thank you.

by okujava

i would suggest first to switch the lines:

InputStream in = rs.getAsciiStream("fileName");
u_id = rs.getString("user_id");

to:

u_id = rs.getString("user_id");
InputStream in = rs.getAsciiStream("fileName");

because the stream is closed when you make getXXX().

if that doesn't help, try the following:

Blob b = rs.getBlob("fileName");
byte bytes[] = b.getBytes();
System.out.println(New String(bytes));

hope it helps.

cheers
second try to

This discussion is closed to new replies.