简体   繁体   English

如何从HTML下拉列表中的选定选项更新/插入Oracle数据库?

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

Okay, so first off, I'm fairly new to web design. 好的,首先,我是Web设计的新手。 But for a project of mine, I'd been asked to create a page that populates multiple drop downs based on the tables in a number of databases. 但是对于我的一个项目,有人要求我创建一个页面,该页面根据多个数据库中的表填充多个下拉列表。 And I believe I have gotten this part to work, a look at my code so far (a jsp page): 而且我相信我已经完成了这一部分的工作,看看到目前为止的代码(一个jsp页面):

CodeSelector.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>

However, now I need to add the ability to update the database with update and insert statements based upon what the user selects in the drop down boxes from above. 但是,现在我需要添加基于用户从上方的下拉框中选择的内容,使用update和insert语句更新数据库的功能。 Again, being fairly new to this, I'm not sure what the best way to go about doing this would be? 再说一次,对此我还很陌生,我不确定做到这一点的最佳方法是什么? A lot of what I've found on google suggests this functionality mainly involves this part of the code: 我在Google上发现的很多内容都表明此功能主要涉及此部分代码:

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

And it seems a lot of the examples online suggest using a seperate php page? 似乎很多在线示例建议使用单独的php页面? But I didn't really understand how the two linked to one another and how one pages contents get transferred between the other page and the database you want to update. 但是我并不真正理解两者之间是如何链接的,以及一页的内容如何在另一页和要更新的数据库之间传输。 Could anyone with experience in this offer some advice here or point me in the right direction as to what I might want to do next in order to be able to write to the database when the submit button is clicked? 对此有经验的人可以在这里提供一些建议或向我指出下一步要做什么以便在单击“ submit按钮时能够写入数据库的正确方向吗?

Well the first thing is the HTTP post. 好吧,第一件事就是HTTP帖子。 You submit your form to a special page. 您将表单提交到特殊页面。 You will get the selected item within the request parameter. 您将在request参数中获得所选项目。

So you create a <form ... >...</form> the actions leads to your jsp. 因此,您创建一个<form ... >...</form>动作将导致您的jsp。 Now you'll get the parameter after submitting the form. 现在,您将在提交表单后获取参数。

The action should be action="./CodeSelector.jsp" 该动作应为action="./CodeSelector.jsp"

Now some critics regarding your code: 现在一些批评您的代码:

  1. It is to long, I would suggest to split the behaviour into some kind of form.jsp and another store.jsp. 很长一段时间,我建议将这种行为分为form.jsp和另一个store.jsp。 You should split your code, to gain a better overview of your code. 您应该拆分代码,以更好地了解代码。
  2. NEVER , under NO CIRCUMSTANCES take request-parameter and append them to a query. 绝对不要没有情况的情况下使用request-parameter并将其附加到查询中。 This will lead into severe sequrity risks. 这将导致严重的序列风险。 Just don't start with this. 只是不要从此开始。 Always use PreparedStatement and set the parmeter. 始终使用PreparedStatement并设置参数。 This will lead into secure queries in terms of SQL-injections . 这将导致在SQL注入方面进行安全查询。
  3. Think about more modern frameworks for creating Java-Backed Websites. 考虑使用更现代的框架来创建Java支持的网站。 I have used Java Server Faces and GWT . 我使用过Java Server FacesGWT You will have more to learn, but incredible less to Code (I think). 您将学到更多东西,但是对代码却少得多(我认为)。

A single JSP-page will lead into untestable write once. 单个JSP页将导致无法测试的写入一次。 never understand code. 永远不懂代码。 With modern frmework, or JSP and CDI you will split your code into gui (JSP) and logic (Java) 使用现代frmework或JSP和CDI,您可以将代码分为gui(JSP)和逻辑(Java)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM