データベースにデータを挿入するプログラムを書いていましたJTable
。エラーや例外は発生していません。オブジェクトも適切に設定されています。executeUpdate
しかし、ステートメントが失敗する理由がわかりません。以下は私のコードです。
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
public class Table extends JFrame implements ActionListener, TableModelListener {
private JTable table;
private JButton addItem, cancel, save, load, search;
DefaultTableModel tableModel;
Object[] columnNames = new Object[]{
"Item No", "ItemName","UnitPrice", "Qty","TotalPrice","Supplier","Remarks"};
public Table() {
getContentPane().setLayout( new FlowLayout() );
tableModel = new DefaultTableModel(columnNames, 0);
tableModel.addTableModelListener( this );
table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
table.setPreferredScrollableViewportSize(new Dimension(600, 400));
getContentPane().add(scrollPane);
addItem = new JButton( "Add" );
addItem.addActionListener( this );
getContentPane().add( addItem );
save = new JButton( "Save" );
save.addActionListener( this );
getContentPane().add( save );
cancel = new JButton( "Cancel" );
cancel.addActionListener( this );
getContentPane().add( cancel );
setVisible( true );
setSize( 640, 500 );
}
public void tableChanged( TableModelEvent e ) {
}
public void actionPerformed( ActionEvent evt ) {
int row = table.getSelectedRow();
int column = table.getSelectedColumn();
if(evt.getSource()== addItem)
{
tableModel.addRow(new Object[]{"","","","","","",""});
}
else if ( evt.getSource() == save )
saveCustomers();
}
private void saveCustomers() {
PreparedStatement pstm = null;
ResultSet rs;
int index=1;
int count=table.getRowCount();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection connect =DriverManager.getConnection("jdbc:mysql://localhost/restaurant","root","root");
for(int i=0;i<count;i++){
Object obj1 = GetData(table, i, 0);
Object obj2 = GetData(table, i, 1);
Object obj3 = GetData(table, i, 2);
Object obj4 = GetData(table, i, 3);
Object obj5 = GetData(table, i, 4);
Object obj6 = GetData(table, i, 5);
Object obj7 = GetData(table, i, 6);
int value1= Integer.parseInt(obj1.toString());
System.out.println(value1);
String value2=obj2.toString();
double value3=Double.parseDouble(obj3.toString());
System.out.println(value2);
double value4=Double.parseDouble(obj4.toString());
System.out.println(value3);
double value5=Double.parseDouble(obj5.toString());
System.out.println(value5);
String value6=obj6.toString();
System.out.println(value6);
String value7=obj7.toString();
System.out.println(value7);
pstm=connect.prepareStatement("insert into stock values(?,?,?,?,?,?,?)");
System.out.println("Preperation");
pstm.setInt(1,value1);
pstm.setString(2,value2);
pstm.setDouble(3,value3);
pstm.setDouble(4, value4);
pstm.setDouble(5,value5);
pstm.setString(6, value6);
pstm.setString(7, value7);
index++;
}
System.out.println("execute");
pstm.executeUpdate();
System.out.println("saved Successfully");
}
catch(Exception e){}
}
public Object GetData(JTable table, int row_index, int col_index){
return table.getModel().getValueAt(row_index, col_index);
}
public static void main ( String[] args ) {
Table frm = new Table();
frm.setVisible(true);
frm.setSize( 640, 500 );
frm.setResizable(false);
WindowQuitter wquit = new WindowQuitter();
frm.addWindowListener( wquit );
}
}
class WindowQuitter extends WindowAdapter {
public void windowClosing( WindowEvent e ) {
System.exit( 0 );
}
}