import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.*;
import java.util.Vector;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TancyPart1test_11 extends JApplet implements ActionListener {
JPanel allPanel = new JPanel();
JPanel search = new JPanel();
JPanel result = new JPanel();
JPanel result1 = new JPanel();
JPanel Panel = new JPanel();
JPanel delete = new JPanel();
JComboBox jlstFunction = new JComboBox(new String[]{"Product Code","Product Name"});
JTextField jtfSearch = new JTextField("",20);
JButton jbtSearch = new JButton("Search");
JButton jbtDelete = new JButton("Delete");
/*private String[] columnNames = {" Code ", "ProductName", "Quantity", "Description"," Price(RM) "," Cost(RM) "};
private Object[][] rowData = {{"","","","","",""},{"","","","","",""},{"","","","","",""},
{"","","","","",""},{"","","","","",""},{"","","","","",""},
{"","","","","",""},{"","","","","",""},{"","","","","",""},
{"","","","","",""},{"","","","","",""}};
private JTable jTable1 = new JTable(rowData,columnNames); */
Connection conn;
ResultSet rs;
public TancyPart1test_11(){
allPanel.setLayout(new BorderLayout());
search.setLayout(new FlowLayout(FlowLayout.LEFT));
search.add(jlstFunction);
search.add(jtfSearch);
search.add(jbtSearch);
search.add(jbtDelete);
result.setLayout(new BorderLayout());
/*result.add(new JScrollPane(jTable1),BorderLayout.CENTER);
TableColumn column1 = jTable1.getColumnModel().getColumn(0);
column1.setPreferredWidth(20);
jTable1.setRowHeight(20); */
/*DefaultTableModel tableModel = new DefaultTableModel();
JTable jTable1 = new JTable(tableModel);*/
Vector columnNames = new Vector();
Vector data = new Vector();
JPanel p=new JPanel();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:product");
String sql = "Select * from product";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery( sql );
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement( md.getColumnName(i) );
}
while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++){
row.addElement( rs.getObject(i) );
}
data.addElement( row );
}
rs.close();
stmt.close();
}
catch(Exception e){
System.out.println(e);
}
JTable jTable1 = new JTable(data,columnNames);
DefaultTableModel tableModel = new DefaultTableModel();
TableColumn col;
for (int i = 0; i < jTable1.getColumnCount(); i++) {
col = jTable1.getColumnModel().getColumn(i);
col.setMaxWidth(250);
}
result.add(new JScrollPane(jTable1),BorderLayout.CENTER);
Panel.setLayout(new BorderLayout());
Panel.add(search,BorderLayout.NORTH);
Panel.add(result,BorderLayout.CENTER);
allPanel.add(Panel,BorderLayout.NORTH);
add(allPanel);
DBConnect();
jbtSearch.addActionListener(this);
jbtDelete.addActionListener(this);
}
public static void main(String []args ){
TancyPart1test_11 applet = new TancyPart1test_11();
JFrame frame = new JFrame();
frame.setSize(900,500);
frame.setTitle("Delete Part");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
applet.init();
applet.start();
frame.getContentPane().add(applet);
}
/*private Vector getColumnNames() {
Vector<String> columnNames = new Vector<String>();
for (int i = 0; i < jTable1.getColumnCount(); i++)
columnNames.add(jTable1.getColumnName(i));
return columnNames;
}*/
private void DBConnect() {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:Product");
}catch( ClassNotFoundException | SQLException ex){
JOptionPane.showMessageDialog(null, ex.toString());
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jbtSearch){
String function = (String) jlstFunction.getSelectedItem();
String code = jtfSearch.getText();
if("".equals(code)){
JOptionPane.showMessageDialog(null, "Code must within 4 digit and at least one...","error",JOptionPane.INFORMATION_MESSAGE);
}
else if(function.equalsIgnoreCase("Product Code")){
try {
Statement stmtDelete;
stmtDelete = conn.createStatement();
stmtDelete.executeUpdate("DELETE FROM Product WHERE productCode ='"+jtfSearch.getText()+"' ;");
} catch (SQLException ex) {
Logger.getLogger(TancyPart1test_1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
else if(e.getSource()==jbtDelete){
try{
String function1 = (String) jlstFunction.getSelectedItem();
String coding = jtfSearch.getText();
if("".equals(coding)){
JOptionPane.showMessageDialog(null, "No Record Deleted...","ERROR",JOptionPane.INFORMATION_MESSAGE);
}
else if(function1.equalsIgnoreCase("Product Name")){
/*if (jTable1.getSelectedRow() >= 0)
tableModel.removeRow(jTable1.getSelectedRow()); */
}
}
catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.toString());
}
}
}
}