我正在尝试将以下内容更新到数据库,即表和日期文本字段,但是当尝试将jtable更新到数据库时,它无法正常工作,我得到一个数字异常。我应该添加/更改什么代码?提前谢谢。
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class Table1 {
JFrame frame;
JComboBox credit;
String[] rowNames = {
"Vasan Phalke", "Pansare", "Anil Kg", "Suresh", "Total Credit", ""};
String[] colNames = {"Name", "Qty", "Rate/ Kg", "Total Amt."};
JTable table;
DefaultTableModel model;
JScrollPane scrollPane;
JButton saveButton;
JTextField dateField;
JLabel dateLabel;
public Table1() {
frame = new JFrame();
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
credit = new JComboBox<>(rowNames);
dateLabel = new JLabel("Date");
dateField = new JTextField(20);
saveButton = new JButton("Save");
saveButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("Jdbc:Odbc:TomsJava");
String i5Date1 = dateField.getText();
PreparedStatement ps = conn.prepareStatement("UPDATE DailyAnalysis SET Total_Credit = ? where Date = i5Date1");
ps.setDouble(1, (Double) table.getValueAt(5,3));
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT * FROM DailyAnalysis");
rs.moveToInsertRow();
rs.updateString("Date1",i5Date1);
rs.close();
ps.close();
conn.close();
}
catch(SQLException e)
{
System.out.println(e);
}
catch(ClassNotFoundException z)
{
System.out.println(z);
}
catch(NumberFormatException n)
{
System.out.println(n);
}
}
});
model = new DefaultTableModel(colNames, 6) {
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
return String.class;
case 1:
return Integer.class;
case 2:
return Double.class;
case 3:
return Double.class;
}
return super.getColumnClass(columnIndex);
}
@Override
public Object getValueAt(int row, int col) {
if (col == 3 & row == 5) {
double sum = 0;
for (int i = 0; i < 5; i++) {
sum += ((Double) getValueAt(i, 3)).doubleValue();
}
return sum;
}
if (col == 3 & row != 5) {
Integer i = (Integer) getValueAt(row, 1);
Double d = (Double) getValueAt(row, 2);
if (i != null && d != null) {
return i * d;
} else {
return 0d;
}
}
return super.getValueAt(row, col);
}
@Override
public void setValueAt(Object aValue, int row, int col) {
super.setValueAt(aValue, row, col);
fireTableDataChanged();
}
@Override
public boolean isCellEditable(int row, int col) {
return col != 3;
}
};
table = new JTable(model);
TableColumn nameColumn = table.getColumnModel().getColumn(0);
nameColumn.setCellEditor(new DefaultCellEditor(credit));
scrollPane = new JScrollPane(table);
frame.add(dateLabel);
frame.add(dateField);
frame.add(new JScrollPane(table));
frame.add(saveButton);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Table1();
}
});
}
}