向 JTable 和数据库 (phpMyAdmin) 添加一行? [英] Add a row to JTable and Database (phpMyAdmin)?

查看:14
本文介绍了向 JTable 和数据库 (phpMyAdmin) 添加一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

initComponents(); 
try {
        ResultSet res = statement.executeQuery("SELECT * FROM banh");
        ResultSetMetaData RSMD = res.getMetaData();
        NumberOfColumns = RSMD.getColumnCount();
        AttributeNames = new String[NumberOfColumns];
        for(int i=0;i<NumberOfColumns;i++)
            AttributeNames[i]=RSMD.getColumnName(i+1);
        MyArray=new Object[10000][NumberOfColumns];
        int R=0;
        while(res.next()) {
            for(int C=1; C<=NumberOfColumns;C++)
                MyArray[R][C-1]=res.getObject(C);
            R++;
        }
        res.close();
        NumberOfRows=R;
        Object[][] TempArray=MyArray;
        MyArray=new Object[NumberOfRows][NumberOfColumns];
        for(R=0;R<NumberOfRows;R++)
            for(int C=0;C<NumberOfColumns;C++)
                MyArray[R][C]=TempArray[R][C];
        TableData.setModel(new MyTableModel());
        TableData.setVisible(true);
    }      
    catch(Exception e) 
    {
        e.printStackTrace();
    }
public void initComponents() 
{             
    model = new DefaultTableModel (new Object [][] 
        {
            {null},
            {null},
            {null},
            {null}
        },
        new String [] {""}
        ) {
          Class[] types = new Class [] {java.lang.Object.class};
          boolean[]canEdit=new boolean[]{false};

          public Class getColumnClass(int columnIndex) 
          {
                return types [columnIndex];
          }
          public boolean isCellEditable(int rowIndex, int columnIndex) 
          {
                return canEdit [columnIndex];
          }
    };
    TableData.setModel(model);
    JScrollPane ScrollPane1 = new JScrollPane(TableData); 
    ScrollPane1.setBounds(30,170,950,290);
    Frame.add(ScrollPane1,BorderLayout.CENTER);
}

我通过这种方式向 JTable 展示了我的数据库,我在 Internet 上找到了它,它不是我的,它可以工作.但是现在我不知道如何向 JTable 和数据库添加行,我发现很多网站都没有用(PreparedStatement、executeUpdate...).任何人都可以帮助我解决这个问题,因为我刚刚学会了.谢谢!

I show my Database to JTable by this way, I found it on Internet, it's not mine and it's work. But now I don't know how to add row to JTable and Database, I've found many website but no use (PreparedStatement, executeUpdate...). Can anyone help me about this because I've just learnt. Thank You !

推荐答案

这是一个不好用的例子:

That is a poor example to use:

  1. 变量名不应以大写字符开头.
  2. 硬编码数组大小以支持 10,000 行是错误的方法.您还可以使用动态的 Vector.

改为查看 Table From Database Example 代码="nofollow">数据库表.此示例使用 Vectors,它会根据在 ResultSet 中找到的行数而增长.

Instead check out the Table From Database Example code found in Table From Database. This example uses Vectors which will grow depending on the number of rows found in the ResultSet.

我不知道如何在 JTable 和数据库中添加行

I don't know how to add row to JTable and Database

  1. 您可以使用DefaultTableModeladdRow(...) 方法动态添加数据.阅读 API 或在论坛/网络中搜索使用 addRow(...) 方法的示例.

  1. You can use the addRow(...) method of the DefaultTableModel to add data dynamically. Read the API or search the forum/web for examples that use the addRow(...) method.

对于数据库插入,您可以从关于 JDBC 数据库访问的教程开始.

For database inserts you can start with the tutorial on JDBC Database Access.

这篇关于向 JTable 和数据库 (phpMyAdmin) 添加一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆