我正在开发的系统是一个系统,JTable
充当orderList
。我已经尝试了Vector
并尝试使用DefaultTableModel
,但我不确定如何使按钮拉取数据并将其添加到表格中。我知道这很难遵循,但有人可能会告诉我应该如何为JButton
做一个,然后其余应该是相似的,我可以自己做吗?
我需要从数据库中提取productID
,productName
和Price
并添加到表格中。
然后我将总计价格并为订单付款。
Order
//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
// 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;
}
}
// Small Americano Listener
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();
}
}
});
答案 0 :(得分:3)
您的代码有点偏差,您有一个Stock
对象,但该对象似乎也在管理数据库,作为一个更长期的解决方案,我建议将这些问题分开,但是现在我们&# 39;暂时离开那个。
我要做的第一件事是定义模型的期望,你期望它做什么,你期望它如何工作
public interface MutableStockModel {
public void add(Stock item) throws ModelException;
public void remove(Stock item) throws ModelException;
}
所以这是一个简单的接口,它定义了任何实现类都应该提供的几个动作。但你为什么这么问?这个问题我很快就会回答。
您可以从MutableStockModel
扩展TableModel
,但这会阻止您进入可能无法满足您长期需求的特定实施渠道,此外,任何使用它的人都会关心,他们只是希望能够添加和删除产品
示例实现......
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
具有&#34;行&#34;的概念,MutableStockModel
没有。{1}}。当然,您必须更新此特定implement
的任何实施interface
;)
好的,但是&#34;为什么&#34;?
简短的回答是,根据数据库,ResultSet
实际上可以直接变异,也就是说,您可以通过ResultSet
直接添加/插入,删除和更新行,请参阅{{ 3}}了解更多细节。
这意味着您可以创建TableModel
的实现,该实现实现MutableStockTableModel
或MutableStockModel
接口,但引用了原始的ResultSet
,因为它是&#39;内部结构。
听起来很简单,但是当您意识到可以为任何满足实现要求的实现更改TableModel
的任何实例而不影响您的任何代码时,它会开启一大堆可能性。
因此,例如,您可以使用
代替在代码中使用TableModel
或DefaultTableModel
private MutableStockTableModel tableModel;
//...
tableModel = new ResultSetMutableStockTableModel(resultSet);
table.setModel(tableModel);
//...
tableModel.add(stockItem);
但你可以简单地为不同的实例更改tableModel
...
tableModel = new DefaultStockTableModel(listOfItems);
没有别的东西需要改变!
所以,这是&#34;编码接口的基本例子,而不是实现&#34;我可以进一步解耦代码,但我不想让你淹没;)
回到我的第一条评论,OO宣传Retrieving and Modifying Values from Result Sets,意味着任何对象应该只有一个责任(或工作)。
这可能表明,例如,您应该将Stock
类的功能分开。
我的,我开始,惊讶,interface
......
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;
}
}
我还建议您查看Single Responsibility Principle以改进整体代码
答案 1 :(得分:1)
首先:当您想要使用SELECT时,您必须使用stmt.executeQuery(sqlString);
或stmt.execute(sqlString);
代替executeUpdate
,当您想要插入时更新或删除:
stmt.executeQuery(sqlString);
第二:我认为您遗漏了网址中缺少端口号的内容:
"jdbc:mysql://localhost/team_project?useSSL=false";
//---------------------^--------------------------
应该是这样,MySQL的默认端口是3306
:
"jdbc:mysql://localhost:3306/team_project?useSSL=false";
注意使用语句不安全,可能会导致语法错误或SQL注入,相反我建议使用PreparedStatement,它更安全,更有帮助
要填充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
});
}
}
答案 2 :(得分:-2)
这是一个Minimal, Complete, and Verifiable example (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;
}
}