我有一个庞大的程序,但是我的“查找产品”按钮不显示在描述JTextfield中键入的每种产品的价格,输出仅显示数据库中的一行,而忽略数据库中的其他行。
我的“添加客户”按钮还有另一个问题,为什么插入JTextfields中的新数据不保存到我拥有的实际数据库中?
这是我的代码:
package electronic.invoice.entry;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class ElectronicInvoiceEntry extends JFrame implements
ActionListener
{
JLabel Name = new JLabel("Name");
JTextField name = new JTextField(20);
JTextfield Address = new JLabel("Address");
JTextField address = new JTextField(20);
JLabel City = new JLabel("City");
JTextField city = new JTextField(20);
JLabel Description = new JLabel("Description");
JTextField description = new JTextField(20);
JLabel Province = new JLabel("Province");
JTextField province = new JTextField(20);
JLabel Zip = new JLabel("Zip");
JTextField zip = new JTextField(20);
JLabel ProductCode = new JLabel("Product Code");
JTextField productcode = new JTextField(20);
JLabel Invoice = new JLabel("Invoice Number");
JTextField invoice = new JTextField(20);
JLabel CustomerNum = new JLabel("Customer Number");
JTextField customernum = new JTextField(20);
JLabel ProductBought = new JLabel("Product Bought");
JComboBox productbought = new JComboBox();
JLabel Quanity = new JLabel("Quanity");
JTextField quanity = new JTextField(20);
JLabel Payment = new JLabel("Payment");
JTextField payment = new JTextField(20);
JLabel Price = new JLabel("Price");
JTextField price = new JTextField(20);
JLabel Deposit = new JLabel("Deposit");
JTextField deposit = new JTextField(20);
JLabel AllProducts = new JLabel("All Products: ");
JComboBox allProducts = new JComboBox();
JButton AddCustomer = new JButton("Add Customer");
JButton FindProduct = new JButton("Find Product");
JButton ListProduct = new JButton("List Product");
JButton AddInvoice = new JButton("Add Invoice");
JButton ShowInvoice = new JButton("Show Invoice");
JButton Exit = new JButton("Exit");
JButton WriteInvoice = new JButton("Write Invoice");
JButton Next = new JButton("Next");
public ElectronicInvoiceEntry() {
super.setTitle("Electronic Invoice Entry");
super.setBounds(500, 200, 500, 450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(0, 2));
pane.setBackground(Color.cyan);
this.getContentPane().add(pane);
pane.add(Name);
pane.add(name);
pane.add(Address);
pane.add(address);
pane.add(City);
pane.add(city);
pane.add(Description);
pane.add(description);
pane.add(Province);
pane.add(province);
pane.add(Zip);
pane.add(zip);
pane.add(ProductCode);
pane.add(productcode);
pane.add(Invoice);
pane.add(invoice);
pane.add(CustomerNum);
pane.add(customernum);
pane.add(ProductBought);
pane.add(productbought);
pane.add(Quanity);
pane.add(quanity);
pane.add(Payment);
pane.add(payment);
pane.add(Price);
pane.add(price);
pane.add(Deposit);
pane.add(deposit);
pane.add(AllProducts);
pane.add(allProducts);
pane.add(AddCustomer);
pane.add(FindProduct);
pane.add(ListProduct);
pane.add(AddInvoice);
pane.add(ShowInvoice);
pane.add(Exit);
pane.add(WriteInvoice);
pane.add(Next);
this.setVisible(true);
AddCustomer.addActionListener(this);
ListProduct.addActionListener(this);
ShowInvoice.addActionListener(this);
WriteInvoice.addActionListener(this);
FindProduct.addActionListener(this);
AddInvoice.addActionListener(this);
Exit.addActionListener(this);
Next.addActionListener(this);
AddCustomer.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/orion", "root", "");
System.out.println("Connection" + conn);
Statement st = conn.createStatement();
String query = ("INSERT INTO `customer`(`Customer_Number`, `Name`, `Address`, `City`, `Province`, `Zip`, `Deposit`)"
+ " VALUES ('Customer_Number', 'Name', 'Address', 'City', 'Province', 'Zip', 'Deposit')");
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, customernum.getText());
ps.setString(2, name.getText());
ps.setString(3, address.getText());
ps.setString(4, city.getText());
ps.setString(5, province.getText());
ps.setString(6, zip.getText());
ps.setString(7, deposit.getText());
ps.execute();
} catch (ClassNotFoundException ex) {
Logger.getLogger(ElectronicInvoiceEntry.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Saved");
}
}
});
ListProduct.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/orion", "root", "");
System.out.println("Connection" + conn);
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT `Description` FROM `product` ORDER BY Description");
while(rs.next()) {
productbought.addItem(rs.getString("Description"));
}
} catch (SQLException ex) {
Logger.getLogger(ElectronicInvoiceEntry.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
ShowInvoice.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
WriteInvoice.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
FindProduct.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/orion", "root", "");
System.out.println("Connection" + conn);
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT`Description`, `Price` FROM `product` WHERE `Description` IN "
+ "('Toaster','Hair dryer','Car vacuum')");
while(rs.next()) {
description.setText(rs.getString("Description"));
price.setText(rs.getString("Price"));
}
} catch (SQLException ex) {
Logger.getLogger(ElectronicInvoiceEntry.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
AddInvoice.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
Exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
Next.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
electronicInvoiceEntryTransaction frame = new electronicInvoiceEntryTransaction();
frame.setVisible(true);
} catch (Exception ex) {
}
}
});
}//end of constructor
public static void main(String[] args) {
ElectronicInvoiceEntry e = new ElectronicInvoiceEntry();
}//end of main method
@Override
public void actionPerformed(ActionEvent e) {
}
public class electronicInvoiceEntryTransaction extends JFrame {
JLabel Name = new JLabel("Name");
JTextField name = new JTextField(20);
JLabel CustomerNumber = new JLabel("Customer Number");
JTextField customernumber = new JTextField(20);
JLabel Balance = new JLabel("Balance");
JTextField balance = new JTextField(20);
JButton CheckBalance = new JButton("CheckBalance");
JButton Deposit = new JButton("Deposit");
JButton Calculate = new JButton("Calculate Payment and Deposit");
JButton Transaction = new JButton("Transaction");
public electronicInvoiceEntryTransaction() {
setTitle("Electronic Invoice Entry - Transaction");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(300, 300, 700, 400);
setVisible(true);
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(0, 2));
pane.setBackground(Color.green);
this.getContentPane().add(pane);
pane.add(Name);
pane.add(name);
pane.add(CustomerNumber);
pane.add(customernumber);
pane.add(Balance);
pane.add(balance);
pane.add(CheckBalance);
pane.add(Deposit);
pane.add(Calculate);
pane.add(Transaction);
CheckBalance.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
Deposit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/* INSERT INTO `account`(`Name`, `Customer_Number`, `Balance`)"
+ " VALUES ('Name','Customer_Number','Balance')*/
}
});
Calculate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
Transaction.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
}//end of constructor
}//end of class electronic transaction
}//end of main class
答案 0 :(得分:0)
按如下所示修改FindProduct操作侦听器,以收集所有结果集行的描述和价格:
FindProduct.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/orion", "root", "");
System.out.println("Connection" + conn);
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT `Description`, `Price` FROM `product` WHERE `Description` IN ('Toaster','Hair dryer','Car vacuum')");
StringBuilder sbDesc = new StringBuilder();
StringBuilder sbPrice = new StringBuilder();
while(rs.next()) {
sbDesc.append(rs.getString("Description")).append("\r\n");
sbPrice.append(rs.getString("Price")).append("\r\n");
}
description.setText(sbDesc.toString());
price.setText(sbPrice.toString());
} catch (SQLException ex) {
Logger.getLogger(ElectronicInvoiceEntry.class.getName()).log(Level.SEVERE, null, ex);
}
finally {
if(conn != null) {
try { conn.close(); }
catch(Exception e) { Logger.getLogger(ElectronicInvoiceEntry.class.getName()).log(Level.SEVERE, null, e); }
finally{ conn = null; }
}
}
}
});