有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

来自服务器的java重复密钥或完整性约束冲突消息:“列“volume”不能为null”

我已经用getmethod和set方法编写了一段代码,但在控制台中,它显示来自服务器的重复密钥或完整性约束冲突消息:

"Column 'volume' cannot be null"

并且只执行getmethod为什么不执行setmethod

 package com.glomindz.mercuri.dao;

 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.List;

 import com.glomindz.mercuri.pojo.Packages;
 import com.glomindz.mercuri.util.MySingleTon;

  public class PackageServicesDAO {

private Connection connection;

public PackageServicesDAO() {
    // connection = new MySingleTon().getConnection();
    connection = MySingleTon.getInstance().getConnection();

}

public List<Packages> get_all_data() {
    List<Packages> packagesList = new ArrayList<Packages>();
     String query = "SELECT * FROM spl_package_master";
    try {
         PreparedStatement stmt = connection.prepareStatement(query);
         boolean execute = stmt.execute();
        System.out.println(execute);
        ResultSet resultSet = stmt.getResultSet();
        System.out.println(resultSet.getMetaData());
        while (resultSet.next()) {
            Packages pack = new Packages();
            pack.setId(resultSet.getInt("id"));
            pack.setVolume(resultSet.getString("volume"));
            pack.setUnit_type(resultSet.getString("unit_type"));
            pack.setQuantity_in_box(resultSet.getInt("quantity_in_box"));
            pack.setType(resultSet.getString("type"));
            packagesList.add(pack);
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
       return packagesList;
    } 

    public boolean set_all_data() {
        Packages packages = new Packages();
        boolean result = true;
            try {
             PreparedStatement stmt = connection.prepareStatement("INSERT INTO spl_package_master(volume,unit_type,quantity_in_box,type)VALUES(?,?,?,?)");
             stmt.setString(1, packages.getVolume());
             stmt.setString(2, packages.getUnit_type());
             stmt.setInt(3, packages.getQuantity_in_box());
             stmt.setString(4, packages.getType());


             result = stmt.execute();


        } 

        catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
        }


   public static void main(String[] args) {
    PackageServicesDAO pac = new PackageServicesDAO();
    List<Packages> data = pac.get_all_data();
    boolean data1 = pac.set_all_data();
    pac.setVolume("450");
    pac.setUnitType("ml");
    pac.setQuantity_in_box("25");
    pac.setType("glass");
    System.out.println(data);
    System.out.println(data1);
    System.exit(0);
   }

   private void setType(String string) {
    // TODO Auto-generated method stub

    }

   private void setQuantity_in_box(String string) {
    // TODO Auto-generated method stub

   }

    private void setUnitType(String string) {
    // TODO Auto-generated method stub

   }

    private void setVolume(String string) {
    // TODO Auto-generated method stub

    }

    }

package类是 包com。格洛明兹。默库里。波乔

   public class Packages {

private int  id;

    private String  volume;

    private String  unit_type;

    private int  quantity_in_box;

    private String  type;


    public int getId() {
    return id;
    }

   public void setId(int id) {
    this.id = id;
    }

   public String getVolume() {
    return volume;
   }

  public void setVolume(String volume) {
   this.volume = volume;
    }

  public String getUnit_type() {
  return unit_type;
    }

  public void setUnit_type(String unit_type) {
  this.unit_type = unit_type;
  }

  public int getQuantity_in_box() {
  return quantity_in_box;
  }

   public void setQuantity_in_box(int quantity_in_box) {
  this.quantity_in_box = quantity_in_box;
   }

  public String getType() {
  return type;
   }

   public void setType(String type) {
  this.type = type;
    }


    @Override
   public String toString() {
  return "Packages [id=" + id + ",volume=" + volume + ", unit_type=" + unit_type + ",     quantity_in_box=" + quantity_in_box
          + ", type=" + type + " ]";
        }



      }

我已经修改了我的类,但是它在控制台中给了我输出,但是用setmethod编写的值不能插入到数据库表中

    package com.glomindz.mercuri.dao;

    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;

    import com.glomindz.mercuri.pojo.Packages;
    import com.glomindz.mercuri.util.MySingleTon;

    public class PackageServicesDAO {

private Connection connection;

public PackageServicesDAO() {
    // connection = new MySingleTon().getConnection();
    connection = MySingleTon.getInstance().getConnection();

}

public List<Packages> get_all_data() {
    List<Packages> packagesList = new ArrayList<Packages>();
     String query = "SELECT * FROM spl_package_master";
    try {
         PreparedStatement stmt = connection.prepareStatement(query);
         boolean execute = stmt.execute();
        System.out.println(execute);
        ResultSet resultSet = stmt.getResultSet();
        System.out.println(resultSet.getMetaData());
        while (resultSet.next()) {
            Packages pack = new Packages();
            pack.setId(resultSet.getInt("id"));
            pack.setVolume(resultSet.getString("volume"));
            pack.setUnit_type(resultSet.getString("unit_type"));
            pack.setQuantity_in_box(resultSet.getInt("quantity_in_box"));
            pack.setType(resultSet.getString("type"));
            packagesList.add(pack);
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
       return packagesList;
    } 

    public Packages set_all_data() {
        Packages packages = new Packages();
        try {
             PreparedStatement stmt = connection.prepareStatement("INSERT INTO spl_package_master(id,volume,unit_type,quantity_in_box,type)VALUES(?,?,?,?,?)");
             stmt.setInt(1, packages.getId());
             stmt.setString(2, packages.getVolume());
             stmt.setString(3, packages.getUnit_type());
             stmt.setInt(4, packages.getQuantity_in_box());
             stmt.setString(5, packages.getType());
             packages.setId(13);
             packages.setVolume("450");
             packages.setUnit_type("ml");
             packages.setQuantity_in_box(25);
             packages.setType("glass");

        } 

        catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return packages;
        }


   public static void main(String[] args) {
    PackageServicesDAO pac = new PackageServicesDAO();
    List<Packages> data = pac.get_all_data();
    Packages data1 = pac.set_all_data();

    System.out.println(data);
    System.out.println(data1);
    System.exit(0);
          }
           }

共 (3) 个答案

  1. # 1 楼答案

    它抛出异常,因为在插入到D.B之前对象中没有设置值

      public boolean set_all_data() {
            Packages packages = new Packages();
            boolean result = true;
                try {
                 PreparedStatement stmt = connection.prepareStatement("INSERT INTO spl_package_master(volume,unit_type,quantity_in_box,type)VALUES(?,?,?,?)");
                 stmt.setString(1, packages.getVolume());
                 stmt.setString(2, packages.getUnit_type());
                 stmt.setInt(3, packages.getQuantity_in_box());
                 stmt.setString(4, packages.getType());
    
    
                 result = stmt.execute();
    
    
            } 
    

    在这里,您创建了“Packages”的新实例,并且没有设置任何值。现在包类中的所有get方法都为null。然后将其插入到D.B中。在D.B中,您已将“volume”字段声明为非null。在将包实例插入D.B之前,需要在包实例的内部方法中设置值

  2. # 2 楼答案

    您正在初始化属性之前调用set方法

    boolean data1 = pac.set_all_data();
    //and after you set properties which are never used.
    pac.setVolume("450");
    pac.setUnitType("ml");
    pac.setQuantity_in_box("25");
    pac.setType("glass");
    

    尝试在构造函数中设置这些包

    Packages packages = new Packages(); // packeges object has properties not initialized I guess
    

    例如:

    Packages packages = new Packages("450", "ml", "25", "glass"); // and set these in constructor
    
  3. # 3 楼答案

    set_all_data()中,您正在插入未设置数据的emtpy Packages(这是您得到“列“volume”不能为null”的原因)
    使用setter或构造函数设置packages数据