日期:2014-05-20  浏览次数:20777 次

一个数据库的增\删\改的问题
为什么,在GUI做增删改后.操作成功..但是在GUI中看不到我所做的操作.只有把程序关闭后,,在重新运行一下程序..这时增删改的内容才会被看到..如何能在GUI上操作完后.就马上可以在GUI上显示啊...
请各位朋友多多帮助.....

------解决方案--------------------
因为你没有更新 repaint()下试试~~你在哪个组件上?JTABLE?
贴个JTABLE操作数据库的 你参考下
/**
@version 1.1 2004-08-22
@author Cay Horstmann
*/

import com.sun.rowset.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.sql.rowset.*;

/**
This program shows how to display the result of a
database query in a table.
*/
public class ResultSetTable
{
public static void main(String[] args)
{
JFrame frame = new ResultSetFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

/**
This frame contains a combo box to select a database table
and a table to show the data stored in the table
*/
class ResultSetFrame extends JFrame
{
public ResultSetFrame()
{
setTitle( "ResultSet ");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

/* find all tables in the database and add them to
a combo box
*/

tableNames = new JComboBox();
tableNames.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
if (scrollPane != null) remove(scrollPane);
String tableName = (String) tableNames.getSelectedItem();
if (rs != null) rs.close();
String query = "SELECT * FROM " + tableName;
rs = stat.executeQuery(query);
if (scrolling)
model = new ResultSetTableModel(rs);
else
{
CachedRowSet crs = new CachedRowSetImpl();
crs.populate(rs);
model = new ResultSetTableModel(crs);
}

JTable table = new JTable(model);
scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER);
validate();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
});
JPanel p = new JPanel();
p.add(tableNames);
add(p, BorderLayout.NORTH);

try
{
conn = getConnection();
DatabaseMetaData meta = conn.getMetaData();
if (meta.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE))
{
scrolling = true;
stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
else
{
stat = conn.createStatement();
scrolling = false;
}
ResultSet tables = meta.getTables(null, null, null, new String[] { "TABLE " });
while (tables.next())
tableNames.addItem(tables.getString(3));
tables.close();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();