JDBC操做DB2 Clob、Blob字段的Bug探究
J2EE的開發之路佈滿了陷阱,相信不少人都深有體會。
IBM的東西就是傻大笨粗,巨難用!!!
最近再用JDBC操做DB2的Clob和Blob時候出現了些問題:
我用的是DB2 V9安裝程序自帶的驅動,相傳DB2驅動不少,這個最經常使用,也是最規範一個。
我用的驅動名字以下:
db2jcc.jar
db2jcc_license_cisuz.jar
db2jcc_license_cu.jar
Clob出現的問題:寫入Clob沒有問題,讀取出問題。
Blob出現的問題:都寫一般狀況下沒有問題。但當同時寫入Data和Blob字段時候,會出問題。(一個例子中遇到的,不足以給出確定結果)。
拋出的異常代碼以下:
com.ibm.db2.jcc.c.SqlException
at com.ibm.db2.jcc.c.ec.<init>(ec.java:183)
at com.ibm.db2.jcc.b.d.b(d.java:1328)
at com.ibm.db2.jcc.c.s.a(s.java:748)
at com.ibm.db2.jcc.c.s.U(s.java:1393)
at com.ibm.db2.jcc.c.wf.getClob(wf.java:914)
......
爲測試搭建環境:
CREATE
TABLE ZFZVF.T_LOB (
NAME
VARCHAR(24),
TXT CLOB(2M) LOGGED
NOT COMPACT,
IMG BLOB(2M) LOGGED
NOT COMPACT
);
DROP
TABLE ZFZVF.NJ_GT;
COMMIT;
CREATE
TABLE ZFZVF.NJ_GT (
ID
BIGINT,
ZTBS
BIGINT,
NJBS
BIGINT,
ND
VARCHAR(4),
NJNR BLOB(2M) LOGGED
NOT COMPACT,
SJC
TIMESTAMP
);
COMMIT;
COMMENT
ON
TABLE ZFZVF.NJ_GT
IS
'年檢_個體';
COMMENT
ON ZFZVF.NJ_GT (
ID
IS
'ID',
ZTBS
IS
'主體BS',
NJBS
IS
'年檢BS',
ND
IS
'年度',
NJNR
IS
'年檢內容',
SJC
IS
'時間戳' );
COMMIT;
例子以下:
package lob;
import java.io.*;
import java.sql.*;
/**
* DB2 Clob、Blob Bug探究
* File: TestLob4DB2.java
* User: leizhimin
* Date: 2008-3-3 8:56:00
*/
public
class TestLob4DB2{
public
static
final String url =
"jdbc:db2://127.0.0.1:50000/zfzvf";
public static final String username = "zfzvf";
public static final String password = "zfzvfdb2";
public static final String driverClassName = "com.ibm.db2.jcc.DB2Driver";
/**
* 獲取數據庫鏈接Connection
*
* @return 數據庫鏈接Connection
*/
public static Connection makeConnection() {
Connection conn = null;
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 測試鏈接
*/
public static void testConnection() {
Connection conn = makeConnection();
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM ZFZVF.DM_HYML");
while (rs.next()) {
String s1 = rs.getString(1);
String s2 = rs.getString(2);
System.out.println(s1 + s2);
}
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 插入文件測試
*/
public static void testInsertlob() {
Connection conn = makeConnection();
try {
conn.setAutoCommit(false);
File txtFile = new File("C:\\txt.txt");
File imgFile = new File("C:\\img.png");
int txt_len = (int) txtFile.length();
int img_len = (int) imgFile.length();
try {
InputStream fis1 = new FileInputStream(txtFile);
InputStream fis2 = new FileInputStream(imgFile);
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO ZFZVF.T_LOB(NAME,TXT,IMG) VALUES('G',?,?)");
pstmt.setAsciiStream(1, fis1, txt_len);
pstmt.setBinaryStream(2, fis2, img_len);
pstmt.executeUpdate();
conn.commit();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 查詢DB2 Clob測試 ,會拋出異常
*/
public static void testQueryLob() {
Connection conn = makeConnection();
try {
conn.setAutoCommit(true);
PreparedStatement stmt = conn.prepareStatement("SELECT TXT,IMG FROM ZFZVF.T_LOB");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Clob clob = rs.getClob("TXT");
Blob blob = rs.getBlob("IMG");
InputStreamReader ir = (InputStreamReader) clob.getCharacterStream();
File fileOutput = new File("C:\\txt_1.txt");
FileOutputStream fo = new FileOutputStream(fileOutput);
int c;
while ((c = ir.read()) != -1) {
fo.write(c);
break;
}
fo.close();
}
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 查詢DB2 Blob測試
*/
public static void testBlobQuery() {
Connection conn = makeConnection();
try {
conn.setAutoCommit(true);
PreparedStatement stmt = conn.prepareStatement("SELECT img FROM ZFZVF.T_LOB");
ResultSet rs = stmt.executeQuery();
int i = 0;
while (rs.next()) {
Blob blob = rs.getBlob(1);
InputStream inputStream = blob.getBinaryStream();
File fileOutput = new File("c:\\str_x" + i + ".txt");
FileOutputStream fo = new FileOutputStream(fileOutput);
int c;
while ((c = inputStream.read()) != -1)
fo.write(c);
fo.close();
System.out.println("Blob " + i + " retrieved!!");
i++;
}
} catch (SQLException e) {
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 查詢DB2 Clob測試 ,會拋出異常
*/
public static void testClobQuery() {
Connection conn = makeConnection();
try {
conn.setAutoCommit(false);
PreparedStatement stmt = conn.prepareStatement("SELECT TXT FROM ZFZVF.T_LOB");
ResultSet rs = stmt.executeQuery();
conn.commit();
while (rs.next()) {
Clob clob = rs.getClob(1);
InputStream is = clob.getAsciiStream();
File fileOutput = new File("C:\\ttttt.txt");
FileOutputStream fo = new FileOutputStream(fileOutput);
int c;
while ((c = is.read()) != -1)
fo.write(c);
fo.close();
break;
}
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) {
testConnection();
testInsertlob();
// testQueryLob();
testBlobQuery();
testClobQuery();
}
}
一一運行各個測試方法,能夠看到操做Clob時候拋出了異常。
因爲DB2驅動比較多,而通常都用測試用的這個驅動作開發,所以我也沒有嘗試用別的驅動了。對於這個問題,我本身用Blob來存儲本應用Clob來處理的字段,也就是將原Clob定義爲Blob,經過Blob來操做。具體代碼就不贅述了。
因爲大量使用Blob,又從中發現個問題,就是日期和Blob同時插入時會出一些問題。有興趣的博友能夠嘗試嘗試,最近很忙,也沒有那麼精力去細究。但願IBM能儘快解決這些問題。