Home  >  Q&A  >  body text

java - servlet向MySQL插入数据,Int可以成功,String无法插入

下面是报出的错误,百度之后我已经删除了对应表的外键和约束,重启了服务器,还是无法插入。
java.sql.SQLException: Duplicate key or integrity constraint violation message from server: "Column 'CourlerTitle' cannot be null"
servlet中的相应代码

`String sql = "insert into t_courler(CourlerId,CourlerTitle,UserId,CourlerContent) values(?,?,?,?)";
                //String sql = "insert into t_courler(CourlerId,UserId) values(?,?)";
                // 获取PreparedStatement  
               PreparedStatement ps = conn.prepareStatement(sql);  
                // 对SQL语句中的第1个参数赋值  
                ps.setInt(1, book.getCourierId());  
               ps.setString(2, book.getCourierTitle());  
                ps.setInt(3, book.getUserId());                
               ps.setString(4, book.getCourierContent());  
                // 执行更新操作,返回所影响的行数  
                int row = ps.executeUpdate();  
                // 判断是否更新成功  
                if (row > 0) {  
                    // 更新成输出信息  
                    System.out.print("成功添加了 " + row + "条数据!");  
                }  
                // 关闭PreparedStatement,释放资源  
                ps.close();  
                // 关闭Connection,释放资源  
                conn.close();  `

对应的javabean代码

package com.fz.bean;

public class courierData {
        public int getCourierId() {
        return CourierId;
    }
    public void setCourierId(int courierId) {
        this.CourierId = courierId;
    }
    public String getCourierTitle() {
        return CourierTitle;
    }
    public void setCourierTitle(String courierTitle) {
        this.CourierTitle = courierTitle;
    }
    public String getCourierContent() {
        return CourierContent;
    }
    public void setCourierContent(String courierContent) {
        this.CourierContent = courierContent;
    }
    public int getUserId() {
        return UserId;
    }
    public void setUserId(int userId) {
        this.UserId = userId;
    }
    public String getCourierImage() {
        return CourierImage;
    }
    public void setCourierImage(String courierImage) {
        this.CourierImage = courierImage;
    }
    public float getCourierMoney() {
        return CourierMoney;
    }
    public void setCourierMoney(float courierMoney) {
        this.CourierMoney = courierMoney;
    }
    public String getCourierDate() {
        return CourierDate;
    }
    public void setCourierDate(String courierDate) {
        this.CourierDate = courierDate;
    }
    public int getOrderId() {
        return OrderId;
    }
    public void setOrderId(int orderId) {
        this.OrderId = orderId;
    }
    
        public String getCourierWeight() {
        return CourierWeight;
    }
    public void setCourierWeight(String courierWeight) {
        CourierWeight = courierWeight;
    }

        private int CourierId;                        //快递信息id
        private String CourierTitle;                //快递标题
        private String CourierContent;        //快递内容
        private int UserId;                            //发布人id
        private String CourierImage;            //快递图片
        private float CourierMoney;            //快递悬赏
        private String CourierDate;                //快递发布日期
        private int OrderId;                    //接单人
        private String CourierWeight;        //快递重量
}

请求的jsp页面相应代码

<form action="courierServlet?method=update" method="post" onsubmit="check(this)">  
    <table align="center" width="450">  
        <tr>  
            <td align="center" colspan="2">  
                <h2>添加图书信息</h2>  
                <hr>  
            </td>  
        </tr>  
  
        <tr>  
            <td align="right">快递ID:</td>  
            <td><input type="text" name="courierId"></td>  
        </tr>  
  
        <tr>  
            <td align="right">title:</td>  
            <td><input type="text" name="courierTitle"></td>  
        </tr>  
  
        <tr>  
            <td align="right">用户ID:</td>  
            <td><input type="text" name="userId" /></td>  
        </tr>  
  
        <tr>  
            <td align="right">content:</td>  
            <td><input type="text" name="courierContent" /></td>  
        </tr>  
        <tr>  
            <td align="center" colspan="2"><input type="submit" value="添 加">  
            </td>  
        </tr>  
    </table>  
</form>  

下图是对应表中的相应字段

阿神阿神2718 days ago571

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-04-18 09:43:48

    Your book object is empty, you can print the book before executing sql. For an empty object, its int attribute defaults to 0, and its String attribute defaults to null, so when you insert it, int is OK, but String reports an cannot be nullerror

    reply
    0
  • Cancelreply