为什么我会收到此MySQLIntegrityConstraintViolationException:Column' first_name'在解释此脚本时不能为空?

时间:2015-05-26 03:07:22

标签: mysql jsp jdbc html-form-post

我尝试使用下面的脚本将html表单数据插入到我的数据库中,但在运行应用程序时,我收到以下错误消息:

  

Error..com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:   专栏' first_name'不能为空

我做错了什么? :/

  <%
        Connection con = null;  
        PreparedStatement st = null;

        String first_name = request.getParameter("first_name");
        String last_name = request.getParameter("last_name");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String address_1 = request.getParameter("address_1");
        String address_2 = request.getParameter("address_2");
        String city = request.getParameter("city");
        String State_Province_Region = request.getParameter("State_Province_Region");
        String Postal_Zip_Code = request.getParameter("Postal_Zip_Code");
        String country = request.getParameter("Country");


        try {
              con = DriverManager.getConnection("jdbc:mysql://localhost:3306/application","root",
         "password");

              st = con.prepareStatement("INSERT INTO customer (first_name, last_name, phone, email, address_1, address_2, city, State_Province_Region, Postal_Zip_Code, Country) VALUES (?,?,?,?,?,?,?,?,?,?)");


                 st.setString(1,first_name);
                 st.setString(2,last_name);
                 st.setString(3,phone);

                 st.setString(4,email);
                 st.setString(5,address_1);
                 st.setString(6,address_2);

                 st.setString(7,city);
                 st.setString(8,State_Province_Region);
                 st.setString(9,Postal_Zip_Code);

                 st.setString(10,country);

              st.executeUpdate();
              st.clearParameters(); 



             st.close();
             con.close();
        } 

        catch (Exception e) { 
             out.println("Error.."+e);
        }
%>

2 个答案:

答案 0 :(得分:1)

我知道这听起来很明显......但是您将数据库集中的firstname列设置为不允许空值。此外,您必须在其中一行中传递该字段的空值。

也许,在那里放一个断点来捕获请求变量填充后的实际sql命令。

答案 1 :(得分:1)

尝试添加System.out.println()语句并调试first_name, last_name等的值......

其他错误的事情: 1将所有JDBC代码移动到单独的Java文件中 2 close()语句应该在finally块中。当有例外时,他们不会在你的情况下执行。