Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2010
    Posts
    38
    Rep Power
    0

    Thumbs up Add New Row Then Insert a data into JTable

    Hello Friends.

    I m create JTable & give some code to insert a row with ADD ROW BUTTON.
    Then I Create a SUBMIT BUTTON to insert a data into access database.

    This is my code..

    import javax.swing.*;
    import javax.swing.table.*;
    import java.sql.*;
    import java.awt.*;
    import java.awt.event.*;

    public class InsertJTableDatabase implements ActionListener{
    JTable table;
    JButton button, button1;
    int i = 0;
    DefaultTableModel model;

    private PreparedStatement pstm = null;
    private ResultSet rs=null;
    private Connection connect = null;

    public static void main(String[] args){
    new InsertJTableDatabase();
    }


    public InsertJTableDatabase()
    {
    JFrame frame = new JFrame("Getting Cell Values in JTable");
    JPanel panel = new JPanel();

    String data[][] = {{"JSP NAME ","SERVLET"},{"Struts","Spring"}};

    String col[] = {"Name","Address"};
    model = new DefaultTableModel(data, col);
    table = new JTable(model);

    JScrollPane pane = new JScrollPane(table);

    button1 =new JButton("Add Row");
    button1.addActionListener(this);

    button=new JButton("Submit");
    button.addActionListener(this);

    panel.add(pane);
    panel.add(button);
    panel.add(button1);

    frame.add(panel);
    frame.setSize(500,250);
    frame.setUndecorated(true);
    frame.getRootPane().setWindowDecorationStyle(JRoot Pane.PLAIN_DIALOG);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    }
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==button1)
    {
    i=i+1;
    model.insertRow(i,new Object[]{"",""});
    }else if(e.getSource()==button)
    {
    int index=1;
    int count=table.getRowCount();

    try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    connect =DriverManager.getConnection("jdbc:odbc:access");

    for(int i=0;i<count;i++)
    {
    Object obj1 = GetData(table, i, 0);
    Object obj2 = GetData(table, i, 1);
    String value1=obj1.toString();
    String value2=obj2.toString();

    System.out.println(value1);
    System.out.println(value2);

    pstm=connect.prepareStatement("insert into data values(?,?)");
    pstm.setString(1,value1);
    pstm.setString(2,value2);

    index++;
    }
    pstm.executeUpdate();
    }
    catch(Exception ex){
    }
    }
    }
    public Object GetData(JTable table, int row_index, int col_index)
    {
    return table.getModel().getValueAt(row_index, col_index);
    }
    }

    __________________________________________________ ___________

    But When ever first i add a row and then insert a data and click on submit.
    The data is not submited into database file.. there is no entry into database
    through JTable new Row..

    Add New Row Then Insert a data into JTable-jtable.jpg

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Add New Row Then Insert a data into JTable

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Add New Row Then Insert a data into JTable

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. how to insert data into jTable in netbeans
    By suhailt in forum NetBeans
    Replies: 13
    Last Post: 03-26-2014, 08:09 PM
  2. How to insert a row into JTable when filters are used
    By prasad.vara in forum AWT / Swing
    Replies: 0
    Last Post: 10-18-2010, 07:25 AM
  3. Replies: 3
    Last Post: 08-07-2010, 09:06 PM
  4. insert data from text field to jtable
    By mackinas in forum New To Java
    Replies: 2
    Last Post: 06-09-2010, 05:30 AM
  5. Replies: 3
    Last Post: 02-28-2009, 10:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •