import java.sql.*;
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import com.ibm.db2.jcc.*;
import COM.ibm.db2.jdbc.app.*;
public class TESTBLOB {
//DB2插入blob字段数据
public void insertBlobData()
{
String sDBDriver = "com.ibm.db2.jcc.DB2Driver";
String sConnDataBase = "jdbc:db2://192.168.12.24:50000/TAXEXDB";
// String sDBDriver = "COM.ibm.db2.jdbc.app.DB2Driver";
// String sConnDataBase = "jdbc:db2:TAXEXDB";
String sConnUser = "db2inst1";
String sConnPassd = "passw0rd";
Connection con;
Statement stmt;
ResultSet rs;
System.out.println("ok0");
try {
Class.forName(sDBDriver);
System.out.println("ok1");
con = DriverManager.getConnection(sConnDataBase, sConnUser, sConnPassd);
System.out.println("ok2");
PreparedStatement preparedStatement =
con.prepareStatement(
"INSERT INTO test2(f1)"
+ " VALUES(?)");
System.out.println("ok3");
File imgFile = new File("c:\\s1.jpg");
InputStream inputStream = new FileInputStream(imgFile);
preparedStatement.setBinaryStream(1, inputStream,
(int) (imgFile.length()));
System.out.println("ok4");
preparedStatement.executeUpdate();
System.out.println("ok5");
preparedStatement.close();
con.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
//取blob字段数据
public void getBlobData()
{
String sDBDriver = "com.ibm.db2.jcc.DB2Driver";
String sConnDataBase = "jdbc:db2://192.168.12.24:50000/TAXEXDB";
// String sDBDriver = "COM.ibm.db2.jdbc.app.DB2Driver";
// String sConnDataBase = "jdbc:db2:TAXEXDB";
String sConnUser = "db2inst1";
String sConnPassd = "passw0rd";
Connection con;
Statement stmt;
ResultSet rs;
System.out.println("ok0");
try {
Class.forName(sDBDriver);
System.out.println("ok1");
con = DriverManager.getConnection(sConnDataBase, sConnUser, sConnPassd);
System.out.println("ok2");
PreparedStatement preparedStatement =
con.prepareStatement(
"SELECT f1 FROM test2");
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Blob blob = resultSet.getBlob("f1");
InputStream inputStream = blob.getBinaryStream();
File fileOutput =
new File("c:\\news1.jpg");
FileOutputStream fo =
new FileOutputStream(fileOutput);
int c;
while ((c = inputStream.read()) != -1) {
fo.write(c);
}
fo.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
TESTBLOB test = new TESTBLOB();
test.insertBlobData();
test.getBlobData();
}
}