Question

Normally when i insert 4000 character limit its works fine but when over 4000 character its throw SQL Exception string literal too long even my DISCHARGE_TEXT data type is CLOB

My JavaScript code is

function saveAsDraftNew(){
    var admissionNo = document.getElementById("apnNo").value;
    if (admissionNo == "") {
        alert("Please Insert Admission Number");
        berak;
    } else {
        var idval = "";
        var regNo = document.getElementById("regNo").innerHTML;
        var editorValue = document.getElementById("editor").value;
        //alert(editorValue.length);
        alert(editorValue);
        editorValuee = unescape(editorValue);
        //alert(editorValuee.length);
        //alert(editorValuee);
        var referredTo = document.getElementById("otherAnswer").value = "";
        var modeOfDischarge = document.getElementsByName('answer');
        for ( var i = 0; i < modeOfDischarge.length; i++) {
            if (modeOfDischarge[i].checked) {
                var rate_value = modeOfDischarge[i].value;


                    $jf.ajax({
                                type: "POST",
                                url:"saveEditorValueAsTemplateIPDPatientInfo.do",
                                data:{
                                    patientAdmissionNo:admissionNo,
                                    editorValueM:editorValuee,
                                    btnYesM:idval,
                                    regNoM:regNo,
                                    referredToM:referredTo,
                                    rate_valueM:rate_value,
                                },
                                success : function(result) {
                                    $jf("#btndiv").html(result);
                                },
                });
            }
        }
    }
}

My Action Class Code is

private String patientAdmissionNo = null;
Date date;
String regNoM;
String btnYesM = "";
String editorValueM = "";
String referredToM = "";
String rate_valueM;

public String saveEditorValueAsTemplate() {
    String[] logInInfo = (String[]) ActionContext.getContext().getSession()
            .get(sessionVariable.getUserLogInInfo());
    String specialization_no = logInInfo[12];
    int specializationInt = specialization_no == null
            || specialization_no.isEmpty() ? 0 : Integer
            .parseInt(specialization_no);
    String doctorEmpId = logInInfo[1].toString();
    String companyNo = logInInfo[4].toString();

    Long rowInserted = patientTreatmentOfDischargeSql
            .addPatientDischargeValue(btnYesM, editorValueM,
                    patientAdmissionNo, regNoM, specializationInt,
                    doctorEmpId, companyNo, doctorEmpId, date, rate_valueM,
                    referredToM);

    if (btnYesM.equals("0") || btnYesM.equals("1")) {
        deschargeInfo.setBtnSave(false);
        deschargeInfo.setBtnSaveAsDruft(false);
        deschargeInfo.setBtnResumeOnDraft(false);
    } else {

        deschargeInfo.setBtnSave(false);
        deschargeInfo.setBtnSaveAsDruft(false);
        deschargeInfo.setBtnResumeOnDraft(true);
    }
    return "saveEditorValueAsTemplate";
}

My SQL Code is

public Long addPatientDischargeValue(String CRITICAL_FLAG , String DISCHARGE_TEXT ,String ADMISSION_NO , String REG_NO ,Integer specialization_no,String ENTERED_BY, String COMPANY_NO, String UPDATE_BY, Date UPDATED_TIMESTAMP,String mode_of_discharge, String referred_to) {
    Long rowInserted=(long) 0;

    boolean fg = true;
    con = dbc.connectDB();

    if (con == null) {
        fg = false;
    }
    if (fg) {
        try {
            st = con.createStatement();
            String countQry = "SELECT COUNT(ADMISSION_NO) COUNT FROM IP_DISCHARGE WHERE ADMISSION_NO='"
                    + ADMISSION_NO + "'";

            int countNo= 0;
            ResultSet rsCount=st.executeQuery(countQry);
            if(rsCount.next()){
                countNo = rsCount.getInt("COUNT");
            }

            String insertUpdate = null;

            if (countNo > 0) {

                insertUpdate="update ip_discharge set " +
                        " update_by    ='"+UPDATE_BY+"', " +
                        " updated_timestamp      = sysdate, " +                             
                        "  critical_flag  = '" + CRITICAL_FLAG + "' ," +
                        "  discharge_text = '" + DISCHARGE_TEXT + "', " +
                        "  mode_of_discharge = '" + mode_of_discharge + "', " +
                        "  referred_to       = '" + referred_to + "' " +
                        " where " +
                        " admission_no = '"+ ADMISSION_NO+ "'";


            } else {
                String PK_NO = generatedSequence("",
                        "SEQ_PATTYPEDTL", "D", "1", "Y");
                insertUpdate = "INSERT INTO ip_discharge (DISCHARGE_ID,ADMISSION_NO , reg_no, DISCHARGE_TEXT , specialization_no , CRITICAL_FLAG , ENTERED_BY , COMPANY_NO, mode_of_discharge, referred_to "
                        + ")values('"+ PK_NO+ "','"+ ADMISSION_NO+ "','"+ REG_NO+ "','" + DISCHARGE_TEXT + "'," + specialization_no + ",'" + CRITICAL_FLAG + "','" + ENTERED_BY + "','" + COMPANY_NO + "','" + mode_of_discharge + "','" + referred_to + "')";
            }
            System.out.println(insertUpdate);
            st.executeUpdate(insertUpdate);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                st.close();
                con.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

    return rowInserted;
}

my table data Type is

CREATE TABLE IP_DISCHARGE
(
  DISCHARGE_ID       VARCHAR2(16 BYTE),
  ADMISSION_NO       VARCHAR2(16 BYTE)          NOT NULL,
  ENTERED_BY         VARCHAR2(16 BYTE),
  ENTRY_TIMESTAMP    DATE                       DEFAULT SYSDATE,
  UPDATE_BY          VARCHAR2(16 BYTE),
  UPDATED_TIMESTAMP  DATE,
  COMPANY_NO         VARCHAR2(10 BYTE),
  DEPT_NO            NUMBER,
  CRITICAL_FLAG      VARCHAR2(1 BYTE),
  DISCHARGE_TEXT     CLOB,
  REG_NO             VARCHAR2(22 BYTE),
  MODE_OF_DISCHARGE  VARCHAR2(22 BYTE),
  REFERRED_TO        VARCHAR2(22 BYTE),
  SPECIALIZATION_NO  NUMBER
)
Was it helpful?

Solution

You're getting this error because your SQL statement (which is the result of this statement:

 insertUpdate = "INSERT INTO ip_discharge (DISCHARGE_ID,ADMISSION_NO , reg_no, DISCHARGE_TEXT , specialization_no , CRITICAL_FLAG , ENTERED_BY , COMPANY_NO, mode_of_discharge, referred_to "
                        + ")values('"+ PK_NO+ "','"+ ADMISSION_NO+ "','"+ REG_NO+ "','" + DISCHARGE_TEXT + "'," + specialization_no + ",'" + CRITICAL_FLAG + "','" + ENTERED_BY + "','" + COMPANY_NO + "','" + mode_of_discharge + "','" + referred_to + "')";
            }

is getting too long.

Building your SQL statement in this way (by concatenating variable values together) is a very bad idea because

  • it negatively impacts performance (you force the database to parse a new statement every time)
  • it allows SQL injection attacks
  • it limits the amount of data you can insert because your SQL statement must not exceed 4000 characters in length

Solution: use a Prepared Statement with bind variables instead

OTHER TIPS

In your SQL editor run update or insert query by putting static value and check are yo getting error there.

You have to specify DISCHARGE_TEXT CLOB(64 K) or something like as per your requirement. See this defination.

CLOB data type

A CLOB (character large object) value can be up to 2,147,483,647 characters long. A CLOB is used to store unicode character-based data, such as large documents in any character set.

The length is given in number characters for both CLOB, unless one of the suffixes K, M, or G is given, relating to the multiples of 1024, 1024*1024, 1024*1024*1024 respectively.

Length is specified in characters (unicode) for CLOB.

i just solve this problem to change SQL Statement to Prepared Statement

here is my new sql code

public Long addPatientDischargeValue(String CRITICAL_FLAG , String DISCHARGE_TEXT ,String ADMISSION_NO , String REG_NO ,Integer specialization_no,String ENTERED_BY, String COMPANY_NO, String UPDATE_BY, Date UPDATED_TIMESTAMP,String mode_of_discharge, String referred_to) { Long rowInserted=(long) 0;

        boolean fg = true;
        con = dbc.connectDB();

        if (con == null) {
            fg = false;
        }
        if (fg) {
            try {
                Date today = new Date();
                st = con.createStatement();
                String countQry = "SELECT COUNT(ADMISSION_NO) COUNT FROM IP_DISCHARGE WHERE ADMISSION_NO='"
                        + ADMISSION_NO + "'";

                int countNo= 0;
                ResultSet rsCount=st.executeQuery(countQry);
                if(rsCount.next()){
                    countNo = rsCount.getInt("COUNT");
                }

                String insertUpdate = null;
                String Pk_No="";
                if (countNo > 0) {

                insertUpdate=   "update ip_discharge set  " +
                    "update_by = ?,  " +
                    "updated_timestamp = ? , " +
                    "critical_flag = ? , " +
                    "discharge_text = ?,  " +
                    "mode_of_discharge = ? , " +
                    "referred_to = ?  " +
                    "where  admission_no = ?  ";
                PreparedStatement preparedStatement = con.prepareStatement(insertUpdate);

                preparedStatement.setString(1, UPDATE_BY);
                preparedStatement.setDate(2, new java.sql.Date(today.getTime()));
                preparedStatement.setString(3, CRITICAL_FLAG);
                preparedStatement.setString(4, DISCHARGE_TEXT);
                preparedStatement.setString(5, mode_of_discharge);
                preparedStatement.setString(6, "referred_to");
                preparedStatement.setString(7, ADMISSION_NO);

                    // execute update SQL stetement
                preparedStatement.executeUpdate();


                } else {
                    Pk_No = generatedSequence("","SEQ_PATTYPEDTL", "D", "1", "Y");
                    insertUpdate = "INSERT INTO ip_discharge (DISCHARGE_ID,ADMISSION_NO , reg_no, DISCHARGE_TEXT , specialization_no , CRITICAL_FLAG , ENTERED_BY , COMPANY_NO, mode_of_discharge, referred_to "
                            + ")values(?,?,?,?,?,?,?,?,?,?)";

                PreparedStatement preparedStatement = con.prepareStatement(insertUpdate);

                preparedStatement.setString(1, Pk_No);
                preparedStatement.setString(2, ADMISSION_NO);
                preparedStatement.setString(3, REG_NO);
                preparedStatement.setString(4, DISCHARGE_TEXT);
                preparedStatement.setInt(5, specialization_no);
                preparedStatement.setString(6, CRITICAL_FLAG);
                preparedStatement.setString(7, ENTERED_BY);
                preparedStatement.setString(8, COMPANY_NO);
                preparedStatement.setString(9, mode_of_discharge);
                preparedStatement.setString(10, referred_to);
                preparedStatement.executeUpdate();

                System.out.println(insertUpdate);
            }

            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                try {
                    st.close();
                    con.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }

        return rowInserted;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top