This discussion is archived
3 Replies Latest reply: Nov 28, 2012 12:35 AM by gimbal2 RSS

need help in source code(java) please really need help :(

976547 Newbie
Currently Being Moderated
how to select on a specific row in JTable and delete it (with also changes in database microsoft access) , the JTable i create already retrieve the data from microsoft access file~~



<<<code>>>

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");




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());



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 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 Code")){
Statement stmtDelete = conn.createStatement();
stmtDelete.executeUpdate("DELETE FROM Product WHERE productCode ='"+jtfSearch.getText()+"' ;");


}






}
catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.toString());
}



}
}

}

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points