Databases 10, Unpaid Order Sum Write an application that connects to the CoffeeD
ID: 3548932 • Letter: D
Question
Databases
10, Unpaid Order Sum
Write an application that connects to the CoffeeDB database, then calculates and displays the total amount owed in unpaid orders. This will be the sum of each row's Cost column.
12, Unpaid Order Lookup
Write an application that connects to the CoffeeDB databases and displays a JList component. The JList component should display a list of customers with unpaid orders. When the user clicks on a customer, the application should a summery of all the unpaid orders for that customer.
Pls add comments
Explanation / Answer
package student.database;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Vector;
import javax.swing.table.TableColumn;
public class StudentDatabase extends JApplet implements ActionListener {
JFrame frame = new JFrame();
JPanel page1 = new JPanel();
JButton store = new JButton("STORE");
JButton get = new JButton("RETRIEVE");
JButton back = new JButton("BACK");
JTextField roll = new JTextField("ROLL NO");
JTextField name = new JTextField("NAME");
JTextField age = new JTextField("AGE");
JButton submit = new JButton("SUBMIT");
JButton exit = new JButton("EXIT");
Container cp = getContentPane();
JPanel storePanel = new JPanel();
JFrame storeFrame = new JFrame("Store");
JFrame getFrame = new JFrame("RETRIEVE");
JPanel getPanel = new JPanel();
JTable table;
public void init() {
page1.setLayout(new FlowLayout());
store.setBounds(10, 50, 20, 40);
store.setBorder(BorderFactory.createBevelBorder(5, Color.lightGray, Color.yellow));
get.setBorder(BorderFactory.createBevelBorder(5, Color.lightGray, Color.yellow));
exit.setBorder(BorderFactory.createMatteBorder(2, 1, 3, 0, Color.cyan));
page1.add(store);
page1.add(get);
page1.add(exit);
store.addActionListener(this);
get.addActionListener(this);
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
frame.dispose();
}
}
);
// back.addActionListener(this);
frame.add(page1);
frame.setVisible(true);
frame.setSize(640, 480);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource().equals(store)) {
//frame.remove(page1);
setVisible(false);
//frame.dispose();
storing();
} else if (ae.getSource().equals(get)) {
setVisible(false);
retrieving();
}
}
private void retrieving() {
Vector colhead = new Vector();
String ro = new String();
String na = new String();
String ag = new String();
Vector data = new Vector();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
try (Connection con = DriverManager.getConnection("jdbc:odbc:student")) {
Statement stmt = con.createStatement();
String r = roll.getText();
String n = name.getText();
String a = age.getText();
String ss = "select * from Student";
ResultSet rs = stmt.executeQuery(ss);
ResultSetMetaData md = rs.getMetaData();
int cols = md.getColumnCount();
for (int i = 1; i <= cols; i++) {
colhead.addElement(md.getColumnName(i));
}
while (rs.next()) {
Vector tmp = new Vector(cols);
for (int i = 1; i <= cols; i++) {
tmp.addElement(rs.getObject(i));
}
data.addElement(tmp);
}
}
} catch (SQLException se) {
System.err.println("ERROR : " + se);
} catch (ClassNotFoundException c) {
System.err.println("ERROR : " + c);
}
table = new JTable(data, colhead);
TableColumn col;
for (int i = 0; i < table.getColumnCount(); i++) {
col = table.getColumnModel().getColumn(i);
col.setMaxWidth(250);
}
getPanel.add(table);
getPanel.add(back);
back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
frame.setVisible(true);
storeFrame.setVisible(false);
storeFrame.dispose();
}
});
getFrame.add(getPanel);
getFrame.setVisible(true);
getFrame.setSize(640, 480);
}
private void storing() {
storePanel.setLayout(new FlowLayout());
submit.setBorder(BorderFactory.createBevelBorder(5, Color.lightGray, Color.yellow));
back.setBorder(BorderFactory.createBevelBorder(5, Color.lightGray, Color.yellow));
storePanel.add(roll);
storePanel.add(name);
storePanel.add(age);
submit.setSize(25, 30);
storePanel.add(submit);
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
try (Connection con = DriverManager.getConnection("jdbc:odbc:student")) {
Statement stmt = con.createStatement();
String r = roll.getText();
String n = name.getText();
String a = age.getText();
String ss = "insert into Student values('" + roll.getText() + "','" + n + "','" + a + "')";
//ResultSet rs=
stmt.executeQuery(ss);
}
} catch (SQLException se) {
System.err.println("ERROR : " + se);
} catch (ClassNotFoundException c) {
System.err.println("ERROR : " + c);
}
}
});
storePanel.add(back);
back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
frame.setVisible(true);
storeFrame.dispose();
getFrame.dispose();
}
});
storeFrame.add(storePanel);
storeFrame.setVisible(true);
storeFrame.setSize(640, 480);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.