2016-04-22 80 views
0

我需要让用户将一些数据提供给像(城市,国家,国家的URL)的第一页,我需要他从中选择什么样的目的地:在数据库中获取多个复选框结果(JSP PAGE)

冬天,圣诞节,夏天(他可以检查最大2)

如何将此结果与我的数据库连接?

我已经建立3个表

  1. 与nodest列(PK),城市,国家,网址
  2. nodest_c(PK),类别
  3. nodest(FK - > nodest),nodest_c( fk - > nodest_c)

创建目标代码。 JSP

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>create dest</title> 
    </head> 
    <body> 
     <html> 
    <head> 
     <title>CREATE DESTINATION</title> 
     <meta charset="UTF-8"> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    </head> 
    <body> 
     <h1> CREATING DESTINATION </h1> 
     <form name="createdest" method="get" action="create dest code.jsp"> 
     Country: <input type="text" required name="id8" /> <br> 
     City: <input type="text" required name="id9" /> <br> 
     URL Video: <input type="url" required name="id10" /> <br> <br> 
     <i><ins>Categorize the destination (max 2): </ins></i> <br> <br> 
     <input type="checkbox" name="id5" value="1" onClick="return KeepCount()" >Christmas<br> 
     <input type="checkbox" name="id6" value="2" onClick="return KeepCount()" >Winter <br> 
     <input type="checkbox" name="id7" value="3" onClick="return KeepCount()" >Summer <br> <br> 

     <input type="submit" value="CREATE DESTINATION" /> 


     <br> 


     </form> 
     <SCRIPT LANGUAGE="javascript"> 

function KeepCount() { 

var NewCount = 0 

if (document.createdest.id5.checked) 
{NewCount = NewCount + 1} 

if (document.createdest.id6.checked) 
{NewCount = NewCount + 1} 

if (document.createdest.id7.checked) 
{NewCount = NewCount + 1} 

if (NewCount == 3) 
{ 
alert('Pick Just Two Please') 
document.createdest; return false; 
} 
} 
</SCRIPT> 


    </body> 
</html> 

插入数据库.JSP

<%@page import="java.sql.*" %> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>create dest code</title> 
    </head> 
    <body> 
     <% 
    int m[] = new int[5000]; String s[] = new String[5000];   
    Class.forName("com.mysql.jdbc.Driver"); 
    String myDatabase = "jdbc:mysql://localhost:3306/project_app?user=root&password=1234"; 
    Connection myConnection = DriverManager.getConnection(myDatabase); 
    Statement myStatement = myConnection.createStatement(); 
    boolean check=null!=request.getParameter("id5"); 
    boolean check1=null!=request.getParameter("id6"); 
    boolean check2=null!=request.getParameter("id7"); 
    String id8=request.getParameter("id8"); 
    String id9=request.getParameter("id9"); 
    String id10=request.getParameter("id10"); 
    String sqlString = "INSERT INTO DEST(COUNTRY,CITY,URL) VALUES ('"+id8+"', '"+id9+"','"+id10+"')"; 
    myStatement.executeUpdate(sqlString); 
    myStatement.executeUpdate(sqlString1); 
    myStatement.close(); 
    myConnection.close(); %> 
    </body> 
    <h1 style="color:blue;">Successful Registration </h1> 
</html> 

有什么建议?

+0

你的问题在哪里? –

+0

我能做些什么来在表格中插入结果复选框 – JHk1821

回答

1
在servlet使用

验证码:

String checked = request.getParameterValue("checkboxName"); 

,并检查是否为空,这意味着你的复选框没有被检查。

另外,如果你想使用复选框组,把一些复选框具有相同的名称,并使用此:

String[] checkedValues = request.getParameterValues("checkboxName"); 

然后就是检查将被添加到的CheckedValue变量的所有复选框值。

boolean check = null != request.getParameter("id5"); 
boolean check1 = null != request.getParameter("id6"); 
boolean check2 = null != request.getParameter("id7"); 

并根据您在数据库中用于检查列的数据类型,可以使用这些布尔值。

+0

input type =“checkbox”name =“id6”value =“2”in database will insert the“2”? (任何有价值的东西?),你能解释一下吗? ; D – JHk1821

+0

更新了答案。 –

+0

检查上面的代码,你能写我所有的代码,因为我不明白:P – JHk1821