Code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class BlobUse
{
Connection connection;
Statement statement;
ResultSet resultSet;
public void generateReport()
{
try
{
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
String url = "jdbc:db2://localhost:50000/smartcit";
Connection connection = DriverManager.getConnection(url, "db2admin", "password");
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO ADMINISTRATOR.BOOKCOVERS VALUES(?,?)");
File imageFile = new File("c:\\redbookcover.jpg");
InputStream inputStream = new FileInputStream(imageFile);
preparedStatement.setString(1," 0738425826");
preparedStatement.setBinaryStream(2,inputStream,(int)(imageFile.length()));
preparedStatement.executeUpdate();
connection.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
System.out.println("SQL Exception");
System.out.println("=============");
System.out.println(e.getMessage()); // (1)
System.out.println("");
}catch (IllegalAccessException ex) {System.err.println(ex.getMessage());}
catch (InstantiationException ex) {System.err.println(ex.getMessage());}
}
public static void main(String[] args)
{
new BlobUse().generateReport();
}
}
I created the table using query: create table bookcovers (bookisbn varchar(10) not null, bookcover blob (1G)
not null, primary key(bookisbn))
This is the example program from IBM site to store an image on database from
IBM site but I am unable to run it sucessfully. Please help