java.sql.SQLException:ORA-01704:字符串文字太长时插入或更新

时间:2014-02-10 06:22:54

标签: java oracle jdbc struct clob

通常当我插入4000个字符限制时它的工作正常但是当超过4000个字符时,它抛出SQL异常字符串文字太长,即使我的DISCHARGE_TEXT数据类型是CLOB

我的JavaScript代码

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

我的操作类代码

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";
}

我的SQL代码

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

我的表数据类型

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
)

4 个答案:

答案 0 :(得分:1)

在你的SQL编辑器中,通过输入静态值来运行更新或插入查询,并检查是否有错误。

答案 1 :(得分:1)

您收到此错误是因为您的SQL语句(这是此语句的结果:

 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 + "')";
            }

太长了。

以这种方式构建SQL语句(通过将变量值连接在一起)是一个非常糟糕的主意,因为

  • 它会对性能产生负面影响(强制数据库解析一个 每次都有新的声明)
  • 它允许SQL注入攻击
  • 它限制了您可以插入的数据量,因为您的SQL语句的长度不得超过4000个字符

解决方案:使用带有绑定变量的Prepared Statement

答案 2 :(得分:0)

您必须根据您的要求指定DISCHARGE_TEXT CLOB(64 K)或类似内容。 看到这个定义。

CLOB数据类型

CLOB(字符大对象)值最长可达2,147,483,647个字符。 CLOB用于存储基于字符的unicode数据,例如任何字符集中的大型文档。

除非给出其中一个后缀K,M或G,否则CLOB的长度以数字字符给出,分别与1024,1024 * 1024,1024 * 1024 * 1024的倍数相关。

长度以CLOB的字符(unicode)指定。

答案 3 :(得分:0)

我只是解决了这个问题,将SQL语句更改为准备语句

这是我新的SQL代码

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 referenced_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;
    }