使用netbeans从jtable中的mysql中检索数据

时间:2016-08-17 21:10:00

标签: java mysql netbeans jtable

我正在尝试将数据库中的数据检索到jTable中。我使用NetBeans 8.1。我无法使它发挥作用。

我使用此代码插入:

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

如何在jTable中检索数据?

1 个答案:

答案 0 :(得分:0)

您需要将AbstractTableModel实现为:(我只是在这里给出一个例子)

<强> 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;
    }

我希望你有这个概念。