无法将数据从JTable插入数据库

时间:2012-05-18 20:30:48

标签: java mysql database swing jtable

我正在编写一个程序,用于将数据从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 );
    }
}

2 个答案:

答案 0 :(得分:0)

我不确定,但我认为您的连接网址错误,请尝试此操作

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

其中3306是localhost上mysql的默认端口号。

答案 1 :(得分:0)

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

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

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

因为据我所知,你必须指出与每个?对应的字段。

查看示例here