我對Java相當陌生,而且我對sql也不是很熟悉。我正在研究一個需要鋪地訂單並將它們存儲在SQL數據庫中的程序。名稱和地址將存儲得很好,但其餘信息不會。信息檢索也不起作用。我不確定我做錯了什麼。我從Connection對象中刪除信息的原因很明顯,但我可以向你保證,這有我的程序中的正確信息..任何幫助,將不勝感激。在使用Java/sql存儲和檢索數據時遇到問題
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.*;
import javax.swing.*;
public class FlooringOrders extends JFrame implements ActionListener {
private JLabel lblFloorType, lblRoomSize, lblLength, lblWidth, lblName,
lblAddress, lblOrderInfo, lblTotal;
private JRadioButton rbtnWood, rbtnCarpet;
private JButton btnCalculate, btnSubmit, btnOrders;
private ButtonGroup grpType;
private JTabbedPane tab;
private JTextField txtLength, txtWidth, txtArea, txtName, txtAddress;
private JTextArea txtOrderInfo, txtTotal, txtCustomer;
private JPanel p1, p2, p3, p4;
private double Length, Width, Area, Total;
private String sLength, sWidth, sArea, sTotal, fType, insertQuery;
private PreparedStatement myQuery;
private ResultSet rs;
private DecimalFormat df;
public FlooringOrders() {
super("Flooring Orders");
df = new DecimalFormat("#,###,###.00");
tab = new JTabbedPane();
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
lblFloorType = new JLabel("Choose Floor Type: ");
lblRoomSize = new JLabel("Enter Room Size: ");
lblLength = new JLabel("Length (ft): ");
lblWidth = new JLabel("Width (ft): ");
lblName = new JLabel("Enter Name: ");
lblAddress = new JLabel("Enter Address: ");
lblOrderInfo = new JLabel("Order Info: ");
lblTotal = new JLabel("Total: ");
rbtnWood = new JRadioButton("Wood - $20/sq. ft.");
rbtnCarpet = new JRadioButton("Carpet - $10/sq. ft.");
grpType = new ButtonGroup();
btnCalculate = new JButton("Calculate Area");
btnSubmit = new JButton("Submit");
btnOrders = new JButton("Customer Orders");
txtLength = new JTextField(10);
txtWidth = new JTextField(10);
txtArea = new JTextField(10);
txtName = new JTextField(10);
txtAddress = new JTextField(10);
txtOrderInfo = new JTextArea(15, 15);
txtTotal = new JTextArea(15, 15);
txtCustomer = new JTextArea(15, 15);
grpType.add(rbtnCarpet);
grpType.add(rbtnWood);
getContentPane().add(tab);
p1.add(lblFloorType);
p1.add(rbtnWood);
p1.add(rbtnCarpet);
p1.add(lblRoomSize);
p1.add(lblLength);
p1.add(txtLength);
p1.add(lblWidth);
p1.add(txtWidth);
p1.add(btnCalculate);
p1.add(txtArea);
tab.add("Flooring", p1);
p2.add(lblName);
p2.add(txtName);
p2.add(lblAddress);
p2.add(txtAddress);
p2.add(btnSubmit);
tab.add("Customer Info", p2);
p3.add(lblOrderInfo);
p3.add(txtOrderInfo);
p3.add(lblTotal);
p3.add(txtTotal);
tab.add("Total", p3);
p4.add(btnOrders);
p4.add(txtCustomer);
tab.add("Order List", p4);
btnCalculate.addActionListener(this);
btnSubmit.addActionListener(this);
btnOrders.addActionListener(this);
} //end constructor
public void actionPerformed(ActionEvent a) {
sLength = txtLength.getText();
sWidth = txtWidth.getText();
fType = "";
Length = Double.parseDouble(sLength);
Width = Double.parseDouble(sWidth);
Area = 0;
Total = 0;
if(a.getSource().equals(btnCalculate)) {
Area = Length * Width;
if(rbtnWood.isSelected()) {
Total = Area * 20;
fType.equals("Wood");
} else if(rbtnCarpet.isSelected()) {
Total = Area * 10;
fType.equals("Carpet");
} //end if/else
sArea = String.valueOf(df.format(Area));
sTotal = String.valueOf(df.format(Total));
txtArea.setText(sArea);
txtTotal.setText(sTotal);
System.out.println("Calculate Area pressed");
} //end if
if(a.getSource().equals(btnSubmit)) {
newOrder();
System.out.println("Submit pressed");
} //end if
if(a.getSource().equals(btnOrders)) {
orderList();
System.out.println("Customer Orders pressed");
} //end if
} //end actionPerformed
public void newOrder() {
insertQuery = "INSERT into CustomerOrders(CustomerName, CustomerAddress,
FloorType, RoomSize, Total) values(?, ?, ?, ?, ?)";
try {
Connection conn = DriverManager.getConnection();
myQuery = conn.prepareStatement(insertQuery);
myQuery.setString(1, txtName.getText());
myQuery.setString(2, txtAddress.getText());
myQuery.setString(3, fType);
myQuery.setDouble(4, Area);
myQuery.setDouble(5, Total);
myQuery.execute();
JOptionPane.showMessageDialog(null, "Thank you for your order!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //end try/catch
} //end newOrder
public void orderList() {
try {
rs = myQuery.executeQuery("SELECT * from CustomerOrders");
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
while (rs.next()) {
for(int i = 1; i < columns; i++) {
txtCustomer.setText(rs.getString(i) + " ");
System.out.print(rs.getString(i) + " ");
System.out.println();
} //end for
}// end while
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //end try/catch
} //end orderList
} //end class
由於您是新手,請求而不是關閉標誌:您是否可以將代碼僅限於此問題,並且可以在代碼中添加換行符,以便我們不必向右滾動閱讀相關的行? –
請嘗試發佈[SSCCE](http://sscce.org)/ [MCVE]。因此,請花些時間,通過刪除不必要的東西來限制您的示例,因爲簡單的控制檯應該足夠了(除非您確定該問題與GUI有關)。另外,如果可以嘗試描述你正在面對的更詳細的問題。你期望發生什麼,發生了什麼(你會得到錯誤/異常/不正確的結果 - 什麼是正確的結果,爲什麼你會期望它)。 – Pshemo
好的,我編輯了GUI組件。對不起,這只是我第二次發佈到這個論壇。 – Karlnage