[英]Converting request.getParameterValues() to int array
我嘗試在網上尋求幫助以解決問題,但無濟於事。 我希望將verify[]
轉換為int
,以便可以在查詢中對其進行處理。
verified
列的數據類型為string
staff_id
列的數據類型為autonumber
我有一個錯誤
java.sql.SQLException:[Microsoft] [ODBC Microsoft Access驅動程序]條件表達式中的數據類型不匹配。
<!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>
<!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>
將您的UPDATE
查詢更改為
st.executeUpdate("update Staff set verified = 'yes' where Staff_id = " + verify[i]);
由於staff_id
列的數據類型為autonumber
,因此其值不應該用引號指定,因為它本質上是數字。
我還建議您使用SQL IN
子句,僅觸發一個UPDATE
而不要觸發多次,每個staff_id
值觸發一次。
IN
子句的UPDATE
查詢應類似於
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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.