我正在开发的系统是一个收银台系统,JTable
充当orderList
。我尝试过Vector
,也尝试过使用DefaultTableModel
,但我不确定如何让按钮拉取数据并将其添加到表中。我知道这很难理解,但有人能告诉我应该如何为一个JButton
做这件事吗?然后其余的应该类似,我可以自己做吗?
我需要productID
,productName
和Price
从DB中提取并添加到表中。
然后我将合计价格并为订单付款。
//Order class for setting up and managing an order
package classes;
import java.sql.*;
import java.util.ArrayList;
import java.util.Vector;
public class Order
{
// Instance Variables
private int productID;
private String productName;
private String productDescription;
private String type;
private String supplierName;
private double quantity;
private double price;
// Connection To DB
// JDBC Driver name and database URL
final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
final String DB_URL = "jdbc:mysql://localhost/team_project?useSSL=false";
final String USER_NAME = "root";
final String PASSWORD = "password";
// sql variables
ResultSet resultSet = null;
private String itemName;
private double itemPrice;
// Constructors
public Order()
{
}
public Order(int productID, String itemName, double itemPrice, double quantity)
{
this.itemName = itemName;
this.itemPrice = itemPrice;
this.quantity = quantity;
}
// Get the details of stock items and add them to the order
public Vector getOrderItems()
{
ResultSet rs = null;
Statement statement = null;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);
Statement stmt = conn.createStatement();
String sqlString= "select ProductID, ProductName, Price from product";
stmt.executeUpdate(sqlString);
Vector vector = new Vector();
Vector<String> orderItem = new Vector<String>();
int i=0;
while(rs.next())
{
Vector<String> items = new Vector<String>();
rs.getInt("ProductID");
rs.getString("ProductName");
rs.getDouble("Price");
}
return vector;
}catch(Exception e)
{
e.printStackTrace();
}
return null;
}
// Getter Methods
public int getProductID()
{
return productID;
}
public String getProductName()
{
return productName;
}
public String getProductDesc()
{
return productDescription;
}
public String getType()
{
return type;
}
public String getSupplierName()
{
return supplierName;
}
public double getQuantity()
{
return quantity;
}
public double getPrice()
{
return price;
}
}
// Stock class
//Team Project
//Stock class for setting up and managing stock
package classes;
import java.sql.*;
import java.util.*;
public class Stock
{
// Instance Variables
private int productID;
private String productName;
private String productDescription;
private String type;
private String supplierName;
private double quantity;
private double price;
// JDBC Driver name and database URL
final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
final String DB_URL = "jdbc:mysql://localhost/team_project?useSSL=false";
final String USER_NAME = "root";
final String PASSWORD = "password";
// sql variables
Statement statement = null;
ResultSet resultSet = null;
public Stock()
{
productID=0;
productName=null;
productDescription = null;
type = null;
supplierName=null;
quantity = 0;
price=0;
}
// Initialisation Constructor that initializes everything in DB
public Stock(int productID, String productName, String productDescription, String type, String supplierName,
double quantity, double price)
{
this.productID = productID;
this.productName = productName;
this.productDescription = productDescription;
this.type = type;
this.supplierName = supplierName;
this.quantity = quantity;
this.price = price;
}
// Add a new product into the product table
public void addProduct(int prodID, int amt)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);
Statement stmt = conn.createStatement();
String sqlString="insert into product " + "(ProductID, ProductName, ProductDescrp, type, SupplierName, Quantity, Price)"
+ " values(30, 'Marmalade', 'Homestead', 'Extras', 'Bakersworld', 20, 0.20)";
stmt.executeUpdate(sqlString);
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
// Delete a product from the product table
public void delete(int prodNumIn)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);
Statement stmt = conn.createStatement();
String sqlString= "delete from team_Project.product where ProductID=" + prodNumIn;
stmt.executeUpdate(sqlString);
}
catch(Exception e)
{
e.printStackTrace();
}
}
// Subtracts quantity in stock in the DB When a sale is made on an item
public int deductStock(int prodID)
{
int status =0;
String sql = ("UPDATE product " + "SET Quantity = " + (getDBQuantity(prodID)-1) + " WHERE ProductID = " + prodID);
status = databaseUpdate(sql);
return status;
}
// Add quantity to particular product in the DB if required
public int addToQuantity(int prodID, int amt)
{
int status =0;
String sql = ("UPDATE product " + "SET Quantity = " + (getDBQuantity(prodID)+ amt) + " WHERE ProductID = " + prodID);
status = databaseUpdate(sql);
return status;
}
// return quantity of a product in DB
public int getDBQuantity(int prodID)
{
int quantity=0;
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);
statement = (Statement) conn.createStatement();
resultSet = statement.executeQuery("select Quantity from team_project.product WHERE ProductID = " + prodID);
while (resultSet.next())
{
quantity = (resultSet.getInt("Quantity"));
}
conn.close();
}catch(Exception e)
{
e.printStackTrace();
}
return quantity;
}
// get price of a particular product
public int getItemPrice(int prodID)
{
int price = 0;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);
statement = (Statement) conn.createStatement();
resultSet = statement.executeQuery("select Price from team_project.product WHERE ProductID = " + prodID);
while (resultSet.next())
{
price = (resultSet.getInt("Price"));
}
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("€"+price);
return price;
}
// Method that returns all products in product table
public ArrayList<Stock> getProducts()
{
ArrayList<Stock> allStock = new ArrayList<Stock>();
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);
statement = (Statement) conn.createStatement();
resultSet = statement.executeQuery("select * from team_project.product");
while (resultSet.next())
{
Stock stock = new Stock(resultSet.getInt("ProductID"), resultSet.getString("ProductName"),
resultSet.getString("ProductDescrp"),resultSet.getString("Type"),resultSet.getString("SupplierName"),
resultSet.getInt("Quantity"), resultSet.getDouble("Price"));
allStock.add(stock);
}
conn.close();
}catch(Exception e)
{
e.printStackTrace();
}
return allStock;
}
// update method to call
// database update method
private int databaseUpdate(String sqlUpdate)
{
int status = 0;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);
statement = conn.createStatement();
status = statement.executeUpdate(sqlUpdate);
conn.close();
}
catch (Exception e) {
e.printStackTrace();
}
return status;
}
// toString method for stock items
public String toString()
{
return productName + "";
}
// Getter Methods
public int getProductID()
{
return productID;
}
public String getProductName()
{
return productName;
}
public String getProductDesc()
{
return productDescription;
}
public String getType()
{
return type;
}
public String getSupplierName()
{
return supplierName;
}
public double getQuantity()
{
return quantity;
}
public double getPrice()
{
return price;
}
}
//小型美式监听器
americanoSmall.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try {
String query = "select ProductName, Price from product where ProductID = 24";
java.sql.PreparedStatement pst = connection.prepareStatement(query);
ResultSet rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e1)
{
e1.printStackTrace();
}
}
});
您的代码有点倾斜,您有一个Stock
对象,但该对象似乎也在管理数据库,作为一个更长期的解决方案,我建议分离这些关注点,但现在我们将暂时保留它。
我要做的第一件事是定义模型的期望,你期望它做什么,你期望它如何工作
public interface MutableStockModel {
public void add(Stock item) throws ModelException;
public void remove(Stock item) throws ModelException;
}
所以这是一个简单的接口,它定义了任何实现类都应该提供的几个操作。但是你为什么要问呢?这是一个很好的问题,我很快就会回答。
您可以从TableModel
扩展MutableStockModel
,但这会将您锁定在一个特定的实现通道中,这可能无法满足您的长期需求,此外,任何使用它的人都关心什么,他们只是希望能够添加和删除产品
一个示例实现…
public class DefaultStockTableModel extends AbstractTableModel implements MutableStockModel {
private List<Stock> items;
public DefaultStockTableModel() {
items = new ArrayList<>(25);
}
public DefaultStockTableModel(List<Stock> items) {
this.items = items;
}
@Override
public int getRowCount() {
return items.size();
}
@Override
public int getColumnCount() {
return 7;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 1:
case 2:
case 3:
case 4: return String.class;
case 0:
case 5:
case 7: return int.class;
}
return Object.class;
}
@Override
public String getColumnName(int column) {
switch (column) {
case 0: return "ID";
case 1: return "Name";
case 2: return "Description";
case 3: return "Type";
case 4: return "Supplier Name";
case 5: return "Quanity";
case 6: return "Price";
}
return null;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Stock item = items.get(rowIndex);
switch (columnIndex) {
case 0: return item.getProductID();
case 1: return item.getProductName();
case 2: return item.getProductDesc();
case 3: return item.getType();
case 4: return item.getSupplierName();
case 5: return item.getQuantity();
case 6: return item.getPrice();
}
return null;
}
@Override
public void add(Stock item) throws ModelException {
// Add the item to the database if required
items.add(item);
int row = items.indexOf(item);
fireTableRowsInserted(row, row);
}
@Override
public void remove(Stock item) throws ModelException {
if (items.contains(item)) {
// Delete theitem from the database if required
int row = items.indexOf(item);
items.remove(row);
fireTableRowsDeleted(row, row);
}
}
}
所以这是一个基本的例子,你的代码都不关心它是如何实际管理的,只关心它满足合同要求。
要添加或删除产品,只需调用特定方法。
现在,您可以扩展此概念以使其更易于使用,例如…
public interface MutableStockTableModel extends MutableStockModel, TableModel {
public void removeAt(int row) throws ModelException;
}
这是一个特定的、有针对性的实现,它允许您删除特定行中的一行。这扩展了TableModel
,因为TableModel
有“行”的概念,而MutableStockModel
没有。当然,您必须更新任何实现实现
这个特定的接口
;)
好吧,但是“为什么”?
简而言之,根据数据库的不同,ResultSet
实际上可以直接变异,也就是说,您可以直接通过ResultSet
添加/插入、删除和更新行,有关详细信息,请参阅从结果集中检索和修改值。
这意味着您可以创建一个TableModel
的实现,它实现了MutableStockTableModel
或MutableStockModel
接口,但它引用了原始的ResultSet
作为其内部结构。
听起来很简单,但是当你意识到你可以为任何满足实现要求的实现更改TableModel
的任何实例,而不会影响你的任何代码时,它就打开了一种可能性。
因此,例如,您可以使用TableModel
或DefaultTableModel
而不是在代码中使用TableModel
private MutableStockTableModel tableModel;
//...
tableModel = new ResultSetMutableStockTableModel(resultSet);
table.setModel(tableModel);
//...
tableModel.add(stockItem);
但是您可以简单地为不同的实例更改tableModel
…
tableModel = new DefaultStockTableModel(listOfItems);
没有什么需要改变!
所以,这是“编码到接口,而不是实现”的基本示例,我可以进一步解耦代码,但我不想让你不知所措;)
回到我的第一个评论,OO提倡单一职责原则,这意味着任何对象都应该有一个单一的职责(或工作)。
例如,这可能建议您应该分离Stock
类的功能。
我的,我会从惊喜开始,一个界面
…
public interface Stock {
public int getProductID();
public String getProductName();
public String getProductDesc();
public String getType();
public String getSupplierName();
public double getQuantity();
public double getPrice();
}
然后你会有某种实现…
public class DefaultStock implements Stock {
// Instance Variables
private int productID;
private String productName;
private String productDescription;
private String type;
private String supplierName;
private double quantity;
private double price;
public DefaultStock() {
productID = 0;
productName = null;
productDescription = null;
type = null;
supplierName = null;
quantity = 0;
price = 0;
}
// Initialisation Constructor that initializes everything in DB
public DefaultStock(int productID, String productName, String productDescription, String type, String supplierName,
double quantity, double price) {
this.productID = productID;
this.productName = productName;
this.productDescription = productDescription;
this.type = type;
this.supplierName = supplierName;
this.quantity = quantity;
this.price = price;
}
// toString method for stock items
@Override
public String toString() {
return productName + "";
}
// Getter Methods
@Override
public int getProductID() {
return productID;
}
@Override
public String getProductName() {
return productName;
}
@Override
public String getProductDesc() {
return productDescription;
}
@Override
public String getType() {
return type;
}
@Override
public String getSupplierName() {
return supplierName;
}
@Override
public double getQuantity() {
return quantity;
}
@Override
public double getPrice() {
return price;
}
}
然后你可能会有某种经理/控制者/工厂…
(是的,我很想通过某种界面来描述这一点,但这已经是一篇很长的文章了)
public class StockManager {
// JDBC Driver name and database URL
final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
final String DB_URL = "jdbc:mysql://localhost/team_project?useSSL=false";
final String USER_NAME = "root";
final String PASSWORD = "password";
// sql variables
Statement statement = null;
ResultSet resultSet = null;
// Add a new product into the product table
public void addProduct(int prodID, int amt) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);
Statement stmt = conn.createStatement();
String sqlString = "insert into product " + "(ProductID, ProductName, ProductDescrp, type, SupplierName, Quantity, Price)"
+ " values(30, 'Marmalade', 'Homestead', 'Extras', 'Bakersworld', 20, 0.20)";
stmt.executeUpdate(sqlString);
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// Delete a product from the product table
public void delete(int prodNumIn) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);
Statement stmt = conn.createStatement();
String sqlString = "delete from team_Project.product where ProductID=" + prodNumIn;
stmt.executeUpdate(sqlString);
} catch (Exception e) {
e.printStackTrace();
}
}
// Subtracts quantity in stock in the DB When a sale is made on an item
public int deductStock(int prodID) {
int status = 0;
String sql = ("UPDATE product " + "SET Quantity = " + (getDBQuantity(prodID) - 1) + " WHERE ProductID = " + prodID);
status = databaseUpdate(sql);
return status;
}
// Add quantity to particular product in the DB if required
public int addToQuantity(int prodID, int amt) {
int status = 0;
String sql = ("UPDATE product " + "SET Quantity = " + (getDBQuantity(prodID) + amt) + " WHERE ProductID = " + prodID);
status = databaseUpdate(sql);
return status;
}
// return quantity of a product in DB
public int getDBQuantity(int prodID) {
int quantity = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);
statement = (Statement) conn.createStatement();
resultSet = statement.executeQuery("select Quantity from team_project.product WHERE ProductID = " + prodID);
while (resultSet.next()) {
quantity = (resultSet.getInt("Quantity"));
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return quantity;
}
// get price of a particular product
public int getItemPrice(int prodID) {
int price = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);
statement = (Statement) conn.createStatement();
resultSet = statement.executeQuery("select Price from team_project.product WHERE ProductID = " + prodID);
while (resultSet.next()) {
price = (resultSet.getInt("Price"));
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("€" + price);
return price;
}
// Method that returns all products in product table
public ArrayList<Stock> getProducts() {
ArrayList<Stock> allStock = new ArrayList<Stock>();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);
statement = (Statement) conn.createStatement();
resultSet = statement.executeQuery("select * from team_project.product");
while (resultSet.next()) {
Stock stock = new DefaultStock(resultSet.getInt("ProductID"), resultSet.getString("ProductName"),
resultSet.getString("ProductDescrp"), resultSet.getString("Type"), resultSet.getString("SupplierName"),
resultSet.getInt("Quantity"), resultSet.getDouble("Price"));
allStock.add(stock);
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return allStock;
}
// update method to call
// database update method
private int databaseUpdate(String sqlUpdate) {
int status = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD);
statement = conn.createStatement();
status = statement.executeUpdate(sqlUpdate);
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return status;
}
}
我还鼓励您查看使用预准备语句来改进整体代码
首先:当您想使用SELECT时,您必须使用stmt.执行查询(sqlString);
或stmt.执行(sqlString);
而不是执行更新
它在您想进行插入更新或删除时使用:
stmt.executeQuery(sqlString);
第二:我认为你在URL中缺少了一些东西,你缺少端口号:
"jdbc:mysql://localhost/team_project?useSSL=false";
//---------------------^--------------------------
应该是这样的,MySQL的默认端口是3306
:
"jdbc:mysql://localhost:3306/team_project?useSSL=false";
注意使用语句是不安全的,它可能会导致语法错误或SQL注入,相反,我建议使用准备语句,它更安全,更有帮助
要填充您的JTable,我通常使用这种方式:
//call this method to fill your Object in your JTable when you click to your button
private void fillData(List<MY_OBJECT> list) {//method which take a list of objects
DefaultTableModel model = (DefaultTableModel) jTableName.getModel();
// empty your JTable
model.setRowCount(0);
//Fill your JTbale
for (MY_OBJECT obj : list) {
model.addRow(new Object[]{
obj.getAttribute1(),
obj.getAttribute2,
obj.getAttribute3
});
}
}
以下是如何填充表的最小、完整和可验证示例(MCVE):
public class TableAddRowButton {
public static class Order {
static int idCounter = 0;
private int productID = ++idCounter;
private String productName = "Product" + productID;
private double quantity = new Random().nextInt(1000);
private double price = new Random().nextInt(1000);
}
public static void main(String[] args) {
Vector<Order> orders = new Vector<>();
DefaultTableModel tableModel = createTableModel(orders);
JButton button = createAddRowButton(orders, tableModel);
JPanel panel = new JPanel(new BorderLayout());
panel.add(button, BorderLayout.NORTH);
panel.add(new JScrollPane(new JTable(tableModel)));
JOptionPane.showMessageDialog(null, panel);
}
private static JButton createAddRowButton(Vector<Order> orders, DefaultTableModel tableModel) {
JButton button = new JButton(new AbstractAction("add row") {
@Override
public void actionPerformed(ActionEvent arg0) {
orders.add(new Order());
tableModel.fireTableRowsInserted(orders.size() - 2, orders.size() - 1);
}
});
return button;
}
private static DefaultTableModel createTableModel(Vector<Order> orders) {
DefaultTableModel tableModel = new DefaultTableModel(
new Vector<>(Arrays.asList("ID", "Name", "quantity", "price")), orders.size()) {
@Override
public int getRowCount() {
return orders.size();
}
@Override
public Object getValueAt(int row, int column) {
switch (column) {
case 0:
return orders.get(row).productID;
case 1:
return orders.get(row).productName;
case 2:
return orders.get(row).quantity;
case 3:
return orders.get(row).price;
default:
return "";
}
}
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
return tableModel;
}
}