无法通过表单数据更新DB

时间:2014-04-12 19:53:22

标签: servlets jdbc

请给我建议!我在servlet中获取表单数据并尝试更新数据库,但它不起作用。存在与数据库的连接(没有servlet代码,在db中正确添加任何数据),但不会抛出任何异常。 servlet获取参数 - 它们在JSP中由EL表达式提供。我试图简单地通过语句更新数据库,而不使用preparedStatement但它没有帮助。这是代码:

public class ServletClass extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DBConn dbConn = new DBConn();
        String number = req.getParameter("number");
        String amount = req.getParameter("amount");
        String date = req.getParameter("date");

        ArrayList list = dbConn.returnList(number, amount, date);
        req.setAttribute("attr", list);
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("index.jsp");
        requestDispatcher.forward(req, resp);
    }
}


public class DBConn {

    public ArrayList<InvoicesBean> returnList(String number, String amount, String date) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        ArrayList<InvoicesBean> beanList = new ArrayList<InvoicesBean>();
        PreparedStatement preparedStatement = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ved", "root", "1111");
            statement = connection.createStatement();
            preparedStatement = connection.prepareStatement("insert into invoices values(?, ?, ?);");
            preparedStatement.setString(1, number);
            preparedStatement.setString(2, amount);
            preparedStatement.setString(3, date);
            preparedStatement.executeUpdate();
            resultSet = statement.executeQuery("SELECT * FROM invoices;");
            while (resultSet.next()){
                InvoicesBean invoicesBean = new InvoicesBean();
                invoicesBean.setNumber(resultSet.getString("number"));
                invoicesBean.setAmount(resultSet.getString("amount"));
                invoicesBean.setDate(resultSet.getString("date"));
                beanList.add(invoicesBean);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                if (statement != null) statement.close();
                if (connection != null) connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return beanList;
    }
}

InvoiceBean-class只是一个带有getter / setters的标准bean类

1 个答案:

答案 0 :(得分:0)

 invoicesBean.setNumber(resultSet.getString("number"));
            invoicesBean.setAmount(resultSet.getString("amount"));
            invoicesBean.setDate(resultSet.getString("date"));

您不能将数据库列名称作为&#39; number&#39;或者&#39; date&#39;。他们是保留的。 再次检查列名称。 为了检查这是否是错误,您可以用列号1,2,3替换列名。