Thomas Kwan wrote:
< ... >
> Hello Christian,
>
> Yes, please, an example will help a lot. I did not
> use PreparedStatement before.
>
> thomas
Hi Thomas, James and others
In this example I assume, that you have successfully loaded the twz or mm driver.
Also you should already be connected to the database of your choice through a
java.sql.Connection object called "con".
The bytes are read from a file "test.gif".
import java.lang.*;
import java.sql.*;
import java.io.*;
...
// read the bytes
byte[] image = new byte[80 * 1024];
byte[] realImage;
FileInputStream gifImage = new FileInputStream("test.gif");
int count = gifImage.read( image );
realImage = new byte[ count ];
System.arraycopy( image, 0, realImage, 0, count );
// create a PreparedStatement for storing the bytes into a table like this:
// id INT NOT NULL PRIMARY KEY AUTO_INCREMENT
// image_name VARCHAR(30) NOT NULL
// image_bin MEDIUMBLOB
PreparedStatement prep;
prep = con.prepareStatement("INSERT INTO table (id,image_name,image_bin)"
+ " VALUES (?,?,?)"
);
// ensure, the internal buffers can handle up to 80KBytes
prep.setMaxFieldSize( 80 * 1024 );
// set the values to store:
prep.setNull( 1, Types.INTEGER );
prep.setString( 2, "test image No.1" );
prep.setBytes( 3, realImage );
// start the query
prep.executeUpdate();
// stop binary processing
prep.close();
....
You obviously have to catch yourself all IOExceptions and SQLExceptions that can occur in
this piece of code.
Hope this helps.
Christian