我只是不能使这个代码工作

时间:2015-10-24 10:01:13

标签: jsp

我想通过HTML更新我的数据库,但我知道出了什么问题。请指导我。我无法执行此代码。请帮忙。我正在使用按钮来触发查询。但它没有显示任何错误。我不知道出了什么问题。没有线索。

<%@ page import="java.io.*"%>
<%@ page language="java" import="java.sql.*"%>

<form name="edit" action="post">
<%
    String CustomerID = request.getParameter("CustomerID");
    String CustomerFirstName = request.getParameter("CustomerFirstName");
    String CustomerLastName = request.getParameter("CustomerLastName");
    String CustomerDOB = request.getParameter("CustomerDOB");
    String CustomerContact = request.getParameter("CustomerContact");
    String CustomerAddress = request.getParameter("CustomerAddress");
    String CustomerEmail = request.getParameter("CustomerEmail");

    try{
        if (request.getParameter("edit") != null) {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/basedinventorysystem",
                    "root", "onetwothree123");

            Statement statement = con.createStatement();
            String command = "SET foreign_key_checks = 0";
            statement.executeUpdate(command);

            command = "UPDATE customertable SET CustomerID=?, CustomerFirstName=?, CustomerLastName=?, CustomerDOB=?, CustomerContact=?, CustomerAddress=?, CustomerEmail=? WHERE CustomerID=?";

            command.setInt(1, Integer.parseInt(CustomerID));
            command.setString(2, CustomerFirstName);
            command.setString(3, CustomerLastName);
            command.setString(4, CustomerDOB);
            command.setString(5, CustomerContact);
            command.setString(6, CustomerAddress);
            command.setString(7, CustomerEmail);
            command.setInt(8, Integer.parseInt(CustomerID));
            statement.executeUpdate(command);

            ResultSet resultset = statement.executeQuery("select * from customertable");

            while (resultset.next()) {
                out.print("Successfully Inserted");
                        }
                    }
%>
</form>

我的代码出了什么问题。有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

我尝试了另一种方法,但它有效。而现在我仍然坚持如何在库存增加时做代码。我被赋予了一种状态,即当股票被标记为&#34;有序&#34;当前库存数量仍然相同,当标签变更为&#34;已交付&#34;股票将自动增加。我在创建.jsp文件,tomcat服务器和MySql时使用Eclipse作为平台。所以我希望是否有关于我应该如何开始这样做的任何意见。

无论如何这是我调整的代码并且它有效。

 <%
                if (request.getParameter("edit") !=null){
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/basedinventorysystem",
                            "root", "onetwothree123");

                    Statement statement = con.createStatement();
                    String command = "SET foreign_key_checks = 0";
                    statement.executeUpdate(command);

                    command = "UPDATE customertable SET CustomerID='"+Integer.parseInt(request.getParameter("CustomerID"))
                    +"', CustomerFirstName='"+request.getParameter("CustomerFirstName")
                    +"', CustomerLastName='"+request.getParameter("CustomerLastName")
                    +"', CustomerDOB='"+request.getParameter("CustomerDOB")
                    +"', CustomerContact='"+request.getParameter("CustomerContact")
                    +"', CustomerAddress='"+request.getParameter("CustomerAdd")
                    +"', CustomerEmail='"+request.getParameter("CustomerEmail")
                    +"' WHERE CustomerID='"+Integer.parseInt(request.getParameter("CustomerID"))+"';";

                    statement.executeUpdate(command);

                    ResultSet resultset = statement.executeQuery("select * from customertable");

                    if (resultset.next()) {
                        out.println("Successfully Inserted");
                    }
                }

                %>