简体   繁体   English

PreparedStatement 类型中的 setString(int, String) 方法不适用于参数 (int, String[])

[英]The method setString(int, String) in the type PreparedStatement is not applicable for the arguments (int, String[])

I know that setString permits to insert a value in specified position, now I want get checkbox values from the jsp page in order to pass it to the database.我知道 setString 允许在指定位置插入一个值,现在我想从 jsp 页面获取复选框值以便将其传递给数据库。

I defined the variable of checkbox as a String array since it may handle one or more values.我将 checkbox 的变量定义为 String 数组,因为它可以处理一个或多个值。

This is how I defined it the variable in the class:这就是我在类中定义变量的方式:

public String[] rep;

This is how my servlet shall retrieve this parameter in the doPost method:这是我的 servlet 在 doPost 方法中检索此参数的方式:

String[] rep = request.getParameterValues("rep");

and this is a line from my DAO class from the preparedStatement query:这是我的 DAO 类中来自 PreparedStatement 查询的一行:

st.setString(3, exam.rep);

but that is shown this error: The method setString(int, String) in the type PreparedStatement is not applicable for the arguments (int, String[])但这显示了这个错误:PreparedStatement 类型中的方法 setString(int, String) 不适用于参数 (int, String[])

the whole query整个查询

public static void  add(Exam exam) throws ClassNotFoundException, SQLException {
    Connection cnx;
    cnx = Connect.getConnection();
    String req = "insert into examen values (?,?,?)";
    PreparedStatement st = cnx.prepareStatement(req);
    st.setString(1, exam.titre);
    st.setString(2, exam.question);
    st.setString(3, exam.rep);
    st.executeUpdate();
}

It would help if you can show us your SQL query.如果您能向我们展示您的 SQL 查询,将会有所帮助。 Prepared statements take a single value, not an array.准备好的语句采用单个值,而不是数组。 As you define the first param as the index of the ?当您将第一个参数定义为? in your query to replace with corresponding second param.在您的查询中替换为相应的第二个参数。 Something like this might suit your needs;像这样的东西可能适合您的需求;

String stmt = "INSERT INTO abc (some_field) VALUES (?);";
PreparedStatement ps = conn.prepareStatement(stmt);
String formatted = String.join(",", items);
ps.setString(3, formatted);

If you want to store an array value using your prepared statement then you should format your string in some way that you can convert it back into an array when reading from your DB.如果要使用准备好的语句存储数组值,则应以某种方式格式化字符串,以便在从数据库读取时将其转换回数组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:PreparedStatement类型中的方法setString(int,String)不适用于参数(int,Object) - error : The method setString(int, String) in the type PreparedStatement is not applicable for the arguments (int, Object) PrintStream 类型中的方法 println(int) 不适用于参数 (String, int, int, int) - The method println(int) in the type PrintStream is not applicable for the arguments (String, int, int, int) Java Writer 错误:Writer 类型中的方法 write(char[], int, int) 不适用于 arguments (int, double, int, String) - Java Writer error: The method write(char[], int, int) in the type Writer is not applicable for the arguments (int, double, int, String) Integer类型的方法valueOf(String)不适用于参数(int) - The method valueOf(String) in the type Integer is not applicable for the arguments (int) 类型中的方法不适用于参数(int) - The method in the type is not applicable for the arguments (int) Android:Toast类型的方法makeText(Context,CharSequence,int)不适用于参数(ListViewAdapter,String,int) - Android: The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (ListViewAdapter, String, int) 王子类型中的Encrypt(long,long,long,long,int)方法不适用于参数(String,long,long,long,long,int) - The method Encrypt(long, long, long, long, int) in the type prince is not applicable for the arguments (String, long, long, long, int) 类型为BitmapFactory的方法encodeResource(Resources,int)不适用于Android中的参数(Resources,String) - The method decodeResource(Resources, int) in the type BitmapFactory is not applicable for the arguments (Resources, String) in Android 如何修复错误:Map 类型中的 put(Integer, Integer) 方法<integer,integer>不适用于 arguments (int, String)</integer,integer> - How to fix error: The method put(Integer, Integer) in the type Map<Integer,Integer> is not applicable for the arguments (int, String) 方法get(int)在List类型中 <String> 不适用于Java 8中的参数字符串 - The method get(int) in the type List<String> is not applicable for the argument string in Java 8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM