日期:2014-05-16  浏览次数:20579 次

ORACLE报文字字符串过长错误
对LONG类型插入文本长度超过4000字节,报错:
ORA-01704: 文字字符串过长

SQL语句中直接插入超过4000字节的问题暂未能解决。
在JAVA中可如下处理:

****************************************
使用JDBC存取ORACLE中的LONG类型的数据
****************************************

插入LONG类型的数据

    BufferedReader bufReader = new BufferedReader(new FileReader(file));
    Integer id = Integer.valueOf(PubFun1.CreateMaxNo(TEST_LONG_ID, 1));
    PreparedStatement pstmt = con.prepareStatement(INSERT_LONG_SQL);
    pstmt.setObject(1, id);
    pstmt.setObject(2, fileName);
    pstmt.setCharacterStream(3, bufReader, (int) length);
    int retValue = pstmt.executeUpdate();
    if (retValue != 1) {
        logger.error("Error on insert value");
    }
    bufReader.close();
    pstmt.close();

INSERT_LONG_SQL的值为:
INSERT INTO T_LONGTEST(ID, FILENAME, CONTENT) VALUES (?, ?, ?)
注意需要使用setCharacterStream方法设置LONG类型的字段的值

读取LONG类型的数据

读取也需要使用Stream的方式来读取,下面的代码片断说明了读取LONG类型的字段的方法.
    PreparedStatement pstmt = con.prepareStatement(QUERY_LONG_COL_SQL);
    pstmt.setObject(1, id);
    ResultSet rs = pstmt.executeQuery();
    if (rs.next()) {
        Reader reader = rs.getCharacterStream(1);
        BufferedReader bufReader = new BufferedReader(reader);
        StringBuffer strBuf = new StringBuffer();
        String line;
        while ((line = bufReader.readLine()) != null) {
            strBuf.append(line);
            strBuf.append("/r/n");
        }
        bufReader.close();
        System.out.println("The content is:" + strBuf.toString());
    }
QUEYR_LONG_COL_SQL的取值为:SELECT CONTENT FROM T_LONGTEST WHERE ID=?