Java 如何使用fireTableRowsInserted和CachedRowSet在JTable中插入新行?
我试图使用按钮从JTable中具有CachedRowSet数据结构的表单(由文本字段组成)中插入新行。创建GIU的类(包括按钮,即CoffeesFrame.java)如下所示:Java 如何使用fireTableRowsInserted和CachedRowSet在JTable中插入新行?,java,swing,Java,Swing,我试图使用按钮从JTable中具有CachedRowSet数据结构的表单(由文本字段组成)中插入新行。创建GIU的类(包括按钮,即CoffeesFrame.java)如下所示: public class CoffeesFrame extends JFrame implements RowSetListener { private static Connection myConn = null; private static String url = null; priv
public class CoffeesFrame extends JFrame implements RowSetListener {
private static Connection myConn = null;
private static String url = null;
private static String username = null;
private static String password = null;
private static JTable table; // The table for displaying data
private static JFrame frame;
private static JLabel label_COF_NAME;
private static JLabel label_SUP_ID;
private static JLabel label_PRICE;
private static JLabel label_SALES;
private static JLabel label_TOTAL;
private static JTextField textField_COF_NAME;
private static JTextField textField_SUP_ID;
private static JTextField textField_PRICE;
private static JTextField textField_SALES;
private static JTextField textField_TOTAL;
private static JButton button_ADD_ROW;
private static JButton button_UPDATE_DATABASE;
private static JButton button_DISCARD_CHANGES;
CoffeesTableModel ctModel;
public CoffeesFrame() {
url = "jdbc:mysql://localhost:3306/myDemo";
username = "student";
password = "student";
}
protected void createAndShowGUI(Connection con) throws SQLException {
con.setAutoCommit(false);
System.out.println("The GUI started");
frame = new JFrame();
frame.setTitle("My Coffee Frame");
CachedRowSet crSet = getContentsOfCoffeesTable(con);
ctModel = new CoffeesTableModel(crSet);
ctModel.addEventHandelersToRowsSet(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try {
myConn.close();
}
catch (SQLException sl) {
sl.printStackTrace();
}
System.exit(0);
}
});
/*
String[] columnNames = { "First Name", "Last Name", "Sport",
"# of Years", "Vegetarian" };
Object[][] data = {
{ "Kathy", "Smith", "Snowboarding", new Integer(5),
new Boolean(false) },
{ "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
{ "Sue", "Black", "Knitting", new Integer(2),
new Boolean(false) },
{ "Jane", "White", "Speed reading", new Integer(20),
new Boolean(true) },
{ "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };
// table = new JTable(data, columnNames);
*/
table = new JTable();
table.setModel(ctModel);
label_COF_NAME = new JLabel();
label_SUP_ID = new JLabel();
label_PRICE = new JLabel();
label_SALES = new JLabel();
label_TOTAL = new JLabel();
textField_COF_NAME = new JTextField(10);
textField_SUP_ID = new JTextField(10);
textField_PRICE = new JTextField(10);
textField_SALES = new JTextField(10);
textField_TOTAL = new JTextField(10);
button_ADD_ROW = new JButton();
button_UPDATE_DATABASE = new JButton();
button_DISCARD_CHANGES = new JButton();
label_COF_NAME.setText("Coffee Name:");
label_SUP_ID.setText("Supplier ID:");
label_PRICE.setText("Price:");
label_SALES.setText("Sales:");
label_TOTAL.setText("Total Sales:");
textField_COF_NAME.setText("Enter new coffee name");
textField_SUP_ID.setText("101");
textField_PRICE.setText("0");
textField_SALES.setText("0");
textField_TOTAL.setText("0");
button_ADD_ROW.setText("Add row to table");
button_UPDATE_DATABASE.setText("Update database");
button_DISCARD_CHANGES.setText("Discard changes");
Container pane = getContentPane();
pane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
pane.setLayout(new GridBagLayout());
GridBagConstraints cons = new GridBagConstraints();
cons.fill = GridBagConstraints.BOTH;
cons.anchor = GridBagConstraints.CENTER;
cons.weightx = 0.5;
cons.weighty = 1.0;
cons.gridx = 0;
cons.gridy = 0;
cons.gridwidth = 2;
pane.add(new JScrollPane(table), cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_START;
cons.weightx = 0.25;
cons.weighty = 0;
cons.gridx = 0;
cons.gridy = 1;
cons.gridwidth = 1;
pane.add(label_COF_NAME, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_END;
cons.weightx = 0.75;
cons.weighty = 0;
cons.gridx = 1;
cons.gridy = 1;
cons.gridwidth = 1;
pane.add(textField_COF_NAME, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.weightx = 0.25;
cons.weighty = 0;
cons.anchor = GridBagConstraints.LINE_START;
cons.gridx = 0;
cons.gridy = 2;
cons.gridwidth = 1;
pane.add(label_SUP_ID, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_END;
cons.weightx = 0.75;
cons.weighty = 0;
cons.gridx = 1;
cons.gridy = 2;
cons.gridwidth = 1;
pane.add(textField_SUP_ID, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_START;
cons.weightx = 0.25;
cons.weighty = 0;
cons.gridx = 0;
cons.gridy = 3;
cons.gridwidth = 1;
pane.add(label_PRICE, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_END;
cons.weightx = 0.75;
cons.weighty = 0;
cons.gridx = 1;
cons.gridy = 3;
cons.gridwidth = 1;
pane.add(textField_PRICE, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_START;
cons.weightx = 0.25;
cons.weighty = 0;
cons.gridx = 0;
cons.gridy = 4;
cons.gridwidth = 1;
pane.add(label_SALES, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_END;
cons.weightx = 0.75;
cons.weighty = 0;
cons.gridx = 1;
cons.gridy = 4;
cons.gridwidth = 1;
pane.add(textField_SALES, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_START;
cons.weightx = 0.25;
cons.weighty = 0;
cons.gridx = 0;
cons.gridy = 5;
cons.gridwidth = 1;
pane.add(label_TOTAL, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_END;
cons.weightx = 0.75;
cons.weighty = 0;
cons.gridx = 1;
cons.gridy = 5;
cons.gridwidth = 1;
pane.add(textField_TOTAL, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_START;
cons.weightx = 0.5;
cons.weighty = 0;
cons.gridx = 0;
cons.gridy = 6;
cons.gridwidth = 1;
pane.add(button_ADD_ROW, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_END;
cons.weightx = 0.5;
cons.weighty = 0;
cons.gridx = 1;
cons.gridy = 6;
cons.gridwidth = 1;
pane.add(button_UPDATE_DATABASE, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_START;
cons.weightx = 0.5;
cons.weighty = 0;
cons.gridx = 0;
cons.gridy = 7;
cons.gridwidth = 1;
pane.add(button_DISCARD_CHANGES, cons);
button_ADD_ROW.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// System.out.println("The add row");
JOptionPane.showMessageDialog(frame, textField_COF_NAME.getText()+" "+
Integer.parseInt(textField_SUP_ID.getText().trim())+" "+
Float.parseFloat(textField_PRICE.getText().trim())+" "+
Integer.parseInt(textField_SALES.getText().trim())+" "+
Integer.parseInt(textField_TOTAL.getText().trim()));
try {
ctModel.insertRow(textField_COF_NAME.getText(),
Integer.parseInt(textField_SUP_ID.getText().trim()),
Float.parseFloat(textField_PRICE.getText().trim()),
Integer.parseInt(textField_SALES.getText().trim()),
Integer.parseInt(textField_TOTAL.getText().trim()));
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
});
frame.add(pane);
frame.pack();
frame.setVisible(true);
}
private CachedRowSet getContentsOfCoffeesTable(Connection con)
throws SQLException {
CachedRowSet cr = null;
try {
cr = new CachedRowSetImpl();
cr.setType(ResultSet.TYPE_SCROLL_INSENSITIVE);
cr.setConcurrency(ResultSet.CONCUR_UPDATABLE);
cr.setUsername(username);
cr.setPassword(password);
cr.setUrl(url);
cr.setCommand("select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES");
cr.execute();
}
catch (Exception ex) {
ex.printStackTrace();
}
return cr;
}
@Override
public void rowSetChanged(RowSetEvent event) {
}
@Override
public void rowChanged(RowSetEvent event) {
}
@Override
public void cursorMoved(RowSetEvent event) {
}
public static void main(String[] args) throws SQLException {
CoffeesFrame cf = new CoffeesFrame();
try {
myConn = DriverManager.getConnection(url, username, password);
if (myConn != null) {
System.out.println("The app is connected with the database");
}
else {
System.out.println("The connection is not established");
}
}
catch (Exception ex) {
ex.printStackTrace();
}
// launch the swing app
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
cf.createAndShowGUI(myConn);
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
}
public class CoffeesTableModel extends AbstractTableModel {
public static CachedRowSet coffeeRowSet;
public static java.sql.ResultSetMetaData rsMetaData;
public static int ncolms, nrows;
public CoffeesTableModel(CachedRowSet crSet) throws SQLException {
this.coffeeRowSet = crSet;
this.rsMetaData = this.coffeeRowSet.getMetaData();
ncolms = rsMetaData.getColumnCount();
this.coffeeRowSet.beforeFirst();
this.nrows = 0;
while (this.coffeeRowSet != null & this.coffeeRowSet.next()) {
nrows++;
}
this.coffeeRowSet.beforeFirst();
}
public void addEventHandelersToRowsSet(RowSetListener rsListener) {
this.coffeeRowSet.addRowSetListener(rsListener);
}
public void insertRow(String coffeeName, int supplierID, float price,
int sales, int total) throws SQLException {
try {
this.coffeeRowSet.moveToInsertRow();
this.coffeeRowSet.updateString("COF_NAME", coffeeName);
this.coffeeRowSet.updateInt("SUP_ID", supplierID);
this.coffeeRowSet.updateFloat("PRICE", price);
this.coffeeRowSet.updateInt("SALES", sales);
this.coffeeRowSet.updateInt("TOTAL", total);
this.coffeeRowSet.insertRow();
this.coffeeRowSet.moveToCurrentRow();
fireTableRowsInserted(coffeeRowSet.getRow()-1, coffeeRowSet.getRow()-1);
// I get SQL error after un-commenting this line
// this.coffeeRowSet.acceptChanges();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void close() {
try {
coffeeRowSet.getStatement().close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public String getColumnName(int columnIndex) {
String colName = null;
try {
colName = this.rsMetaData.getColumnLabel(columnIndex + 1);
} catch (Exception ex) {
ex.toString();
}
return colName;
}
@Override
public int getRowCount() {
return nrows;
}
@Override
public int getColumnCount() {
return ncolms;
}
public Class getColumnClass(int column) {
return String.class;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object o = new Object();
try {
this.coffeeRowSet.absolute(rowIndex + 1);
o = this.coffeeRowSet.getObject(columnIndex + 1);
}
catch (SQLException ex) {
ex.printStackTrace();
}
if (o == null){
return null;
}
else{
return o.toString();
}
}
public boolean isEdible(int rowIndex , int colIndex ){
return false;
}
public void setValueAt(Object o, int col, int row ){
System.out.println();
}}
它使用CoffeesTableModel.java进行数据建模,如下所示:
public class CoffeesFrame extends JFrame implements RowSetListener {
private static Connection myConn = null;
private static String url = null;
private static String username = null;
private static String password = null;
private static JTable table; // The table for displaying data
private static JFrame frame;
private static JLabel label_COF_NAME;
private static JLabel label_SUP_ID;
private static JLabel label_PRICE;
private static JLabel label_SALES;
private static JLabel label_TOTAL;
private static JTextField textField_COF_NAME;
private static JTextField textField_SUP_ID;
private static JTextField textField_PRICE;
private static JTextField textField_SALES;
private static JTextField textField_TOTAL;
private static JButton button_ADD_ROW;
private static JButton button_UPDATE_DATABASE;
private static JButton button_DISCARD_CHANGES;
CoffeesTableModel ctModel;
public CoffeesFrame() {
url = "jdbc:mysql://localhost:3306/myDemo";
username = "student";
password = "student";
}
protected void createAndShowGUI(Connection con) throws SQLException {
con.setAutoCommit(false);
System.out.println("The GUI started");
frame = new JFrame();
frame.setTitle("My Coffee Frame");
CachedRowSet crSet = getContentsOfCoffeesTable(con);
ctModel = new CoffeesTableModel(crSet);
ctModel.addEventHandelersToRowsSet(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try {
myConn.close();
}
catch (SQLException sl) {
sl.printStackTrace();
}
System.exit(0);
}
});
/*
String[] columnNames = { "First Name", "Last Name", "Sport",
"# of Years", "Vegetarian" };
Object[][] data = {
{ "Kathy", "Smith", "Snowboarding", new Integer(5),
new Boolean(false) },
{ "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
{ "Sue", "Black", "Knitting", new Integer(2),
new Boolean(false) },
{ "Jane", "White", "Speed reading", new Integer(20),
new Boolean(true) },
{ "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };
// table = new JTable(data, columnNames);
*/
table = new JTable();
table.setModel(ctModel);
label_COF_NAME = new JLabel();
label_SUP_ID = new JLabel();
label_PRICE = new JLabel();
label_SALES = new JLabel();
label_TOTAL = new JLabel();
textField_COF_NAME = new JTextField(10);
textField_SUP_ID = new JTextField(10);
textField_PRICE = new JTextField(10);
textField_SALES = new JTextField(10);
textField_TOTAL = new JTextField(10);
button_ADD_ROW = new JButton();
button_UPDATE_DATABASE = new JButton();
button_DISCARD_CHANGES = new JButton();
label_COF_NAME.setText("Coffee Name:");
label_SUP_ID.setText("Supplier ID:");
label_PRICE.setText("Price:");
label_SALES.setText("Sales:");
label_TOTAL.setText("Total Sales:");
textField_COF_NAME.setText("Enter new coffee name");
textField_SUP_ID.setText("101");
textField_PRICE.setText("0");
textField_SALES.setText("0");
textField_TOTAL.setText("0");
button_ADD_ROW.setText("Add row to table");
button_UPDATE_DATABASE.setText("Update database");
button_DISCARD_CHANGES.setText("Discard changes");
Container pane = getContentPane();
pane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
pane.setLayout(new GridBagLayout());
GridBagConstraints cons = new GridBagConstraints();
cons.fill = GridBagConstraints.BOTH;
cons.anchor = GridBagConstraints.CENTER;
cons.weightx = 0.5;
cons.weighty = 1.0;
cons.gridx = 0;
cons.gridy = 0;
cons.gridwidth = 2;
pane.add(new JScrollPane(table), cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_START;
cons.weightx = 0.25;
cons.weighty = 0;
cons.gridx = 0;
cons.gridy = 1;
cons.gridwidth = 1;
pane.add(label_COF_NAME, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_END;
cons.weightx = 0.75;
cons.weighty = 0;
cons.gridx = 1;
cons.gridy = 1;
cons.gridwidth = 1;
pane.add(textField_COF_NAME, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.weightx = 0.25;
cons.weighty = 0;
cons.anchor = GridBagConstraints.LINE_START;
cons.gridx = 0;
cons.gridy = 2;
cons.gridwidth = 1;
pane.add(label_SUP_ID, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_END;
cons.weightx = 0.75;
cons.weighty = 0;
cons.gridx = 1;
cons.gridy = 2;
cons.gridwidth = 1;
pane.add(textField_SUP_ID, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_START;
cons.weightx = 0.25;
cons.weighty = 0;
cons.gridx = 0;
cons.gridy = 3;
cons.gridwidth = 1;
pane.add(label_PRICE, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_END;
cons.weightx = 0.75;
cons.weighty = 0;
cons.gridx = 1;
cons.gridy = 3;
cons.gridwidth = 1;
pane.add(textField_PRICE, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_START;
cons.weightx = 0.25;
cons.weighty = 0;
cons.gridx = 0;
cons.gridy = 4;
cons.gridwidth = 1;
pane.add(label_SALES, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_END;
cons.weightx = 0.75;
cons.weighty = 0;
cons.gridx = 1;
cons.gridy = 4;
cons.gridwidth = 1;
pane.add(textField_SALES, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_START;
cons.weightx = 0.25;
cons.weighty = 0;
cons.gridx = 0;
cons.gridy = 5;
cons.gridwidth = 1;
pane.add(label_TOTAL, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_END;
cons.weightx = 0.75;
cons.weighty = 0;
cons.gridx = 1;
cons.gridy = 5;
cons.gridwidth = 1;
pane.add(textField_TOTAL, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_START;
cons.weightx = 0.5;
cons.weighty = 0;
cons.gridx = 0;
cons.gridy = 6;
cons.gridwidth = 1;
pane.add(button_ADD_ROW, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_END;
cons.weightx = 0.5;
cons.weighty = 0;
cons.gridx = 1;
cons.gridy = 6;
cons.gridwidth = 1;
pane.add(button_UPDATE_DATABASE, cons);
cons.fill = GridBagConstraints.HORIZONTAL;
cons.anchor = GridBagConstraints.LINE_START;
cons.weightx = 0.5;
cons.weighty = 0;
cons.gridx = 0;
cons.gridy = 7;
cons.gridwidth = 1;
pane.add(button_DISCARD_CHANGES, cons);
button_ADD_ROW.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// System.out.println("The add row");
JOptionPane.showMessageDialog(frame, textField_COF_NAME.getText()+" "+
Integer.parseInt(textField_SUP_ID.getText().trim())+" "+
Float.parseFloat(textField_PRICE.getText().trim())+" "+
Integer.parseInt(textField_SALES.getText().trim())+" "+
Integer.parseInt(textField_TOTAL.getText().trim()));
try {
ctModel.insertRow(textField_COF_NAME.getText(),
Integer.parseInt(textField_SUP_ID.getText().trim()),
Float.parseFloat(textField_PRICE.getText().trim()),
Integer.parseInt(textField_SALES.getText().trim()),
Integer.parseInt(textField_TOTAL.getText().trim()));
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
});
frame.add(pane);
frame.pack();
frame.setVisible(true);
}
private CachedRowSet getContentsOfCoffeesTable(Connection con)
throws SQLException {
CachedRowSet cr = null;
try {
cr = new CachedRowSetImpl();
cr.setType(ResultSet.TYPE_SCROLL_INSENSITIVE);
cr.setConcurrency(ResultSet.CONCUR_UPDATABLE);
cr.setUsername(username);
cr.setPassword(password);
cr.setUrl(url);
cr.setCommand("select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES");
cr.execute();
}
catch (Exception ex) {
ex.printStackTrace();
}
return cr;
}
@Override
public void rowSetChanged(RowSetEvent event) {
}
@Override
public void rowChanged(RowSetEvent event) {
}
@Override
public void cursorMoved(RowSetEvent event) {
}
public static void main(String[] args) throws SQLException {
CoffeesFrame cf = new CoffeesFrame();
try {
myConn = DriverManager.getConnection(url, username, password);
if (myConn != null) {
System.out.println("The app is connected with the database");
}
else {
System.out.println("The connection is not established");
}
}
catch (Exception ex) {
ex.printStackTrace();
}
// launch the swing app
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
cf.createAndShowGUI(myConn);
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
}
public class CoffeesTableModel extends AbstractTableModel {
public static CachedRowSet coffeeRowSet;
public static java.sql.ResultSetMetaData rsMetaData;
public static int ncolms, nrows;
public CoffeesTableModel(CachedRowSet crSet) throws SQLException {
this.coffeeRowSet = crSet;
this.rsMetaData = this.coffeeRowSet.getMetaData();
ncolms = rsMetaData.getColumnCount();
this.coffeeRowSet.beforeFirst();
this.nrows = 0;
while (this.coffeeRowSet != null & this.coffeeRowSet.next()) {
nrows++;
}
this.coffeeRowSet.beforeFirst();
}
public void addEventHandelersToRowsSet(RowSetListener rsListener) {
this.coffeeRowSet.addRowSetListener(rsListener);
}
public void insertRow(String coffeeName, int supplierID, float price,
int sales, int total) throws SQLException {
try {
this.coffeeRowSet.moveToInsertRow();
this.coffeeRowSet.updateString("COF_NAME", coffeeName);
this.coffeeRowSet.updateInt("SUP_ID", supplierID);
this.coffeeRowSet.updateFloat("PRICE", price);
this.coffeeRowSet.updateInt("SALES", sales);
this.coffeeRowSet.updateInt("TOTAL", total);
this.coffeeRowSet.insertRow();
this.coffeeRowSet.moveToCurrentRow();
fireTableRowsInserted(coffeeRowSet.getRow()-1, coffeeRowSet.getRow()-1);
// I get SQL error after un-commenting this line
// this.coffeeRowSet.acceptChanges();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void close() {
try {
coffeeRowSet.getStatement().close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public String getColumnName(int columnIndex) {
String colName = null;
try {
colName = this.rsMetaData.getColumnLabel(columnIndex + 1);
} catch (Exception ex) {
ex.toString();
}
return colName;
}
@Override
public int getRowCount() {
return nrows;
}
@Override
public int getColumnCount() {
return ncolms;
}
public Class getColumnClass(int column) {
return String.class;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object o = new Object();
try {
this.coffeeRowSet.absolute(rowIndex + 1);
o = this.coffeeRowSet.getObject(columnIndex + 1);
}
catch (SQLException ex) {
ex.printStackTrace();
}
if (o == null){
return null;
}
else{
return o.toString();
}
}
public boolean isEdible(int rowIndex , int colIndex ){
return false;
}
public void setValueAt(Object o, int col, int row ){
System.out.println();
}}
JOptionPane在按下“将行添加到表”按钮后显示数据,但没有在表中插入数据。如何改进代码?既然您在问题中添加了更多代码,我想知道您为什么要使用
CachedRowSet
界面(参见示例和)。我会考虑从数据库中检索数据并将其存储在常规对象或表模型中。
对于表格模型,还可以使用DefaultTableModel
类。这样做的好处是为所有方法都创建了默认实现,这使您的工作更加轻松。尽可能将表模型与数据库访问分离可能是一个好主意
您可以将所有coffee数据从数据库复制到DefaultTableModel
实例并关闭数据库连接(您甚至可以在TableModel类之外执行此操作)。稍后,您可以在希望更新数据库时启动新的数据库连接(例如,删除所有旧记录并插入所有当前记录;您可以使用getDataVector
方法检索表模型中的当前记录)
表模型类可以如下所示:
import java.sql.*;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
public class CoffeesTableModelV2 extends DefaultTableModel {
public CoffeesTableModelV2(Vector data, Vector columnNames) {
super(data, columnNames);
}
public void insertRow(String coffeeName, int supplierID, float price,
int sales, int total) {
final Vector<Object> rowVector = new Vector<>();
rowVector.add(coffeeName);
rowVector.add(supplierID);
rowVector.add(price);
rowVector.add(sales);
rowVector.add(total);
addRow(rowVector);
}
@Override
//public boolean isEdible(int rowIndex, int colIndex) {
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
@Override
public void setValueAt(Object o, int col, int row) {
final String message = "should not be called for a read-only table model";
throw new RuntimeException("CoffeesTableModelV2.setValueAt " + message);
}
public static CoffeesTableModelV2 createTableModel(ResultSet resultSet)
throws SQLException {
return new CoffeesTableModelV2(getData(resultSet), getColumnNames(resultSet));
}
private static Vector<Vector<Object>> getData(ResultSet resultSet)
throws SQLException {
Vector<Vector<Object>> data = new Vector<>();
int columnCount = resultSet.getMetaData().getColumnCount();
while (resultSet.next()) {
Vector<Object> rowVector = new Vector<>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
rowVector.add(resultSet.getObject(columnIndex));
}
data.add(rowVector);
}
return data;
}
private static Vector<String> getColumnNames(ResultSet resultSet)
throws SQLException {
Vector<String> columnNames = new Vector<>();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
columnNames.add(metaData.getColumnName(columnIndex));
}
return columnNames;
}
}
import java.sql.*;
导入java.util.Vector;
导入javax.swing.table.DefaultTableModel;
公共类CoffeesTableModelV2扩展了DefaultTableModel{
public CoffeesTableModelV2(向量数据、向量列名称){
超级(数据、列名称);
}
public void insertRow(字符串coffeeName、int supplierID、浮动价格、,
整数销售额(整数合计){
最终向量rowVector=新向量();
rowVector.add(咖啡名);
rowVector.add(供应商ID);
rowVector.add(价格);
添加(销售);
rowVector.add(总计);
addRow(行向量);
}
@凌驾
//公共布尔值可编辑(int行索引,int colIndex){
公共布尔值isCellEditable(int-rowIndex、int-columnIndex){
返回false;
}
@凌驾
public void setValueAt(对象o、整数列、整数行){
final String message=“不应为只读表模型调用”;
抛出新的运行时异常(“CoffeesTableModelV2.setValueAt”+消息);
}
公共静态CoffeesTableModelV2 createTableModel(结果集结果集)
抛出SQLException{
返回新的CoffeesTableModelV2(getData(resultSet)、getColumnNames(resultSet));
}
私有静态向量getData(ResultSet ResultSet)
抛出SQLException{
向量数据=新向量();
int columnCount=resultSet.getMetaData().getColumnCount();
while(resultSet.next()){
向量行向量=新向量();
对于(int columnIndex=1;columnIndex可能类似于fireTableRowsInserted(coffeeRowSet.getRow()-1,coffeeRowSet.getRow()-1);
感谢您的回答,但是,这不起作用。不过,我没有收到任何错误。数据来自我可以使用消息框看到的内容:JOptionPane.showMessageDialog(frame,textField\u COF\u NAME.getText()+“”+Integer.parseInt(textField\u SUP\u ID.getText().trim())+“”+Float.parseFloat(textField\u PRICE.getText().trim())+“”+Integer.parseInt(textField\u SALES.getText().trim())+“”+Integer.parseInt(textField\u TOTAL.getText().trim());您仍然在使用TableModel还是在使用AbstractTableModel?您仍然在使用空实现覆盖add/removeTableModelListener吗?嘿,很抱歉回复太晚。正如您之前所建议的,我是从AbstractTableModel进行扩展,而不是使用空实现覆盖add/removeTableModelListener。我更新了q关于整个程序的问题,您可以看一下。很抱歉回复太晚,我非常感谢您的回答。但是,解决方案不起作用,因为它从以下行提供了“java.sql.SQLException:无法获得连接”:this.coffeeRowSet.acceptChanges();如果我注释掉这一行,程序运行正常,但按下按钮后不会添加任何内容。