0
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 ...)。任何人都可以幫助我,因爲我剛剛學到了東西。謝謝 !添加一行到JTable和數據庫(phpMyAdmin)?
非常感謝您! –