从MS Access中选择COUNT到带有DButils的JTable

时间:2012-08-15 21:11:08

标签: java sql select count jtable

我有一个JDBC:ODBC连接到MS Access数据库,它使用DButils填充JTable。我的基本SQL查询工作正常,直到我尝试COUNT函数,然后在尝试填充JTable时出错。该查询可以正常填充JTextArea,所以我知道查询是正常的。 我的代码低于任何帮助将不胜感激。

    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import java.sql.*;
    import net.proteanit.sql.DbUtils;

public class table {
    Connection con;
    Statement st;
    ResultSet rs;

    public table(){
        connect();
    }
    public void connect(){

        JTable tbl = new JTable();
        tbl.setDefaultEditor(Object.class, null);
        JFrame resFrame = new JFrame("Results");
        resFrame.setLayout(null);
        resFrame.setSize(525, 445);
        resFrame.setVisible(true);
        JScrollPane tsp = new JScrollPane(tbl);
        tsp.setLocation(20, 20);
        resFrame.add(tsp);
        tbl.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        tsp.setSize(370,375);

        String sql = "SELECT Club, COUNT('memberID') AS total FROM Members_Table, Club_Table WHERE Club_Table.clubID=Members_Table.clubID GROUP BY Club";

        try{

            String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
            Class.forName(driver);
            String db = "jdbc:odbc:ITUKdb";
            con = DriverManager.getConnection(db);
            st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
            rs = st.executeQuery(sql);
            tbl.setModel(net.proteanit.sql.DbUtils.resultSetToTableModel(rs));
        }catch(Exception ex){

        }
    }
}

我得到的错误在

之下
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
    at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(JdbcOdbc.java:3906)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(JdbcOdbcResultSet.java:5697)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:353)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getObject(JdbcOdbcResultSet.java:1677)
    at net.proteanit.sql.DbUtils.resultSetToTableModel(DbUtils.java:28)
    at trial.table.connect(table.java:53)
    at trial.table.<init>(table.java:22)
    at trial.ITUKSQL.main(ITUKSQL.java:212)

1 个答案:

答案 0 :(得分:0)

排序必须删除

ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE

来自

 st = con.createStatement();