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

标签 java html oracle jsp drop-down-menu

好吧,首先,我对网页设计相当陌生。但对于我的一个项目,我被要求创建一个页面,根据多个数据库中的表填充多个下拉列表。我相信我已经让这部分工作了,看看到目前为止我的代码(一个 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>

但是,现在我需要添加根据用户在上面的下拉框中选择的内容使用更新和插入语句来更新数据库的功能。再说一次,我对此还很陌生,我不确定这样做的最佳方法是什么?我在谷歌上找到的很多内容表明这个功能主要涉及这部分代码:

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

似乎很多在线示例都建议使用单独的 php 页面?但我并不真正理解两者如何相互链接以及一个页面的内容如何在另一个页面和要更新的数据库之间传输。有这方面经验的任何人都可以在这里提供一些建议,或者指出我下一步可能想要做什么的正确方向,以便能够在 submit 时写入数据库。按钮被点击?

最佳答案

首先是 HTTP post。您将表单提交到一个特殊页面。您将在请求参数中获取所选项目。

所以你创建了一个 <form ... >...</form>这些操作将引导至您的 jsp。现在提交表单后您将获得参数。

操作应该是action="./CodeSelector.jsp"

现在一些关于您的代码的批评者:

  1. 太长了,我建议将行为分成某种 form.jsp 和另一个 store.jsp。您应该拆分代码,以便更好地了解代码。
  2. 从不,在无情况下获取请求参数并将其附加到查询中。这将导致严重的安全风险。只是不要从这个开始。始终使用PreparedStatement并设置参数。这将导致安全查询 SQL-injections .
  3. 考虑使用更现代的框架来创建 Java 支持的网站。我用过Java Server FacesGWT 。您将需要学习更多内容,但要学习的代码却少得令人难以置信(我认为)。

单个 JSP 页面将导致无法测试的一次写入。永远不懂代码。使用现代框架,或 JSP 和 CDI您将把代码分成 gui (JSP) 和逻辑 (Java)

关于java - 如何从 HTML 下拉列表中选定的选项更新/插入 Oracle 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13127729/

相关文章:

java - Maven - 如何将单个资源文件放置在与其他资源文件不同的位置?

javascript - .hide() div 当计时器达到 0 时

javascript - 在 JavaScript 函数中将 CSS id 作为参数传递

sql - Oracle 中的 View 和物化 View 有什么区别?

database - dml 由多个用户/在 oracle 中提交场景

java - 将 byte[] 转换为字符串时出现问题

java - 如何在Java中对不同数字的字符串进行正确排序?

html - 在 CSS、HTML 或 Javascript 中创建可自定义的计数函数?

sql - 如何在 SQL DEVELOPER 中设置表中主键的自动增量?

java - 我的 Android 应用程序与巨大的 MS Access 数据库的 Ucanaccess 连接在 Android 设备上占用了过多的堆空间。还有其他选择吗?