Loading a blob record

Hi,In my program I must put some new record in a database. The problems come when I have to load the column of blob type. I'd want to put into that field the contents of an array of byte[ ].
[205 byte] By [IronBargiaa] at [2007-11-26 22:34:04]
# 1

Assuming you are using ResultSet:

Look into the ResultSet's getBlob methods, then check out Blob's methods. This might take you a whole minute to do and I guarantee you'll find what you want.

[url=http://java.sun.com/j2se/1.5.0/docs/api/]API[/url]s are your friend. You should check them for what you need first (or google), then post here if you cannot find your answer.

ignignokt84a at 2007-7-10 11:41:39 > top of Java-index,Java Essentials,Java Programming...
# 2
CROSSPOST: http://forum.java.sun.com/thread.jspa?threadID=5151643
DrLaszloJamfa at 2007-7-10 11:41:39 > top of Java-index,Java Essentials,Java Programming...
# 3
And it was answered in your previous post: http://forum.java.sun.com/thread.jspa?threadID=5151028
DrLaszloJamfa at 2007-7-10 11:41:39 > top of Java-index,Java Essentials,Java Programming...
# 4
I tried this way: > ResultSet res = stmt.executeQuery("select * from myTable");> byte [ ] data = .............> res.next(); > res.getBlob("data").setBytes(1,data);Now the problem is: NullPointerException, because res.getBlob("data") is null..
IronBargiaa at 2007-7-10 11:41:39 > top of Java-index,Java Essentials,Java Programming...
# 5
Please post your exact code using [ code]*your code*[ /code] tags.
ignignokt84a at 2007-7-10 11:41:39 > top of Java-index,Java Essentials,Java Programming...
# 6

This is my exact code

//Connecting database

MyDBConnection mdbc=new MyDBConnection();

mdbc.init();

Connection conn=mdbc.getMyConnection();

try {

Statement stmt= conn.createStatement();

} catch (SQLException ex) {

ex.printStackTrace();

}

// Other code

public void updateDataBase(String data1, byte[] data){

int u = stmt.executeUpdate("insert into myTable (column1) values("

+quotate(data1)+" );");

ResultSet res = stmt.executeQuery("select * from myTable");

res.next();

res.next();

res.getBlob("data").setBytes(1,data);

}

//To easily make quotes

private String quotate(String content) {

return "'"+content+"'";

}

IronBargiaa at 2007-7-10 11:41:39 > top of Java-index,Java Essentials,Java Programming...
# 7
That was an SQL SELECT statement. To insert data, use an SQL INSERT statement.
DrLaszloJamfa at 2007-7-10 11:41:39 > top of Java-index,Java Essentials,Java Programming...
# 8
By the way: "data" is also the name of the blob column.
IronBargiaa at 2007-7-10 11:41:39 > top of Java-index,Java Essentials,Java Programming...
# 9
I thought I before have to insert the new row leaving the blob field empty, and than get the blob and update it with the right data.
IronBargiaa at 2007-7-10 11:41:39 > top of Java-index,Java Essentials,Java Programming...
# 10
> I thought I before have to insert the new row leaving> the blob field empty, and than get the blob and> update it with the right data.You don't have to do that. It's better to keep it simple.
DrLaszloJamfa at 2007-7-10 11:41:39 > top of Java-index,Java Essentials,Java Programming...
# 11
How can I do that in an easier way?
IronBargiaa at 2007-7-10 11:41:39 > top of Java-index,Java Essentials,Java Programming...
# 12
As I wrote, simply INSERT.
DrLaszloJamfa at 2007-7-10 11:41:39 > top of Java-index,Java Essentials,Java Programming...
# 13

I tried to make it using "Insert":

byte[] data = new byte[]{0x01, 0x02};

int u = stmt.executeUpdate("insert into myTable (column1, blobColumn) values(" +quotate(dato_TX_RX)+", "+data+" );");

the problem is that if I look into the database, in the blob field there is only the memory address of the array.

IronBargiaa at 2007-7-10 11:41:39 > top of Java-index,Java Essentials,Java Programming...
# 14
My mistake, I thought this was stated already, perhaps in the other thread:Always use a PreparedStatement. Never use just a Statement. With PreparedStatement, you use the setBinaryStream method to set blob data.
DrLaszloJamfa at 2007-7-10 11:41:39 > top of Java-index,Java Essentials,Java Programming...
# 15
I'll try to make it on my own, but if you could give me an example I'll be grateful to you.
IronBargiaa at 2007-7-21 18:48:40 > top of Java-index,Java Essentials,Java Programming...
# 16

PreparedStatement pstmt = con.prepareStatement("insert into tablename (columnname) values ( ? )");

pstmt.setBinaryStream(1, in, length);

pstmt.executeUpdate();

Again, you should get in the habit of using only PreparedStatements.

They have many advantages over plain Statements.

DrLaszloJamfa at 2007-7-21 18:48:40 > top of Java-index,Java Essentials,Java Programming...