簡體   English   中英

如何從HTML下拉列表中的選定選項更新/插入Oracle數據庫?

[英]How to Update/Insert Into Oracle Database From Selected Option in HTML Dropdown?

好的,首先,我是Web設計的新手。 但是對於我的一個項目,有人要求我創建一個頁面,該頁面根據多個數據庫中的表填充多個下拉列表。 而且我相信我已經完成了這一部分的工作,看看到目前為止的代碼(一個jsp頁面):

CodeSelector.jsp

<%@page import="java.sql.*"%>
<!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>Codes Page</title>
        </head>
        <body>
            <form name = "codes" method = "POST" action="....." target="_self">
                <h1>Please select the applicable codes:</h1>
                <select name='code1' onchange="showState(this.value)">  
                <option value="none">Code One: None</option>  
                <%
                    String debug = "ON";

                    if(debug.equals("ON"))
                    {
                        System.out.println("***DEBUGGING IS TURNED ON!!!***");
                    }

                    //Pulls the ids and descriptions from the first codes table and stores them in the first drop down
                    try
                    {
                        String caseId = request.getParameter("caseID");
                        //caseId = "30";

                        if (caseId == null)
                        {
                            //debug
                            System.out.println("The caseID is NULL!");

                            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();  
                            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@url:sid","username","password");  
                            Statement stmt = con.createStatement();  
                            ResultSet rs = stmt.executeQuery("select id, descr from case_codes");
                            String tempString;

                            while(rs.next())
                            {
                                //If the code description is more than 125 characters long, truncate the string and append "..." to the end of it.
                                if (rs.getString(2).length() > 125)
                                {
                                    tempString = rs.getString(2).substring(0, 125);
                                    %>
                                        <option value="<%=rs.getString(1)%>"><%=rs.getString(1)%> <%=tempString%>...</option>  
                                    <%
                                }
                                //Else just insert the whole description into the option field.
                                else
                                {
                                    %>
                                        <option value="<%=rs.getString(1)%>"><%=rs.getString(1)%> <%=rs.getString(2)%></option>  
                                    <%
                                }

                            }

                            //Closes the database connection
                            stmt.close();
                            con.close();
                        }
                        else if (caseId != null)
                        {
                            if(debug.equals("ON"))
                            {
                                System.out.println("The caseID is NOT NULL!");
                            }

                            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();  
                            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@url:sid","username","password");  
                            Statement stmt = con.createStatement();

                            //Returns a list of all the tables and views in the database
                            if(debug.equals("ON"))
                            {
                                DatabaseMetaData meta = con.getMetaData();
                                ResultSet res = meta.getTables(null, null, null, new String[] {"TABLE"});

                                while (res.next()) 
                                {
                                    System.out.println(
                                        "   "+res.getString("TABLE_CAT") 
                                       + ", "+res.getString("TABLE_SCHEM")
                                       + ", "+res.getString("TABLE_NAME")
                                       + ", "+res.getString("TABLE_TYPE")
                                       + ", "+res.getString("REMARKS")); 
                                 }
                            }

                            if(debug.equals("ON"))
                            {
                                System.out.println("BEFORE SQL Statement: select id from cases");
                            }

                            //Returns a result set of all the ids in the cases table
                            ResultSet rs = stmt.executeQuery("select id from cases");

                            if(debug.equals("ON"))
                            {
                                System.out.println("AFTER SQL Statement: select id from cases");
                            }

                            while(rs.next())
                            {
                                if(debug.equals("ON"))
                                {
                                    System.out.println("The rs is: " + rs.getString(1));
                                }

                                if(rs.getString(1).equals(caseId))
                                {
                                    if(debug.equals("ON"))
                                    {
                                        System.out.println("Case ID Found!");
                                    }

                                    ResultSet rs2 = stmt.executeQuery("select rlawcd_id, display_seq from cs_rlawcd where cs_id = " + caseId);

                                    while(rs2.next())
                                    {
                                        if(debug.equals("ON"))
                                        {
                                            System.out.println("Inside rs2 while loop");

                                        }

                                        //If no values are returned in the rlawcd table, populate the drop down as you normally would
                                        if (rs2 == null)
                                        {
                                            if(debug.equals("ON"))
                                            {
                                                System.out.println("Inside rs2 IF");
                                                System.out.println("rs2 = null");
                                            }

                                            ResultSet rs3 = stmt.executeQuery("select id, descr from case_codes");
                                            String tempString;

                                            while(rs3.next())
                                            {
                                                //If the code description is more than 125 characters long, truncate the string and append "..." to the end of it.
                                                if (rs3.getString(2).length() > 125)
                                                {
                                                    tempString = rs3.getString(2).substring(0, 125);
                                                    %>
                                                        <option value="<%=rs3.getString(1)%>"><%=rs3.getString(1)%> <%=tempString%>...</option>  
                                                    <%
                                                }
                                                //Else just insert the whole description into the option field.
                                                else
                                                {
                                                    %>
                                                        <option value="<%=rs3.getString(1)%>"><%=rs3.getString(1)%> <%=rs3.getString(2)%></option>  
                                                    <%
                                                }

                                            }
                                        }
                                        //Else if the values are indeed returned and the display sequence equals 1
                                        //populate the drop downs normally but with the returned values set as the selected/default items
                                        else if(rs2.getString(2).equals("1"))
                                        {
                                            if(debug.equals("ON"))
                                            {
                                                System.out.println("Inside rs2 ELSE IF");
                                                System.out.println("The rs2 is NOT NULL!");
                                            }

                                            String codeID = rs2.getString(1);

                                            ResultSet rs3 = stmt.executeQuery("select id, descr from case_codes");
                                            String tempString;

                                            while(rs3.next())
                                            {
                                                if(debug.equals("ON"))
                                                {
                                                    System.out.println("Inside rs3 while loop");
                                                }

                                                if (rs3.getString(1).equals(codeID))
                                                {
                                                    if(debug.equals("ON"))
                                                    {
                                                        System.out.println("Inside rs3 IF");
                                                        System.out.println("A matching law code was found!");
                                                    }

                                                    //If the code description is more than 125 characters long, truncate the string and append "..." to the end of it.
                                                    if (rs3.getString(2).length() > 125)
                                                    {
                                                        tempString = rs3.getString(2).substring(0, 125);
                                                        %>
                                                            <option selected="<%=rs3.getString(1)%>"><%=rs3.getString(1)%> <%=tempString%>...</option>  
                                                        <%
                                                    }
                                                    //Else just insert the whole description into the default/selected option field.
                                                    else
                                                    {
                                                        %>
                                                            <option selected="<%=rs3.getString(1)%>"><%=rs3.getString(1)%> <%=rs3.getString(2)%></option>  
                                                        <%
                                                    }       
                                                }
                                                else
                                                {
                                                    //If the code description is more than 125 characters long, truncate the string and append "..." to the end of it.
                                                    if (rs3.getString(2).length() > 125)
                                                    {
                                                        tempString = rs3.getString(2).substring(0, 125);
                                                        %>
                                                            <option value="<%=rs3.getString(1)%>"><%=rs3.getString(1)%> <%=tempString%>...</option>  
                                                        <%
                                                    }
                                                    //Else just insert the whole description into the option field.
                                                    else
                                                    {
                                                        %>
                                                            <option value="<%=rs3.getString(1)%>"><%=rs3.getString(1)%> <%=rs3.getString(2)%></option>  
                                                        <%
                                                    }       
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if(debug.equals("ON"))
                                            {
                                                System.out.println("Inside the rs2 ELSE");
                                                System.out.println("Something must have gone wrong.");
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    //do nothing...
                                }
                            }
                            //Closes the database connection
                            stmt.close();
                            con.close();
                        }
                        else
                        {
                            //debug
                            System.out.println("Something weird happened.");
                        }

                    }
                    catch (ClassNotFoundException e)
                    {
                        System.err.println("ClassNotFoundException: " + e.getMessage());
                    } 
                    catch (SQLException e)
                    {
                        System.err.println("SQLException: " + e.getMessage());
                    }
                    catch (Exception e)
                    {
                        System.err.println("Generic Exception: " + e.getMessage());
                    }       
                %>
                </select>
                <br>
                <br>
                <input type="submit" value="Submit">
              </form>
          </body> 
      </html>

但是,現在我需要添加基於用戶從上方的下拉框中選擇的內容,使用update和insert語句更新數據庫的功能。 再說一次,對此我還很陌生,我不確定做到這一點的最佳方法是什么? 我在Google上發現的很多內容都表明此功能主要涉及此部分代碼:

<form name = "codes" method = "POST" action="...." target="_self">

似乎很多在線示例建議使用單獨的php頁面? 但是我並不真正理解兩者之間是如何鏈接的,以及一頁的內容如何在另一頁和要更新的數據庫之間傳輸。 對此有經驗的人可以在這里提供一些建議或向我指出下一步要做什么以便在單擊“ submit按鈕時能夠寫入數據庫的正確方向嗎?

好吧,第一件事就是HTTP帖子。 您將表單提交到特殊頁面。 您將在request參數中獲得所選項目。

因此,您創建一個<form ... >...</form>動作將導致您的jsp。 現在,您將在提交表單后獲取參數。

該動作應為action="./CodeSelector.jsp"

現在一些批評您的代碼:

  1. 很長一段時間,我建議將這種行為分為form.jsp和另一個store.jsp。 您應該拆分代碼,以更好地了解代碼。
  2. 絕對不要沒有情況的情況下使用request-parameter並將其附加到查詢中。 這將導致嚴重的序列風險。 只是不要從此開始。 始終使用PreparedStatement並設置參數。 這將導致在SQL注入方面進行安全查詢。
  3. 考慮使用更現代的框架來創建Java支持的網站。 我使用過Java Server FacesGWT 您將學到更多東西,但是對代碼卻少得多(我認為)。

單個JSP頁將導致無法測試的寫入一次。 永遠不懂代碼。 使用現代frmework或JSP和CDI,您可以將代碼分為gui(JSP)和邏輯(Java)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM