我已经用 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);
}
}