在这种情况下,如何动态地将值设置为preperaedStatement

时间:2017-10-10 12:45:05

标签: java jdbc

我正在使用Java Prepared Statement以这种方式构建动态SQL。能告诉我如何动态设置值吗?

这是我的代码:

我收到以下异常

java.sql.SQLException: Parameter index out of range (4 > number of parameters, which is 3).
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)

这是我的代码

java.sql.SQLException:参数索引超出范围(4>参数个数,即3)。     在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)     在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)     在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)     在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)     在com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3813)     在com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3795)     在com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4616)     在Test.main(Test.java:72)

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.text.ParseException;

public class Test{

    public static void main(String args[]) throws ParseException {
        try
        {

        String sqlUpdateforemp = "update emp_details set emp_name=? , emp_author = ? , emp_description = ?";


        String str_Preview_Pic = "";
        String str_Thumb_Nail_Pic = "";
        String str_How_to_Video = "";
        String str_emp_id = "1";
        PreparedStatement Stmtupdateforemp = null;
        Connection dbConnectionFitSh = null;

         Class.forName("com.mysql.jdbc.Driver");
         String sURL="jdbc:mysql:/fdsfds/fdsfse_qa";
            String sUserName="root";
            String sPwd="fdsf";
         dbConnectionFitSh = DriverManager.getConnection(sURL,sUserName,sPwd);

        StringBuffer stringbuffer_sql = new StringBuffer(sqlUpdateforemp);
        int paramInd = 0;

        if (str_Preview_Pic != null && !str_Preview_Pic.isEmpty()) {
            stringbuffer_sql.append(",emp_preview_pic=?");
        }


        if (str_Thumb_Nail_Pic != null && !str_Thumb_Nail_Pic.isEmpty()) {
            stringbuffer_sql.append(",emp_thumbnail=?");
        }


        if (str_How_to_Video != null && !str_How_to_Video.isEmpty()) {
            stringbuffer_sql.append(",emp_video=?");
        }


        stringbuffer_sql.append("where emp_id='" + str_emp_id + "'");
        Stmtupdateforemp = dbConnectionFitSh.prepareStatement(stringbuffer_sql.toString());

        Stmtupdateforemp.setString(++paramInd,  "emp_name");
        Stmtupdateforemp.setString(++paramInd,  "emp_author");
        Stmtupdateforemp.setString(++paramInd,  "emp_description");

         if (str_Preview_Pic != null && !str_Preview_Pic.isEmpty()) {
             Stmtupdateforemp.setString(++paramInd, str_Preview_Pic);
            }

            if (str_Thumb_Nail_Pic != null && !str_Thumb_Nail_Pic.isEmpty()) {
                Stmtupdateforemp.setString(++paramInd, str_Thumb_Nail_Pic);
            }

            if (str_How_to_Video != null && !str_How_to_Video.isEmpty()) {
                Stmtupdateforemp.setString(++paramInd, str_How_to_Video);
            }
            Stmtupdateforemp.setString(++paramInd,  str_emp_id);

            System.out.println(Stmtupdateforemp);

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

    String sqlUpdateforemp = "update emp_details set emp_name=? , emp_author = ? , emp_description = ? ,emp_description= ? ,  emp_thumbnail_name = ? ,emp_subscription=?";

    StringBuffer stringbuffer_sql = new StringBuffer(sqlUpdateforemp);
    int paramInd = 0;
    if (str_Preview_Pic != null && !str_Preview_Pic.isEmpty()) {
        stringbuffer_sql.append(",emp_preview_pic=?");
    }

    if (str_Thumb_Nail_Pic != null && !str_Thumb_Nail_Pic.isEmpty()) {
        stringbuffer_sql.append(",emp_thumbnail=?");
    }

    if (str_How_to_Video != null && !str_How_to_Video.isEmpty()) {
        stringbuffer_sql.append(",emp_video=?");
    }

    stringbuffer_sql.append("where emp_id=?");
    PreparedStatement Stmtupdateforemp = dbConnection.prepareStatement(stringbuffer_sql.toString());
    Stmtupdateforemp.setString(++paramInd,  % emp_name_VARIABLE %);
    Stmtupdateforemp.setString(++paramInd,  % emp_author_VARIABLE %);
    Stmtupdateforemp.setString(++paramInd,  % emp_description_VARIABLE %);
    Stmtupdateforemp.setString(++paramInd,  % emp_description_VARIABLE %);
    Stmtupdateforemp.setString(++paramInd,  % emp_thumbnail_name_VARIABLE %);
    Stmtupdateforemp.setString(++paramInd,  % emp_subscription_VARIABLE %);
    if (str_Preview_Pic != null && !str_Preview_Pic.isEmpty()) {
        Stmtupdateforemp.setString(++paramInd, str_Preview_Pic);
    }

    if (str_Thumb_Nail_Pic != null && !str_Thumb_Nail_Pic.isEmpty()) {
        Stmtupdateforemp.setString(++paramInd, str_Thumb_Nail_Pic);
    }

    if (str_How_to_Video != null && !str_How_to_Video.isEmpty()) {
        Stmtupdateforemp.setString(++paramInd, str_How_to_Video);
    }
    Stmtupdateforemp.setString(++paramInd,  % YOUR_EMPID_VARIABLE %);