问题遇到的现象和发生背景
刚学到数据库编程,实验任务要求编程实现在GUI中展现数据库中某表的信息,并实现对应的增删查改,相关的代码我以码好,数据库中表也有反馈,但是在JTbale中显示的信息我却不知该如何更新,看了很多帖子也都云里雾里
问题相关代码,请勿粘贴截图
//demand_Second是一个按钮,这个监听的功能是更改数据库中表某行,某列的信息
demand_Second.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//DML是我写的一个用于数据库DML操作的类update是其中的更改数据的方法
DML dml = new DML();
dml.updateInfo(9,16);
jTable.repaint();
}
});
/**
* DQL操作 --->数据库的查询
*/
class DQL {
/**
* 查询表中信息
*/
//因为要向JTable中添加数据,所以返回了一个二维数组
public Object[][] selectInfo() {
Object data[][] = new Object[0][];
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//JDBCUtils是我自己写的工具类,具体就是链接数据库和一些关闭资源的方法之类的
connection = JDBCUtils.getConnection();
preparedStatement = connection.prepareStatement("select * from info_medal",ResultSet.TYPE_SCROLL_INSENSITIVE);
resultSet = preparedStatement.executeQuery();
resultSet.last();
int n = resultSet.getRow();
data = new Object[n][5];
int i = 0;
resultSet.beforeFirst();
while (resultSet.next()) {
data[i][0] = resultSet.getString(1);
data[i][1] = resultSet.getString(2);
data[i][2] = resultSet.getString(3);
data[i][3] = resultSet.getString(4);
data[i][4] = resultSet.getString(5);
i++;
}
}catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.closeResource(resultSet,preparedStatement,connection);
}
return data;
}
}
运行结果及报错内容
总体表现为数据库中的表的信息已经成功更改,但是程序运行出来的JTable却买有任何变化
我想要达到的结果
想做到这个更改做完之后,JTable就自动刷新,对应上更改的数据
如果你有兴趣看的话,这是整体的代码
public class TheThirdLab {
static {
DML dml = new DML();
dml.insertInfo("挪威",16,8,13,37);
dml.insertInfo("中国",8,4,3,15);
dml.insertInfo("美国",8,10,7,25);
dml.insertInfo("冰岛",0,0,0,0);
dml.insertInfo("荷兰",8,5,4,17);
dml.insertInfo("印度",0,0,0,0);
}
JPanel jPanel = new JPanel(new GridLayout(2,3));
JFrame jFrame = new JFrame("冬奥会奖牌信息查询系统");
Object[] title = {"代表队","金牌","银牌","铜牌","合计"};
Object data[][];
JTable jTable;
public void init() {
JButton insert = new JButton("查询奖牌信息");
//查询表格信息
DQL dql = new DQL();
data = dql.selectInfo();
DefaultTableModel model = new DefaultTableModel(data,title);
jTable = new JTable(model);
//添加按钮对象
JButton demand_First = new JButton("查询奖牌信息");
JButton demand_Second = new JButton("中国再次获得金牌");
JButton demand_Third = new JButton("加入德国");
JButton demand_Forth = new JButton("查询金牌数为9的国家");
JButton demand_Fifth = new JButton("查询冰岛");
JButton demand_Sixth = new JButton("删除无奖牌的国家");
//为按钮添加监听
/**
* 查询奖牌信息(暂时不会写)
*/
/**
* 中国队再获一枚金牌
*/
demand_Second.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DML dml = new DML();
dml.updateInfo(9,16);
jTable.repaint();
}
});
/**
* 添加德国队
*/
demand_Third.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DML dml = new DML();
dml.insertInfo("德国",12,10,5,27);
jTable.repaint();
}
});
//将按钮添加到JPanel中
jPanel.add(demand_First);
jPanel.add(demand_Second);
jPanel.add(demand_Third);
jPanel.add(demand_Forth);
jPanel.add(demand_Fifth);
jPanel.add(demand_Sixth);
//组装JFrame界面
jFrame.add(new JScrollPane(jTable));
jFrame.add(jPanel, BorderLayout.SOUTH);
jFrame.pack();
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TheThirdLab().init();
}
}
/**
* DML操作-->数据库的增删改
*/
class DML {
/**
* 向表中添加信息
*/
public void insertInfo(String delegate,int gold,int silver,int copper,int total) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JDBCUtils.getConnection();
preparedStatement = connection.prepareStatement("insert into info_medal values (?,?,?,?,?)");
preparedStatement.setString(1,delegate);
preparedStatement.setInt(2,gold);
preparedStatement.setInt(3,silver);
preparedStatement.setInt(4,copper);
preparedStatement.setInt(5,total);
int i = preparedStatement.executeUpdate();
}catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.closeResource(preparedStatement,connection);
}
}
/**
* 更改表格中的数据
*/
public void updateInfo(int gold,int total) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JDBCUtils.getConnection();
preparedStatement = connection.prepareStatement("update info_medal set 金牌 = ?,合计 = ? where 代表队 = '中国'");
preparedStatement.setInt(1,gold);
preparedStatement.setInt(2,total);
int i = preparedStatement.executeUpdate();
}catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.closeResource(preparedStatement,connection);
}
}
}
/**
* DQL操作 --->数据库的查询
*/
class DQL {
/**
* 查询表中信息
*/
public Object[][] selectInfo() {
Object data[][] = new Object[0][];
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCUtils.getConnection();
preparedStatement = connection.prepareStatement("select * from info_medal",ResultSet.TYPE_SCROLL_INSENSITIVE);
resultSet = preparedStatement.executeQuery();
resultSet.last();
int n = resultSet.getRow();
data = new Object[n][5];
int i = 0;
resultSet.beforeFirst();
while (resultSet.next()) {
data[i][0] = resultSet.getString(1);
data[i][1] = resultSet.getString(2);
data[i][2] = resultSet.getString(3);
data[i][3] = resultSet.getString(4);
data[i][4] = resultSet.getString(5);
i++;
}
}catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.closeResource(resultSet,preparedStatement,connection);
}
return data;
}
}