2016-04-22 9 views
0

(都市、国、国のURL)のような最初のページにいくつかのデータを与え、どのような目的地を選択する必要があります:複数のチェックボックスの結果をデータベース(JSPページ)

冬、クリスマス、夏(彼は最大2を確認できます)

どうすればこの結果を自分のデータベースに接続できますか?

私はnodest列(PK)、市、国、URL
  • nodest_c(PK)、カテゴリ
  • nodest(FK - > nodest)と3つのテーブル

    1. を作成している

      、nodest_c( fk→nodest_c)

    宛先コードを作成します。

    <%@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> 
    

    .jspファイル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> 
    

    挿入データベースの任意の提案ですか?サーブレットの使用中

  • +0

    どこがあなたの質問ですか? –

    +0

    テーブルに挿入するために何ができるのですか? – JHk1821

    答えて

    1

    このコード:

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

    と場合にチェックがnullの場合、これはあなたのチェックボックスがチェックされなかったことを意味します。また

    あなたは、チェックボックスのグループを使用して同じ名前でいくつかのチェックボックスを入れて、これを使用する場合:

    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

    入力タイプ= "checkbox" name = "id6" value = "2"データベースに "2"が挿入されますか? (価値があるものは何でも)、あなたは上記をより良く説明できますか? ; D – JHk1821

    +0

    が答えを更新しました。 –

    +0

    上記のコードを確認してください、私は理解できないすべてのコードを書いてもいいですか?P – JHk1821

    関連する問題