ResultSet to jTable = java.sql.SQLException:已完成准备好的语句

时间:2017-11-16 17:06:47

标签: java swing sqlite jdbc

StackOverFlow之友!

我在select语句中尝试“返回”一个ResultSet并且有错误... 我在Google和StackOverFlow中搜索,但没有解决这个问题!

声明插入工作正常!但选择不起作用......

数据库层:

package Database;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DBConnection {
    private static Connection               conn = null;
    private static ResultSet                rs = null;
    private static Statement                stmt = null;
    private static PreparedStatement        pst = null;

    private static final String             DATABASE_PATH_NAME="jdbc:sqlite:MiraReal.Xitano";

    private Connection Connect()
    {
        try
        {
            Class.forName("org.sqlite.JDBC");

            conn                            = DriverManager.getConnection(DATABASE_PATH_NAME);

            return conn;
        }
        catch (Exception ex)
        {
            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);

            return null;
        }
    }

    public boolean RunQuery(String sql)
    {
        Statement                           stmt;
        try {
            Connect();

            stmt                            = conn.createStatement();

            stmt.execute(sql);

            Disconnect();

            return true;

        } catch (SQLException ex) {
            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        // create a new table
    }

    public ResultSet RunSelect(String sql){
        try {
            Connect();

            stmt                            = conn.createStatement();

            rs                              = stmt.executeQuery(sql);
            //rs..moveToFirst();
            Disconnect();
        } catch (SQLException ex) {
            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }

    public int RunCount(String sql, String where)
    {
        return 1;
    }

    private void Disconnect()
    {
        try 
        {
            DBConnection.conn.close();
            DBConnection.conn               = null;
        } catch (SQLException ex) {
            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

动作图层:

package Actions;

import Models.CustomersModels;
import java.sql.ResultSet;
import java.util.logging.Logger;

public class CustomersActions extends CustomersModels {
    protected String sqlInsert = "INSERT INTO customers (uuid, fullname, email, created_at, updated_at) VALUES ('%s', '%s', '%s', '%s', '%s') ";
    protected String sqlUpdate = "UPDATE customers SET fullname='%s' WHERE uuid='%s'";
    protected String sqlDelete = "DELETE FROM customers WHERE uuid='%s'";
    protected String sqlSelect = "SELECT %s FROM customers %s %s %s";

    public boolean insert()
    {
        String sql = String.format(sqlInsert, this.getUuid(), 
                this.getFullname(), this.getEmail(), this.getCreatedAt(), 
                this.getUpdatedAt());

        return this.RunQuery(sql);
    }

    public boolean update()
    {
        String sql = String.format(sqlUpdate, this.getFullname(), this.getUuid());

        return this.RunQuery(sql);
    }

    public boolean delete()
    {
        String sql = String.format(sqlDelete, this.getUuid());

        return this.RunQuery(sql);
    }

    public ResultSet select(String columns, String where, String order, String limit)
    {
        String sql = String.format(sqlSelect, columns, where, order, limit);

        System.out.println(sql);

        return this.RunSelect(sql);
    }

}

尝试使用它(使用net.proteanit.sql.DbUtils将结果集设置为jTable):

Customers customers = new Customers();

        String customersColumns = "*";
        String customersWhere = "";
        String customersOrder = "";
        String customersLimit = "";

        ResultSet dsCustomers = customers.select(customersColumns, customersWhere, customersOrder, customersLimit);

        this.jTable1.setModel(DbUtils.resultSetToTableModel(dsCustomers));

错误:

run:
SELECT * FROM customers   
java.sql.SQLException: The prepared statement has been finalized
    at org.sqlite.core.NativeDB.throwex(NativeDB.java:471)
    at org.sqlite.core.NativeDB.column_name_utf8(Native Method)
    at org.sqlite.core.NativeDB.column_name(NativeDB.java:232)
    at org.sqlite.jdbc3.JDBC3ResultSet.getColumnName(JDBC3ResultSet.java:721)
    at org.sqlite.jdbc3.JDBC3ResultSet.getColumnLabel(JDBC3ResultSet.java:714)
    at net.proteanit.sql.DbUtils.resultSetToTableModel(DbUtils.java:21)
    at Frames.MiraReal.seederCustomers(MiraReal.java:142)
    at Frames.MiraReal.<init>(MiraReal.java:37)
    at Frames.MiraReal$2.run(MiraReal.java:177)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Frames.MiraReal.seederCustomers(MiraReal.java:142)
    at Frames.MiraReal.<init>(MiraReal.java:37)
    at Frames.MiraReal$2.run(MiraReal.java:177)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
CONSTRUÍDO COM SUCESSO (tempo total: 2 segundos)

我做什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

请仔细阅读Statement的Javadoc(PreparedStatement的超类)。

  

注意:当Statement对象关闭时,它的当前ResultSet对象(如果存在)也将关闭。

正在发生的事情是PreparedStatementRunQuery中声明,当该方法结束时,它超出了范围,此时ResultSet不再有效。

您无法按照当前的方式构建代码。必须在使用PreparedStatement的执行范围中定义ResultSet