数据库显示[]?

时间:2014-06-10 16:06:58

标签: java sql swing ms-access jtable

我制作了一个程序,它从整个jtable中获取字符串并以ms访问方式填充我的数据库。但每当我运行它时,我的数据库都有[]而不是实际信息。我如何获取jtable中的信息并使用它来填充我的数据库?

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class apples extends JPanel {
static JTable table;
static JFrame frame;
static apples a;
 static ActionListener actionListener;
 static ActionListener actionListenerImport;
 static ActionListener actionListenerAddrow;
 static String[] columns = {"Fname","Lname"};
 static String[][] data;
 static DefaultTableModel model = new DefaultTableModel(data, columns);

public static void main(String[] args) {

    table = new JTable(model);
    frame = new JFrame();
    table.setPreferredScrollableViewportSize(new Dimension(450, 200));
    table.setFillsViewportHeight(true);
    JScrollPane jps = new JScrollPane(table);
    JPanel panel = new JPanel(new GridBagLayout());
    panel.add(jps);

    GridBagConstraints c = new GridBagConstraints();
    JButton b1 = new JButton("Start");
    c.gridx = 1;
    c.gridy = 0;
    c.insets = new Insets(10, 10, 10, 10);
    panel.add(b1, c);

    JButton b2 = new JButton("Import");
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(10, 10, 10, 10);
    panel.add(b2, c);

    JButton b3 = new JButton("Add Row");
    c.gridx = 3;
    c.gridy = 0;
    c.insets = new Insets(10,10,10,10);
    panel.add(b3, c);


    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(panel, BorderLayout.NORTH);
    frame.getContentPane().add(jps, BorderLayout.CENTER);

    frame.setVisible(true);
    frame.pack();


    actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection con = DriverManager.getConnection("jdbc:odbc:TestDatabase");
                Statement s = con.createStatement();
                String deleteRows = "DELETE FROM Table1";
                s.execute(deleteRows);
                ArrayList<String> numdata = new ArrayList<String>();
                ArrayList<String> numdata2 = new ArrayList<String>();
                String insertTable = ("INSERT INTO Table1(Fname,Lname) VALUES('"+numdata+"','"+numdata2+"');");//get data from table and put in here

                for (int count = 0; count < model.getRowCount(); count++){
                      numdata.add(model.getValueAt(count, 0).toString());
                      for (int count1 = 0; count1 < model.getRowCount(); count1++){
                          numdata2.add(model.getValueAt(count, 1).toString());
                          s.executeUpdate(insertTable);
                    }
                }




            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                e.getMessage();
            } catch (SQLException e) {
                e.printStackTrace();
                e.getMessage();
            }

        }
    };
    actionListenerImport = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection con = DriverManager.getConnection("jdbc:odbc:TestDatabase");
                Statement s = con.createStatement();
                String selTable = "SELECT * FROM Table1";
                s.execute(selTable);
                ResultSet rs = s.getResultSet();

                while((rs!= null) && (rs.next())) {
                    model.addRow(new String[]{rs.getString(1),rs.getString(2)});
                }



            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                e.getMessage();
            } catch (SQLException e) {
                e.printStackTrace();
                e.getMessage();
            }
        }
    };
    actionListenerAddrow = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.addRow(new String[]{"",""});
        }
    };


    b1.addActionListener(actionListener);
    b2.addActionListener(actionListenerImport);
    b3.addActionListener(actionListenerAddrow);
}

}

1 个答案:

答案 0 :(得分:0)

ArrayList<String> numdata = new ArrayList<String>();
ArrayList<String> numdata2 = new ArrayList<String>();
String insertTable = ("INSERT INTO Table1(Fname,Lname) VALUES('"+numdata+"','"+numdata2+"');");

您正在尝试将ArrayList添加到数据库中。您不能只在循环中更改numdata和numdata2的值。

相反,您需要创建一个动态SQL语句。最简单的方法是使用PreparedStatement,然后您可以动态更改参数。代码类似于:

String sql = "INSERT INTO Table1(Fname,Lname) VALUES(?,?)";
PreparedStatement stmt = connection.prepareStatement(sql);

for (int count = 0; count < model.getRowCount(); count++)
{
    String numdata = model.getValueAt(count, 0).toString();
    String numdata2 = model.getValueAt(count, 1).toString();
    stmt.setString( 1, numdata );
    stmt.setString( 2, numdata2 );
    stmt.executeUpdate();
}