【问题标题】:Why do I receive this MySQLIntegrityConstraintViolationException: Column 'first_name' cannot be null when exuting this script?为什么我收到这个 MySQLIntegrityConstraintViolationException: Column 'first_name' cannot be null 在执行此脚本时?
【发布时间】:2015-05-26 03:07:22
【问题描述】:

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

错误..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);
        }
%>

【问题讨论】:

  • 我们可以看到您发送到服务器的 URL 吗?更好的是,自己检查一下。我赌 1000 Stack Overflow 点数,您将传递 null 值。

标签: mysql jsp jdbc forms http-post


【解决方案1】:

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

也许,在里面放一个断点来捕捉请求变量被填充后实际的 sql 命令是什么。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-18
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      • 2012-03-01
      相关资源
      最近更新 更多