AndreaNobili
AndreaNobili

Reputation: 42957

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

I am working on an old legacy Java application and I have some problem with this method that perform a simple insert query on an Oracle database:

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;

}    

The problem is that the String fatturaXml paramether represent an XML file and it is pretty big. So when the previous query is performed I obtain that this exception is thrown:

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

How can I solve this issue and correctly insert the record?

Tnx

Upvotes: 0

Views: 1458

Answers (2)

Norbert
Norbert

Reputation: 6084

You could use a pretty old method I used on an IBM mainframe to store large data: Split the string into chunks, store the chunks:

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

And a loop:

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();
}

P.S. Do not use your query.append code: It is sensitive to SQL injection and unescaped data. Use PreparedStatement instead.

Upvotes: 0

Kalyan Chavali
Kalyan Chavali

Reputation: 1348

If one tries to insert data which is greater than 4000 characters into a column - VARCHAR2 , they would the error ORA-01704. Check the below SO post

Error : ORA-01704: string literal too long

Upvotes: 1

Related Questions