将request.getParameterValues()转换为int数组

时间:2015-02-03 17:56:18

标签: java arrays jsp parseint

我尝试在网上寻求帮助以解决我的问题,但无济于事。我希望将verify[]转换为int,以便在查询中对其进行处理。

verified列的数据类型为string

staff_id列的数据类型为autonumber

我收到错误

  

java.sql.SQLException:[Microsoft] [ODBC Microsoft Access驱动程序]条件表达式中的数据类型不匹配。

VerifyStaff.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Verification of Staff Accounts</title>
</head>
<body>
<div align="center">
        <%-- Imports --%>
        <%@ page import="java.sql.*"%>
        <%@ page import="java.util.*"%>
        <%-- HTTP header --%>
        <%response.addHeader("Cache-Control","no-cache");
            response.addHeader("Pragma","no-cache");
                response.addHeader("Expires","0");  
        %>
        <%-- Retrieving Staff Accounts - Reading --%>
        <%
        try{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String conURL= "jdbc:odbc:HOD_DATA";
        Connection con = DriverManager.getConnection(conURL);
        Statement st = con.createStatement();
        String query = "select staff_id, username, password, user_group, verified from Staff";
        ResultSet rs = st.executeQuery(query);
    %>
        <form action="VerifyStaffAuth.jsp">
                <table width="200" border="1">
                        <tr>
                                <td>Staff_ID</td>
                                <td>Username</td>
                                <td>Password</td>
                                <td>User Group</td>
                                <td>Verified?</td>
                                <td>Verify/Un-verify</td>
                        </tr>
                        <%
        while(rs.next()){
            int staff = rs.getInt("staff_id");
%>
                        <tr>
                                <td><%= staff %></td>
                                <td><%= rs.getString("username") %></td>
                                <td><%= rs.getString("password") %></td>
                                <td><%= rs.getString("user_group") %></td>
                                <td><%= rs.getString("verified") %></td>
                                <td><label>
                                                <input type="checkbox" name="CheckboxGroup" value="<%= staff %>">
                                        </label></td>
                        </tr>
                        <%
        }
        rs.close();
        st.close();
        con.close();
        }

        catch(Exception e){
        out.println(e);
        }

        %>
                </table>
                <input type="submit" VALUE="submit">
        </form>
</div>
</body>
</html>

VerifyStaffAuth.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
</head>
<body>
<%-- Imports --%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%-- HTTP header --%>
<%response.addHeader("Cache-Control","no-cache");
            response.addHeader("Pragma","no-cache");
                response.addHeader("Expires","0");  %>

<%  String[] verify = request.getParameterValues("CheckboxGroup");

            int[] verify2 = new int[verify.length];

            for(int i=0;i<verify.length;i++){
                verify2[i]=Integer.parseInt(verify[i]);
            }

        if(verify != null){
            for(int i=0; i<verify.length; i++){

    try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String conURL= "jdbc:odbc:HOD_DATA";
            Connection con = DriverManager.getConnection(conURL);
            Statement st = con.createStatement();
            int status = st.executeUpdate("update Staff set verified = 'yes' where Staff_id = '"+verify[i]+"'");

            if(status>0){
                //response.sendRedirect("SuccessfulReg1.html");

            }
            else{
                //out.println("Update unsuccessful");
            }

            st.close();
            con.close();
            }

            catch(Exception e){
            out.println(e);
            }
        }
        }
        %>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

将您的UPDATE查询更改为

st.executeUpdate("update Staff set verified = 'yes' where Staff_id = " + verify[i]);

由于staff_id列的数据类型为autonumber,因此不应在引号中指定其值,因为它本质上是数字。

我还建议您使用SQL IN子句并仅触发一个UPDATE而不是多次触发,每个staff_id值一个。


带有UPDATE子句的IN查询应该类似于

UPDATE staff SET verified = 'yes' WHERE staff_id IN (1, 2, 3)

使用Arrays.toString()将逗号分隔的字符串作为

String inValues = Arrays.toString(verify); // "[1, 2, 3]"
inValues = inValues.substring(1, inValues.length() - 1)); // "1, 2, 3"