//DB2插入blob字段数据
public void insertBlobData()
{
String url = "jdbc:db2:tar20";
String user = "db2admin";
String password = "target";
try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement preparedStatement =
conn.prepareStatement(
"INSERT INTO SHERP.TAR_T_EXECINFOFILE("
+
"TASKCODE, EXECTIME, SORTNO, FILEID, FILENAME, FILEDETAIL)"
+ " VALUES(?,?,?,?,?,?)");
File docFile =
new File(
"e://test.doc");
InputStream inputStream = new FileInputStream(docFile);
preparedStatement.setInt(1, 44);
preparedStatement.setString(2, "2006-08-08");
preparedStatement.setInt(3, 1);
preparedStatement.setInt(4, 1);
preparedStatement.setString(5, "测试");
preparedStatement.setBinaryStream(6, inputStream,
(int) (docFile.length()));
preparedStatement.executeUpdate();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} catch (IllegalAccessException iae) {
iae.printStackTrace();
} catch (InstantiationException ie) {
ie.printStackTrace();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
}
//获取blob字段数据
public void getBlobData()
{
String url = "jdbc:db2:tar20";
String user = "db2admin";
String password = "target";
try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement preparedStatement =
conn.prepareStatement(
"SELECT FILEDETAIL FROM SHERP.TAR_T_EXECINFOFILE WHERE FILEID=?");
preparedStatement.setInt(1, 3);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Blob blob = resultSet.getBlob("FILEDETAIL");
InputStream inputStream = blob.getBinaryStream();
File fileOutput =
new File(
"e://demo777.doc");
FileOutputStream fo =
new FileOutputStream(fileOutput);
int c;
while ((c = inputStream.read()) != -1) {
fo.write(c);
}
fo.close();
}
} catch (ClassNotFoundException ex) {
} catch (IllegalAccessException ex) {
} catch (InstantiationException ex) {
} catch (SQLException ex) {
/** @todo Handle this exception */
} catch (FileNotFoundException ex) {
/** @todo Handle this exception */
} catch (IOException ex) {
/** @todo Handle this exception */
}
}
//QueryDataSet获取blob数据
/**
* 此方法和getBytes的不同之处是,这里需要两个字符转成一个字节码。
* @param str String
* @return byte[]
*/
private byte[] str2ByteArray(String str) {
int length = 0;
if (str.length() % 2 == 0) {
length = str.length() / 2;
} else {
length = str.length() / 2 + 1;
}
byte[] result = new byte[length];
String s = "";
int j = 0;
for (int i = 0; i < str.length(); i += 2) {
if ((i + 2) < str.length()) {
s = str.substring(i, i + 2);
} else {
s = str.substring(i, str.length());
}
if (!s.equals("") && !s.equals("##")) {
byte b = new Integer(Integer.parseInt(s, 16)).byteValue();
result[j++] = b;
}
}
return result;
}
//测试blob字段处理
public void btnTest_actionPerformed(ActionEvent e) {
String url = "jdbc:db2:tar20";
String user = "db2admin";
String password = "target";
try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
Connection conn = DriverManager.getConnection(url, user, password);
Database db = new Database(conn);
String sql =
"select FILEDETAIL from SHERP.TAR_T_EXECINFOFILE where FILEID=1";
qdsTemp.setQuery(new QueryDescriptor(db, sql));
qdsTemp.executeQuery();
String str = qdsTemp.getString("FILEDETAIL");
InputStream in = new ByteArrayInputStream(this.str2ByteArray(str));
FileOutputStream out = new FileOutputStream("E://demo888.doc");
int c = 0;
while ((c = in.read()) != -1) {
out.write(c);
}
in.close();
out.close();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} catch (IllegalAccessException iae) {
iae.printStackTrace();
} catch (InstantiationException ie) {
ie.printStackTrace();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (FileNotFoundException ex) {
/** @todo Handle this exception */
} catch (IOException ex) {
/** @todo Handle this exception */
}
}
public void insertBlobData()
{
String url = "jdbc:db2:tar20";
String user = "db2admin";
String password = "target";
try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement preparedStatement =
conn.prepareStatement(
"INSERT INTO SHERP.TAR_T_EXECINFOFILE("
+
"TASKCODE, EXECTIME, SORTNO, FILEID, FILENAME, FILEDETAIL)"
+ " VALUES(?,?,?,?,?,?)");
File docFile =
new File(
"e://test.doc");
InputStream inputStream = new FileInputStream(docFile);
preparedStatement.setInt(1, 44);
preparedStatement.setString(2, "2006-08-08");
preparedStatement.setInt(3, 1);
preparedStatement.setInt(4, 1);
preparedStatement.setString(5, "测试");
preparedStatement.setBinaryStream(6, inputStream,
(int) (docFile.length()));
preparedStatement.executeUpdate();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} catch (IllegalAccessException iae) {
iae.printStackTrace();
} catch (InstantiationException ie) {
ie.printStackTrace();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
}
//获取blob字段数据
public void getBlobData()
{
String url = "jdbc:db2:tar20";
String user = "db2admin";
String password = "target";
try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement preparedStatement =
conn.prepareStatement(
"SELECT FILEDETAIL FROM SHERP.TAR_T_EXECINFOFILE WHERE FILEID=?");
preparedStatement.setInt(1, 3);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Blob blob = resultSet.getBlob("FILEDETAIL");
InputStream inputStream = blob.getBinaryStream();
File fileOutput =
new File(
"e://demo777.doc");
FileOutputStream fo =
new FileOutputStream(fileOutput);
int c;
while ((c = inputStream.read()) != -1) {
fo.write(c);
}
fo.close();
}
} catch (ClassNotFoundException ex) {
} catch (IllegalAccessException ex) {
} catch (InstantiationException ex) {
} catch (SQLException ex) {
/** @todo Handle this exception */
} catch (FileNotFoundException ex) {
/** @todo Handle this exception */
} catch (IOException ex) {
/** @todo Handle this exception */
}
}
//QueryDataSet获取blob数据
/**
* 此方法和getBytes的不同之处是,这里需要两个字符转成一个字节码。
* @param str String
* @return byte[]
*/
private byte[] str2ByteArray(String str) {
int length = 0;
if (str.length() % 2 == 0) {
length = str.length() / 2;
} else {
length = str.length() / 2 + 1;
}
byte[] result = new byte[length];
String s = "";
int j = 0;
for (int i = 0; i < str.length(); i += 2) {
if ((i + 2) < str.length()) {
s = str.substring(i, i + 2);
} else {
s = str.substring(i, str.length());
}
if (!s.equals("") && !s.equals("##")) {
byte b = new Integer(Integer.parseInt(s, 16)).byteValue();
result[j++] = b;
}
}
return result;
}
//测试blob字段处理
public void btnTest_actionPerformed(ActionEvent e) {
String url = "jdbc:db2:tar20";
String user = "db2admin";
String password = "target";
try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
Connection conn = DriverManager.getConnection(url, user, password);
Database db = new Database(conn);
String sql =
"select FILEDETAIL from SHERP.TAR_T_EXECINFOFILE where FILEID=1";
qdsTemp.setQuery(new QueryDescriptor(db, sql));
qdsTemp.executeQuery();
String str = qdsTemp.getString("FILEDETAIL");
InputStream in = new ByteArrayInputStream(this.str2ByteArray(str));
FileOutputStream out = new FileOutputStream("E://demo888.doc");
int c = 0;
while ((c = in.read()) != -1) {
out.write(c);
}
in.close();
out.close();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} catch (IllegalAccessException iae) {
iae.printStackTrace();
} catch (InstantiationException ie) {
ie.printStackTrace();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (FileNotFoundException ex) {
/** @todo Handle this exception */
} catch (IOException ex) {
/** @todo Handle this exception */
}
}