2017-10-10 77 views
0

我正在使用Java Prepared Statement以這種方式構建動態SQL。你可以讓我知道我可以如何動態設置數值嗎?如何在這種情況下將值動態設置爲preperaed語句

這是我的代碼:

我收到以下異常

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)

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

} 

回答

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 %); 
+0

這將是paramInd的初始vaue? – Pawan

+0

0(零),它會在你設置參數預處理之前預先增加 –

+0

這個工作怎麼樣,因爲emp_description會被三個知道嗎? – Pawan