我已经编写了代码,但是我无法编写完整的 set 方法的代码。请告诉我如果我正在制作用户界面,那么我应该如何编写 set 方法。我的代码是 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.Package;
import com.glomindz.mercuri.util.MySingleTon;
public class PackageServicesDAO {
private Connection connection;
public PackageServicesDAO() {
// connection = new MySingleTon().getConnection();
connection = MySingleTon.getInstance().getConnection();
}
public List<Package> get_all_data() {
List<Package> packageList = new ArrayList<Package>();
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()) {
Package pack = new Package();
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"));
packageList.add(pack);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return packageList;
}
public boolean set_all_data() {
Package packages = new Package();
boolean result = false;
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 pacdao = new PackageServicesDAO();
List<Package> data = pacdao.get_all_data();
boolean data1 = pacdao.set_all_data();
System.out.println(data);
System.out.println(data1);
System.exit(0);
}
}