复选框的动态数据查询,有人遇到过吗,帮忙解决下,谢谢!
jsp页面:四个复选框,每个后面都有一个文本框,单击复选框以后,就可以得到TEXT中的内容,在action中接收,动态地选择所传过来的值通过SQL进行查询,可是我写的查询语句不知道哪里有问题,查不出来结果。有懂case when语句的帮忙看一下SQL有什么问题,谢谢了
<script type= "javascript ">
function oc1(){
document.myform.check1.value=document.myform.t1.value;
}
function oc2(){
document.myform.check2.value=document.myform.t2.value;
}
function oc3(){
document.myform.check3.value=document.myform.t3.value;
}
function oc4(){
document.myform.check4.value=document.myform.t4.value;
}
</script>
<form method= "post " action= "ts.do " name= "myform ">
<br> <br>
<input type= "checkbox " name= "check1 " value= " " onclick= "oc1() "/>
<input type= "text " name= "t1 " value= " "/>
<input type= "checkbox " name= "check2 " value= " " onclick= "oc2() "/>
<input type= "text " name= "t2 " value= " "/>
<input type= "checkbox " name= "check3 " value= " " onclick= "oc3() "/>
<input type= "text " name= "t3 " value= " "/>
<input type= "checkbox " name= "check4 " value= " " onclick= "oc4() "/>
<input type= "text " name= "t4 " value= " "/>
<input type= "submit " name= "Submit " value= "Submit ">
<input type= "reset " value= "Reset ">
</form>
ACTION中接收页面过来的值
//页面接收过来的复选框
String v1 = request.getParameter( "check1 ");
String v2 = request.getParameter( "check2 ");
String v3 = request.getParameter( "check3 ");
String v4 = request.getParameter( "check4 ");
//通过复选框进行动态查询
try {
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
String url =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=stu ";
Connection conn = DriverManager.getConnection(url, "sa ", "1234 ");
Statement st = conn.createStatement();
System.out.println( "2 ");
String sql = " select * from person a where " +
" case when v1 is not null then a.name = ' " + v1 +
" ' end " +
"and case when v2 is not null then a.address = ' " +
v2 +
" ' end and case when v3 is not null then a.sex = ' " +
v3 +
" 'end and case when v4 is not null then a.zhiwei = ' " +
v4 +
" ' end ";
ResultSet rs = st.executeQuery(sql);
List list = new ArrayList();
while (rs.next()) {
tsForm tForm = new tsForm();
tForm.setName(rs.getString(1));
tForm.setAddress(rs.getString(2));
tForm.setSex(rs.getString(3));
tForm.setZhiwei(rs.getString(4));
list.add(tForm);
}
request.setAttribute( "list ", list);
} catch (Exception ex) {
}
return null;
[解决办法]
ACTION中接收页面过来的值的时候
要接收下复选框选了的内容
然后看看选了哪个了
再去拼SQL
request.getParameterValues( " ")
获得多选的内容,返回的是个String[]