0

I am trying to retrieve data from a database into jTable. I use NetBeans 8.1. I cannot make it work.

I used this code to insert:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    String name = jTextField1.getText();
    String father = jTextField2.getText();
    String mother = jTextField3.getText();
    String line1 = jTextField4.getText();
    String line2 = jTextField5.getText();
    String line3 = jTextField11.getText();
    String roll = jTextField6.getText();
    String school = jTextField7.getText();
    String hobby1 = jTextField8.getText();
    String hobby2=jTextField9.getText();
    String hobby3 = jTextField10.getText();
    String address = line1 + " " + line2 + " " + line3;
    //String hobby = hobby1 + " " + hobby2 + " " + hobby3;
    java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("dd-MM-yyyy");
    String date = fmt.format(jDateChooser1.getDate());
    try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(
                   "jdbc:mysql://127.0.0.1:3306/pritam", "root", "root");
        PreparedStatement ps = con.prepareStatement(
                   "insert into basic_info values(?,?,?,?,?,?,?)");
        ps.setString(1, name);
        ps.setString(2, father);
        ps.setString(3, mother);
        ps.setString(4, roll);
        ps.setString(5, date);
        ps.setString(6 ,address);
        // ps.setString(7, hobby);
        ps.setString(7, school);
        ps.executeUpdate();
        con.close();
    }catch(Exception E) {
        System.out.println(E);
    }

How can I retrieve data in jTable?

2

1 Answer 1

1

You need to implement an AbstractTableModel as:(Am just giving it an example here)

NameTableModel.java

public class NameTableModel extends AbstractTableModel {

    String[] columnNames = {"Id", "Name","Father", "Mother","Hobby" };
    Object[][] data;

    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
    }        
}

NameStoreUI.java

public class NameStoreUI extends javax.swing.JPanel {

    public NameStoreUI() {
        initComponents();
        loadData();
    }
    void loadData()
    {

        NameTableModel model = new NameTableModel();
        NameDAO m = new NameDAO();
        Object[][] data_x = m.list();
        model.data = data_x;
        tableData.setModel(model);
    }

NameDAO.java

public Object[][] list()
    {
        try
        {
            s = con.createStatement();
            rs = s.executeQuery("SELECT * FROM name_table");

            ArrayList mylist = new ArrayList();

            int column_count = rs.getMetaData().getColumnCount();

            while (rs.next()) 
            {
                String[] s = {rs.getString("id"),rs.getString("name"),rs.getString("father"),rs.getString("mother"),rs.getString("hobby")};
                mylist.add(s);
            }

            Object[][] o = new Object[mylist.size()][column_count];

            int i, j;
            for(i = 0 ; i < mylist.size() ; i++)
            {
                o[i] = (String[])mylist.get(i);
            }

            return o;

        }
        catch(SQLException s)
        {
            System.out.println("Error in list()");
        }
        return null;
    }

I hope you got the concept.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.