簡體   English   中英

如何避免此java.sql.SQLException:ORA-01704:字符串文字拋出的時間太長?

[英]How can I avoid that this java.sql.SQLException: ORA-01704: string literal too long is thrown?

我正在使用舊的舊版Java應用程序,但是此方法在對Oracle數據庫執行簡單插入查詢的方法上存在一些問題:

private boolean insertFlussoXmlsdi(DBOperatore op, String numeroFattura, String dataFattura, String fatturaXml) {

    StringBuffer query = new StringBuffer();

    query.append("INSERT INTO FLUSSO_XMLSDI (NUMERO_FATTURA, DATA_EMISSIONE, XML) VALUES (");
    query.append(numeroFattura);
    query.append(", date'");
    query.append(dataFattura);
    query.append("', '");
    query.append(fatturaXml);
    query.append("')");


    try {
        Statement stmt = op.getConnessione().createStatement();
        stmt.execute(query.toString());

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

        TraceLog.scrivi("INSERIMENTO FATTURA", "ERRORE inserimento fattura con numero fattura: " + numeroFattura, false, TraceLog.lowConsole + TraceLog.highTrace + TraceLog.highLog);

        return false;
    }

    TraceLog.scrivi("INSERIMENTO FATTURA", "Inserimento fattura con numero fattura: " + numeroFattura, false, TraceLog.lowConsole + TraceLog.highTrace + TraceLog.highLog);

    return true;

}    

問題是String fatturaXml參數代表一個XML文件,並且它很大。 因此,當執行上一個查詢時,我會拋出此異常:

java.sql.SQLException: ORA-01704: string literal too long

如何解決此問題並正確插入記錄?

特納克斯

如果嘗試將大於4000個字符的數據插入到列VARCHAR2中,則會出現錯誤ORA-01704。 檢查下面的SO帖子

錯誤:ORA-01704:字符串文字太長

您可以使用我在IBM大型機上使用的一種相當古老的方法來存儲大數據:將字符串拆分為塊,然后存儲塊:

CREATE TABLE mystore(referenceid int, somecounter int, data varchar(255));

和一個循環:

public void insertSomeData(Connection connection,String inputData,int referenceId) {
    int i=0;
    String insertSql="INSERT INTO mystore (referenceid,somecounter,data) VALUES (?,?,?)";
    PreparedStatement pstmt=connection.prepareStatement(insertSql);
    int i=0;
    boolean keepGoing=true;
    while(keepGoing) {
        // Take 120 bytes only: Just in case of double byte encoding
        String substr=new String();
        if(inputData.length()>120) {
            substr=inputData.substring(0,120);
            inputData=inputData.substring(120);
        }
        else {
            substr=inputData;
            // Lets get out of this loop
            keepGoing=false;
        }
        pstmt.setInt(1,referenceId);
        pstmt.setInt(2,i);
        pstmt.setString(3,substr);
        pstmt.execute();
        pstmt.clearBatch();
        i++;
    }
    pstmt.close();
}

PS不要使用您的query.append代碼:它對SQL注入和未轉義的數據敏感。 請改用PreparedStatement。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM