0

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

“列‘卷’不能为空”

并且只执行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

    }

    }

包类是包 com.glomindz.mercuri.pojo;

   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);
          }
           }
4

3 回答 3

0

set_all_data()您插入一个Packages未设置数据的 emtpy 时(这是您得到“列'卷'不能为空”的原因)。使用 setter 或使用构造函数
设置数据。packages

于 2013-08-05T07:32:39.367 回答
0

由于在插入数据库之前未在对象中设置值,因此引发异常

  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”的新实例并且没有设置任何值。现在 Package 类中的所有 get 方法都为空。然后将其插入 DB 在 DB 中,您已将“volume”字段声明为非空。在将其插入数据库之前,您需要在方法内部的包实例中设置值

于 2013-08-05T07:26:43.803 回答
0

您在初始化属性之前调用 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 packages = new Packages(); // packeges object has properties not initialized I guess

例如:

Packages packages = new Packages("450", "ml", "25", "glass"); // and set these in constructor
于 2013-08-05T07:18:55.470 回答