Blob using MySQL

M

Moerderin

i've been doing ton's of reading these past couple of days about how
to create a blob and this is about as far as i was able to get. (the
code still doesn't work) I was wondering if any computer experts out
there could help me figure out what my code is lacking or what kind
of revisions it might need, thank you

public static void main(String [] args) {
int id = 1;
byte [] data = new byte[255];
int i = 0;
Access test = new Access();
for (i=0;i<255;i++)
data = 'x';
try {
Blob blob = null;
test.connect("com.mysql.jdbc.Driver","jdbc:mysql://localhost/mdars","bfaul","123");
ResultSet rs = test.query("Select * from FrameData where
ID="+id);

rs.next();

blob = rs.getBlob("RawData");
PreparedStatement pstmt = test.dbConn.prepareStatement(
"UPDATE FrameData SET RawData = ? WHERE ID = "+id);

blob.setBytes(0,data);
pstmt.setBlob(1, blob);

pstmt.executeUpdate();
} catch (Exception error) {
error.printStackTrace();
}
}
 
S

Sudsy

Moerderin said:
i've been doing ton's of reading these past couple of days about how
to create a blob and this is about as far as i was able to get. (the
code still doesn't work) I was wondering if any computer experts out
there could help me figure out what my code is lacking or what kind
of revisions it might need, thank you

public static void main(String [] args) {
int id = 1;
byte [] data = new byte[255];
int i = 0;
Access test = new Access();
for (i=0;i<255;i++)
data = 'x';
try {
Blob blob = null;
test.connect("com.mysql.jdbc.Driver","jdbc:mysql://localhost/mdars","bfaul","123");
ResultSet rs = test.query("Select * from FrameData where
ID="+id);


Add "FOR UPDATE" at the end of your SQL statement. Also, turn off
auto-commit beforehand, manually commit, reset auto-commit after.
For some sample code using Oracle, see the following:
<http://www.sudsy.net/technology/clobs.html>
 
I

Ike

google up (by searching groups) on all keywords in the following line:

blob ike rxv java "ObjectInput/Output streams and byte[]"

-Ike
 
A

Alexandr Molochnikov

You did not say in what way the code fails, so my advice may not apply.
Having said this: try setting the blob bytes in your PreparedStatement like
this:

pstmt.setBytes(index, buffer);

HTH

Alex Molochnikov
Gestalt Corporation
www.gestalt.com
 
M

Moerderin

Alexandr Molochnikov said:
You did not say in what way the code fails, so my advice may not apply.
Having said this: try setting the blob bytes in your PreparedStatement like
this:

pstmt.setBytes(index, buffer);

HTH

Alex Molochnikov
Gestalt Corporation
www.gestalt.com

Moerderin said:
i've been doing ton's of reading these past couple of days about how
to create a blob and this is about as far as i was able to get. (the
code still doesn't work) I was wondering if any computer experts out
there could help me figure out what my code is lacking or what kind
of revisions it might need, thank you

public static void main(String [] args) {
int id = 1;
byte [] data = new byte[255];
int i = 0;
Access test = new Access();
for (i=0;i<255;i++)
data = 'x';
try {
Blob blob = null;
test.connect("com.mysql.jdbc.Driver","jdbc:mysql://localhost/mdars","bfaul",
"123");
ResultSet rs = test.query("Select * from FrameData where
ID="+id);

rs.next();


// first line of interest
// second line of interest
crap, i forgot to tell you where my code fails. at the "first line of
interest" no value is stored in the blob, it gets the value of "null",
then at the second line of interest it throws a null pointer exception
which makes sence because blob has a value of "null"
 
A

Alexandr Molochnikov

Moerderin said:
// first line of interest

// second line of interest

crap, i forgot to tell you where my code fails. at the "first line of
interest" no value is stored in the blob, it gets the value of "null",
then at the second line of interest it throws a null pointer exception
which makes sence because blob has a value of "null"

So, are you saying that the blob comes as null when it is not supposed to
be? Because if it is null _legitimately_, then your code should handle this
condition separately:

if (buffer != null)
pstmt.setBytes(index, buffer);
else
pstmt.setNull(index, Types.BLOB);

AM
 
J

John Fereira

Moerderin said:
i've been doing ton's of reading these past couple of days about how
to create a blob and this is about as far as i was able to get. (the
code still doesn't work) I was wondering if any computer experts out
there could help me figure out what my code is lacking or what kind
of revisions it might need, thank you

public static void main(String [] args) {
int id = 1;
byte [] data = new byte[255];
int i = 0;
Access test = new Access();
for (i=0;i<255;i++)
data = 'x';
try {
Blob blob = null;
test.connect("com.mysql.jdbc.Driver","jdbc:mysql://localhost/mdar
s","bfaul","123"); ResultSet rs = test.query("Select * from
FrameData where
ID="+id);


Add "FOR UPDATE" at the end of your SQL statement. Also, turn off
auto-commit beforehand, manually commit, reset auto-commit after.
For some sample code using Oracle, see the following:
<http://www.sudsy.net/technology/clobs.html>


If you're using the default table types for MySQL the commit/rollback
methods are just noops anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top