Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: HELP !!!!!!! URGENT !!!!! 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

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default HELP !!!!!!! URGENT !!!!! 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

    anyone can help tqtqtqtqtqtq really need help

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


  2. #2
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    help.jpg

     
    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());
                }
     
     
     
    }
        }    
     
    }
    [/QUOTE]

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    Please see the link in my signature on asking questions the smart way, then try again.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. The Following User Says Thank You to KevinWorkman For This Useful Post:

    tancy (November 27th, 2012)

  5. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    what mean in your signature ?? can u help me in this question??

  6. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    I recommend you read this: How To Ask Questions The Smart Way

    It tells you that it's rude to post caps-locked, multi-exclamation-pointed titles and to dump hundreds of lines of code with no explanation.

    Instead, post an SSCCE that demonstrates only exactly what you're talking about, and ask a specific technical question, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,952
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    The posted code does not compile without errors. Please correct the warnings and errors.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    tancy (November 27th, 2012)

  9. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    oh ya ~~ got error this code does not have error already ~~ help me plss ~~ttqtq

     
    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 Name")){
     
     
                }
     
     
     
     
     
     
        }
                 catch(Exception ex){
                    JOptionPane.showMessageDialog(null, ex.toString());
                }
     
     
     
    }
        }    
     
    }

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,952
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    Why does the code extend JApplet and have a main() method? Applets do not use the main() method.

    Why is this statement in the code two times:
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");


    You need to make a small simple version of the code for testing that does not use a SQL database.
    If your problem is with a JTable, there is no need for any database code. Fill the JTable from Strings in the program. Get rid of all the database code or at least replace it with calls to println() that prints out the SQL calls vs trying to execute them.

    The posted code still has compiler errors. Where is this defined:
    TancyPart1test_1.class
    If you don't understand my answer, don't ignore it, ask a question.

  11. #9
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    but i got a microsoft access file(stored record of product) that need retrieve data from it which the retrieved data i plan to store in JTable

    then for example i select the first row of data help.jpg W0010 tancy 1 , then i press delete it will delete from database and form the
    Frame there , so how??

    this is a assignment and i need to pass up tomorrow .... really frastrated now

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,952
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    What are you having problems with?
    Working with a database
    or working with a JTable?

    If with the JTable then you need to write a smaller, simpler program that only uses a JTable.
    Not many people will want to set up a database for your testing.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #11
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    got way to attach my database file here?? if can i attach here let u see or i can send u through email

    my problem is working with the JTable to delete the records from the database

  14. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,952
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    Make a small testing program without the database and post that.

    If your problem is with the database, I can NOT help you.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #13
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    help1.jpg this is my microsoft access database file image ~~ i need to delete the row of data when i press delete button

    <<<this is our last exercise>>>

     
    package chapter36;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import java.io.*;
    import java.util.Vector;
     
    public class ModifyTable extends JApplet {
      // Create table column names
      private String[] columnNames =
        {"Country", "Capital", "Population in Millions", "Democracy"};
     
      // Create table data
      private Object[][] rowData = {
        {"USA", "Washington DC", 280, true},
        {"Canada", "Ottawa", 32, true},
        {"United Kingdom", "London", 60, true},
        {"Germany", "Berlin", 83, true},
        {"France", "Paris", 60, true},
        {"Norway", "Oslo", 4.5, true},
        {"India", "New Delhi", 1046, true}
      };
     
      // Create a table model
      private DefaultTableModel tableModel = new DefaultTableModel(
        rowData, columnNames);
     
      // Create a table
      private JTable jTable1 = new JTable(tableModel);
     
      // Create buttons
      private JButton jbtAddRow = new JButton("Add New Row");
      private JButton jbtAddColumn = new JButton("Add New Column");
      private JButton jbtDeleteRow = new JButton("Delete Selected Row");
      private JButton jbtDeleteColumn = new JButton(
        "Delete Selected Column");
      private JButton jbtSave = new JButton("Save");
      private JButton jbtClear = new JButton("Clear");
      private JButton jbtRestore = new JButton("Restore");
     
      // Create a combo box for selection modes
      private JComboBox jcboSelectionMode =
        new JComboBox(new String[] {"SINGLE_SELECTION",
          "SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION"});
     
      // Create check boxes
      private JCheckBox jchkRowSelectionAllowed =
        new JCheckBox("RowSelectionAllowed", true);
      private JCheckBox jchkColumnSelectionAllowed =
        new JCheckBox("ColumnSelectionAllowed", false);
     
      public ModifyTable() {
        JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayout(2, 2));
        panel1.add(jbtAddRow);
        panel1.add(jbtAddColumn);
        panel1.add(jbtDeleteRow);
        panel1.add(jbtDeleteColumn);
     
        JPanel panel2 = new JPanel();
        panel2.add(jbtSave);
        panel2.add(jbtClear);
        panel2.add(jbtRestore);
     
        JPanel panel3 = new JPanel();
        panel3.setLayout(new BorderLayout(5, 0));
        panel3.add(new JLabel("Selection Mode"), BorderLayout.WEST);
        panel3.add(jcboSelectionMode, BorderLayout.CENTER);
     
        JPanel panel4 = new JPanel();
        panel4.setLayout(new FlowLayout(FlowLayout.LEFT));
        panel4.add(jchkRowSelectionAllowed);
        panel4.add(jchkColumnSelectionAllowed);
     
        JPanel panel5 = new JPanel();
        panel5.setLayout(new GridLayout(2, 1));
        panel5.add(panel3);
        panel5.add(panel4);
     
        JPanel panel6 = new JPanel();
        panel6.setLayout(new BorderLayout());
        panel6.add(panel1, BorderLayout.SOUTH);
        panel6.add(panel2, BorderLayout.CENTER);
     
        add(panel5, BorderLayout.NORTH);
        add(new JScrollPane(jTable1),
          BorderLayout.CENTER);
        add(panel6, BorderLayout.SOUTH);
     
        // Initialize table selection mode
        jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
     
        jbtAddRow.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            if (jTable1.getSelectedRow() >= 0)
              tableModel.insertRow(jTable1.getSelectedRow(),
                new java.util.Vector());
            else
              tableModel.addRow(new java.util.Vector());
          }
        });
     
        jbtAddColumn.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            String name = JOptionPane.showInputDialog("New Column Name");
            tableModel.addColumn(name, new java.util.Vector());
          }
        });
     
        jbtDeleteRow.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            if (jTable1.getSelectedRow() >= 0)
              tableModel.removeRow(jTable1.getSelectedRow());
          }
        });
     
        jbtDeleteColumn.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            if (jTable1.getSelectedColumn() >= 0) {
              TableColumnModel columnModel = jTable1.getColumnModel();
              TableColumn tableColumn =
                  columnModel.getColumn(jTable1.getSelectedColumn());
              columnModel.removeColumn(tableColumn);
            }
          }
        });
     
        jbtSave.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            try {
              ObjectOutputStream out = new ObjectOutputStream(
                new FileOutputStream("tablemodel.dat"));
              out.writeObject(tableModel.getDataVector());
              out.writeObject(getColumnNames());
              out.close();
            }
            catch (Exception ex) {
              ex.printStackTrace();
            }
          }
        });
     
        jbtClear.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            tableModel.setRowCount(0);
          }
        });
     
        jbtRestore.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            try {
              ObjectInputStream in = new ObjectInputStream(
                new FileInputStream("tablemodel.dat"));
              Vector rowData = (Vector)in.readObject();
              Vector columnNames = (Vector)in.readObject();
              tableModel.setDataVector(rowData, columnNames);
              in.close();
            }
            catch (Exception ex) {
              ex.printStackTrace();
            }
          }
        });
     
        jchkRowSelectionAllowed.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            jTable1.setRowSelectionAllowed(
              jchkRowSelectionAllowed.isSelected());
          }
        });
     
        jchkColumnSelectionAllowed.addActionListener(
          new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            jTable1.setColumnSelectionAllowed(
              jchkColumnSelectionAllowed.isSelected());
          }
        });
     
        jcboSelectionMode.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            String selectedItem =
              (String) jcboSelectionMode.getSelectedItem();
     
            if (selectedItem.equals("SINGLE_SELECTION"))
              jTable1.setSelectionMode(
                ListSelectionModel.SINGLE_SELECTION);
            else if (selectedItem.equals("SINGLE_INTERVAL_SELECTION"))
              jTable1.setSelectionMode(
                ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            else if (selectedItem.equals("MULTIPLE_INTERVAL_SELECTION"))
              jTable1.setSelectionMode(
                ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
          }
        });
      }
     
      private Vector getColumnNames() {
        Vector<String> columnNames = new Vector<String>();
     
        for (int i = 0; i < jTable1.getColumnCount(); i++)
          columnNames.add(jTable1.getColumnName(i));
     
        return columnNames;
      }
     
      //Main method
      public static void main(String[] args) {
        ModifyTable applet = new ModifyTable();
        JFrame frame = new JFrame();
        //EXIT_ON_CLOSE == 3
        frame.setDefaultCloseOperation(3);
        frame.setTitle("ModifyTable");
        frame.getContentPane().add(applet, java.awt.BorderLayout.CENTER);
        applet.init();
        applet.start();
        frame.setSize(400,320);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    }

  16. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,952
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    Can you explain what the problem with the posted code is?
    When I select a row, the row is hi-lighted.
    When I press the Delete button, the selected row is deleted.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #15
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    yaya that i wanted in my assignment~~ the posted code no have any problem ~~ the problem now is this code

    yaya(When I select a row, the row is hi-lighted.When I press the Delete button, the selected row is deleted.) >>> that what i wanna to perfrom in the below code .... but the when the row data is deleted , the same data in database is also need to be deleted

    get what i mean ??

    hey bro really so tq u r the one who listen me my problem really tqtq

    but i really need someone help me now

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


    --- Update ---

    norm u have email address ?? i send my microsoft access file to u ~~ u help me link see can ?? tqttqq

  18. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,952
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    I don't have databases on my PC

    If you need help with databases, there is a section of the forum for databases:
    http://www.javaprogrammingforums.com/jdbc-databases/
    If you don't understand my answer, don't ignore it, ask a question.

  19. #17
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    nvm u have some way that can lead me out of this problem??

  20. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,952
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    If you need help with databases, there is a section of the forum for databases:
    JDBC & Databases
    If you don't understand my answer, don't ignore it, ask a question.

  21. #19
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    okok~~ thanks you thanks you norm are there anyone u know still can help me now?? the deadline of this assignment is 4 hour from now ~~ if delayed pass up one day will minus 5 mark

  22. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,952
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    Try asking the database questions in that section of the forum.
    See post#18
    If you don't understand my answer, don't ignore it, ask a question.

  23. #21
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    This thread has been cross posted here:

    http://www.java-forums.org/awt-swing/65584-help-urgent-how-select-specific-row-jtable-delete-wi.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  24. #22
    Member vigneshwaran's Avatar
    Join Date
    Nov 2012
    Location
    Chennai, TamilNadu
    Posts
    35
    My Mood
    Cheerful
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: HELP !!!!!!! URGENT !!!!! 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

    To get value from JTable,

     
    jtable.getValueAt(int column, int row)

Similar Threads

  1. Replies: 0
    Last Post: December 25th, 2011, 01:59 PM
  2. Replies: 1
    Last Post: October 31st, 2011, 01:22 PM
  3. Replies: 1
    Last Post: September 9th, 2011, 03:30 PM
  4. Problem With Applet Connecting With Microsoft Access Database
    By Saurabh Mittal in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 29th, 2011, 11:43 AM
  5. Printing JTable that retrieve data from the Database
    By hundu in forum AWT / Java Swing
    Replies: 3
    Last Post: June 28th, 2009, 02:50 PM