public class GetData
{
Object[][] MyArray=null;
String [] AttributeNames;
Connection connect;
Statement statement;
String state;
int NumberOfRows,NumberOfColumns;
JTable TableData = new JTable ();
DefaultTableModel model = (DefaultTableModel) TableData.getModel();
public void GetData(JFrame Frame)
{
connection();
initComponents(Frame);
Data();
TableWidth();
TableData.setVisible(true);
}
public void TableWidth ()
{
TableColumn column = null;
for (int i = 0; i < NumberOfColumns; i++)
{
column = TableData.getColumnModel().getColumn(i);
if (i == 7) {
column.setPreferredWidth(350);
}
if (i == 0) {
column.setPreferredWidth(25);
}
if (i == 1) {
column.setPreferredWidth(50);
}
if (i == 3) {
column.setPreferredWidth(30);
}
if (i == 4) {
column.setPreferredWidth(35);
}
if (i == 5) {
column.setPreferredWidth(50);
}
}
}
public void Data ()
{
try {
ResultSet res = statement.executeQuery("SELECT * FROM nhanvien");
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[500][NumberOfColumns];
int R=0;
while(res.next()) {
for(int C=1; C<=NumberOfColumns;C++)
MyArray[R][C-1]=res.getObject(C);
R++;
}
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) {}
}
public void initComponents(JFrame f)
{
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);
f.add(ScrollPane1,BorderLayout.CENTER);
TableData.setVisible(true);
}
public class MyTableModel extends AbstractTableModel
{
public int getColumnCount()
{
return (NumberOfColumns);
}
public int getRowCount()
{
return(NumberOfRows);
}
public String getColumnName(int i)
{
return (AttributeNames[i]);
}
public java.lang.Object getValueAt(int row, int column)
{
return(MyArray[row][column]);
}
}
public void connection ()
{
try {
Class.forName("org.gjt.mm.mysql.Driver");
String url = "jdbc:mysql://localhost:3306/jdbc? useUnicode=true&characterEncoding=UTF-8";
connect = DriverManager.getConnection(url,"root","");
statement = connect.createStatement();
}
catch(Exception e) {}
}
public static void main (String[] args)
{
GetData GD = new GetData ();
}
}
这是我将数据库显示为JTable的代码
public void Add (JFrame F) throws SQLException
{
String MNV = TX1.getText();
String HNV = TX2.getText();
String TNV = TX3.getText();
String GT = TX4.getText();
String NS = TX5.getText();
String TD = TX6.getText();
String SDT = TX7.getText();
String DC = TX8.getText();
AD.Add("INSERT INTO `nhanvien` "
+ "(`MÃ NV`, `HỌ NV`, `TÊN NV`, `GIỚI TÍNH`, `NĂM SINH`, `TRÌNH ĐỘ`, `SỐ ĐT`, `ĐỊA CHỈ`)"
+ " VALUES ('"+MNV+"', '"+HNV+"', '"+TNV+"', '"+GT+"', '"+NS+"', '"+TD+"', '"+SDT+"', '"+DC+"');");
GD.GetData(F);
}
这是我向Jtable和Database插入行的代码。 我的问题是有时我正常添加一行但有时当我点击按钮添加Jtable消失但值仍然添加到数据库和talbe,我必须关闭框架并再次打开所以我可以看到新的行。所以任何人都可以知道为什么会发生这种情况,并且无论如何都要向Jtable显示数据库(简单方法)。谢谢!