1

我正在编写一个程序来将数据插入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 );
    }
}
4

2 回答 2

0

您的插入语句不正确,您应该使用:

connect.prepareStatement("insert into stock(field1,field2,field3,field4,field5,field6,field7) values(?,?,?,?,?,?,?)");

(用实际的字段名称替换字段!)

因为据我所知,您必须指出与每个?.

请参阅此处的一些示例。

于 2012-05-27T10:10:56.230 回答
0

我不确定,但我认为您的连接 url 错误,试试这个

Connection con = ( Connection ) DriverManager.getConnection( "jdbc:mysql://localhost:3306/yourdatabasename", "yourmysqlid", "yourmysqlpassword" );

3306本地 mysql 的默认端口号在哪里。

于 2012-05-25T07:00:44.047 回答