我正在制作一个Java简单的应用程序以更好地学习代码,并结合了intellji上的一些GUI我已经有一个主页,我只是尝试制作一个弹出表,其中包含我产品上的所有数据,这是我的tableView代码:
public class ProductTable extends JFrame
{
private Model.product product;
private List<product> list;
public ProductTable() {
list=product.ProductList();
//headers for the table
String[] columns = {"Product Name",
"Quantity",
"Expiration day",
"Need fridge"};
Object[][] data = new Object[][] {
};
//create table with data
JTable table = new JTable(data, columns);
//add the table to the frame
this.add(new JScrollPane(table));
this.setTitle("ProductList");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ProductTable();
}
});
}
}
关于我拥有的产品类别:
product.name =字符串
product.quantity = int
Product.Expiration_day = String
product.fridge =布尔值
我想加载到字符串对象中
Data = {{product.name,product.quantity,Product.Expiration_day,Product.fridge},{.....,....,....,...} {..... .........}};
直到我现在放出所有数据,我知道如何知道大小,但我需要将其逐个加载到数据对象中,任何人都可以通过它进行引导吗?!
我的产品代码:
package Model;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class product
{
private String product_name;
private Integer product_quantity;
private String experation_day;
private boolean fridge = false;
private boolean matched= false;
private Connection connection;
public product()
{
this.product_name = "";
this.product_quantity = 0;
this.experation_day = "";
}
public product(String product_name, Integer product_quantity, String experation_day) {
this.product_name = product_name;
this.product_quantity = product_quantity;
this.experation_day = experation_day;
}
public Integer getProduct_quantity() {
return product_quantity;
}
public String getExperation_day() {
return experation_day;
}
public String getProduct_name()
{
return product_name;
}
public void setProduct_quantity(Integer product_quantity) {
this.product_quantity = product_quantity;
}
public void setExperation_day(String experation_day) {
this.experation_day = experation_day;
}
public void setProduct_name(String product_name)
{
this.product_name = product_name;
}
public List<product> ProductList() {
String windownsUserName = System.getProperty("user.name");
try
{
System.out.println("Connecting to a selected database...");
Connection connection = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\"+windownsUserName+"\\IdeaProjects\\Software-engineer-final-project\\untitled\\src\\DButills\\account.db");
System.out.println("Connected database successfully...");
String sqlQuary = "SELECT product_name, product_amount, product_expration_date FROM products";
PreparedStatement ps = connection.prepareStatement(sqlQuary);
ResultSet rs = ps.executeQuery();
{
}
List<product> products = new ArrayList<>();
while (rs.next()) {
product product = new product();
product.setProduct_quantity(rs.getInt("product_amount"));
product.setProduct_name(rs.getString("product_name"));
product.setExperation_day(rs.getString("product_expration_date"));
products.add(product);
}
return products;
} catch (SQLException e) {
System.out.println("Query failed: " + e.getMessage());
return null;
}
}
}
为什么需要String?根据您提供的代码-您需要的是数组Object [] [] [],但使用值填充,因此只需遍历产品列表并填充data以正确的顺序。