Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java:将复选框值以0(未选中)和1(已选中)的形式存储到mysql数据库,并正确检索以向用户显示_Mysql_Jsp_Servlets - Fatal编程技术网

java:将复选框值以0(未选中)和1(已选中)的形式存储到mysql数据库,并正确检索以向用户显示

java:将复选框值以0(未选中)和1(已选中)的形式存储到mysql数据库,并正确检索以向用户显示,mysql,jsp,servlets,Mysql,Jsp,Servlets,我想从mysql中检索复选框值,我以1(选中)和0(未选中)的形式存储了它,但我不知道如何检索它。或者是否有其他选项可用于相同的操作 以下是我的jsp和servlet代码: HobbiesTest.jsp <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HT

我想从mysql中检索复选框值,我以1(选中)和0(未选中)的形式存储了它,但我不知道如何检索它。或者是否有其他选项可用于相同的操作

以下是我的jsp和servlet代码:

HobbiesTest.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
        <title>Insert title here</title>
    </head>
    <body>
        <form action="HobbiesTestServlet" method="post">

            <input type = "checkbox" name = "Hobbies" value = 'Dancing' >Dancing
            <input type = "checkbox" name = "Hobbies" value = 'Reading'>Reading         
            <input type = "checkbox" name = "Hobbies" value = 'Singing'  >Singing
            <input type = "checkbox" name = "Hobbies" value = 'Programming'>Programming
            <input type = "checkbox" name = "Hobbies" value = 'Sleeping'>Sleeping

            <input type = "submit" value = 'Submit'>

        </form>
    </body>
</html>

在此处插入标题
跳舞
阅读
歌唱
程序设计
睡觉
HobbiesTestServlet.java

package com.fulcrum.EmpForm;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

    public class HobbiesTestServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;

        public HobbiesTestServlet() {
            super();
        }

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        }


        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            int danceCheck = 0;

            int singCheck = 0;

            int readCheck = 0;

            int programCheck = 0;

            int sleepCheck = 0;

            List<String> hobbiesList = new ArrayList<String>(); 

            String[] hobbies = request.getParameterValues("Hobbies");
            if(hobbies != null){
                for(int i=0;i<hobbies.length;i++){
                    System.out.println("hobbies are"+hobbies[i]);
                    hobbiesList.add(hobbies[i]);
                }
            }

            if(hobbiesList.contains("Dancing")){

                danceCheck = 1;

            }

            if(hobbiesList.contains("Reading")){

                readCheck = 1;

            }

            if(hobbiesList.contains("Singing")){

                singCheck = 1;

            }

            if(hobbiesList.contains("Programming")){

                programCheck = 1;

            }

            if(hobbiesList.contains("Sleeping")){

                sleepCheck = 1;

            }
            System.out.println(danceCheck);
            System.out.println(readCheck);
            System.out.println(singCheck);
            System.out.println(programCheck);
            System.out.println(sleepCheck);
        }

    }
package com.fulcrum.EmpForm;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
导入javax.servlet.ServletException;
导入javax.servlet.http.HttpServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
公共类HobbiesTestServlet扩展了HttpServlet{
私有静态最终长serialVersionUID=1L;
公共爱好TestServlet(){
超级();
}
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
}
受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException{
int-danceCheck=0;
int singCheck=0;
int readCheck=0;
int programCheck=0;
int sleepCheck=0;
List hobbiesList=新建ArrayList();
String[]嗜好=request.getParameterValues(“嗜好”);
if(嗜好!=null){

对于(inti=0;iIn summary,您希望获得表的结构,不是吗?不。我将向该表添加更多列,如first_name、last_name等。但在最后,我希望在jsp文件中显示所有用户输入的值。首先,现在只处理这些复选框。因此,最后的输出应该是这样的(您的爱好是:跳舞、阅读等)Sp您的表格在输出中的列数是定制的,取决于选中的复选框?