I am fairly new to Java and I'm not very familiar with sql either. I am working on a program that takes flooring orders and stores them in an SQL database. The names and addresses will store just fine but the rest of the information won't. The information retrieval isn't working either. I'm not certain what I am doing wrong either. I removed the information from the Connection object for obvious reasons, but I can assure you that this has the correct information in my program.. Any help would be appreciated.
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
Aucun commentaire:
Enregistrer un commentaire