[英]Add a row to JTable and Database (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);
}
I show my Database to JTable by this way, I found it on Internet, it's not mine and it's work. 我通过这种方式将数据库显示给JTable,我在Internet上找到它,这不是我的,并且可以正常工作。 But now I don't know how to add row to JTable and Database, I've found many website but no use (PreparedStatement, executeUpdate...).
但是现在我不知道如何向JTable和数据库添加行,我发现了很多网站,但没有用(PreparedStatement,executeUpdate ...)。 Can anyone help me about this because I've just learnt.
有人可以帮助我,因为我刚刚学过。 Thank You !
谢谢 !
That is a poor example to use: 那是一个不好的例子:
Instead check out the Table From Database Example
code found in Table From Database . 相反,检查出的
Table From Database Example
中发现的代码表从数据库 。 This example uses Vectors which will grow depending on the number of rows found in the ResultSet. 本示例使用的Vector将根据ResultSet中发现的行数而增长。
I don't know how to add row to JTable and Database
我不知道如何向JTable和数据库添加行
You can use the addRow(...)
method of the DefaultTableModel
to add data dynamically. 您可以使用
DefaultTableModel
的addRow(...)
方法动态添加数据。 Read the API or search the forum/web for examples that use the addRow(...) method. 阅读API或在论坛/网络上搜索使用addRow(...)方法的示例。
For database inserts you can start with the tutorial on JDBC Database Access . 对于数据库插入,您可以从JDBC Database Access教程开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.