我已经为表的视图部分编写了代码
private JTable table;
public String setTableData(Vector data){
Vector<String> column = new Vector<String>();
column.addElement("SN");
column.addElement("Name");
column.addElement("Last Name");
column.addElement("Mobile No");
column.addElement("Email");
DefaultTableModel model = new DefaultTableModel(data,column);
table.setModel(model);
return null;
}
然后我写了如下模型部分:
public Vector retriveUser() {
Connection connection;
ResultSet resultSet;
Statement statement = null;
Vector data = new Vector();
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/contact_info", "root", "");
System.out.println("Connection established");
statement = connection.createStatement();
resultSet = statement.executeQuery(retriveUser);
ResultSetMetaData metaData = resultSet.getMetaData();
int columns = metaData.getColumnCount();
while (resultSet.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(resultSet.getObject(i));
}
data.addElement(row);
}
} catch (SQLException e) {
System.out.println("Exception occur");
e.printStackTrace();
}
}
然后我写了控制器
public class ActionBtnListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String firstName =userView.getfirstName();
String lastName = userView.getlastName();
String mobNo = userView.getmobNo();
String emailAdd = userView.getemailAdd();
userModel.insertUser(firstName,lastName,mobNo,emailAdd);
userModel.retriveUser();
userView.setTableData(null);
}
}
因此,在运行时我没有在表单中看到任何表格。只显示框架。我想将数据库数据显示到视图表中。并且在单击提交按钮时刷新表数据。请解决我的问题。