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

查看:147
本文介绍了在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,我在互联网上找到它,它不是我的,它的工作。但是现在我不知道如何向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。

请查看表格从数据库示例数据库表。此示例使用的Vector将根据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. 你可以使用 addRow(.. 。) DefaultTableModel 的方法,动态添加数据。阅读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天全站免登陆