简体   繁体   中英

java.sql.SQLException: The prepared statement has been finalized

So this is my code:

package Gui;

import java.sql.*;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import net.proteanit.sql.DbUtils;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class accounts extends JFrame {

    private JPanel contentPane;
    private JTable table;
    private JTable table_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    accounts frame = new accounts();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    Connection connection = null;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JPasswordField passwordField;


    /**
     * Create the frame.
     */
    public accounts() {
        connection = sqliteConnection.dbConnector();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 700, 400);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnNewButton = new JButton("Load Accounts");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) 
            {
                try {

                    String query = "select * from Users";
                    PreparedStatement pst = connection.prepareStatement(query);
                    ResultSet rs = pst.executeQuery();
                    table_1.setModel(DbUtils.resultSetToTableModel(rs));


                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
        btnNewButton.setBounds(500, 25, 150, 30);
        contentPane.add(btnNewButton);

        table = new JTable();
        table.setBounds(100, 256, 261, -183);
        contentPane.add(table);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(250, 75, 400, 150);
        contentPane.add(scrollPane);

        table_1 = new JTable();
        scrollPane.setViewportView(table_1);

        JLabel lblNewLabel = new JLabel("UserName");
        lblNewLabel.setBounds(10, 10, 100, 20);
        contentPane.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("Password");
        lblNewLabel_1.setBounds(10, 70, 100, 20);
        contentPane.add(lblNewLabel_1);

        JLabel lblNewLabel_2 = new JLabel("Email");
        lblNewLabel_2.setBounds(10, 130, 100, 20);
        contentPane.add(lblNewLabel_2);

        JLabel lblNewLabel_3 = new JLabel("PhoneNo");
        lblNewLabel_3.setBounds(10, 190, 100, 20);
        contentPane.add(lblNewLabel_3);

        textField = new JTextField();
        textField.setBounds(10, 40, 100, 20);
        contentPane.add(textField);
        textField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(10, 100, 100, 20);
        contentPane.add(passwordField);

        textField_1 = new JTextField();
        textField_1.setBounds(10, 160, 100, 20);
        contentPane.add(textField_1);
        textField_1.setColumns(10);

        textField_2 = new JTextField();
        textField_2.setBounds(10, 220, 100, 20);
        contentPane.add(textField_2);
        textField_2.setColumns(10);

        JButton btnNewButton_1 = new JButton("Add");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) 
            {
                try
                {
                    String query = "insert into Users (NickName, Password, Email, PhoneNo) values (?, ?, ?, ?)";
                    PreparedStatement pst = connection.prepareStatement(query);
                    pst.setString(1, textField.getText() );
                    pst.setString(2, passwordField.getName() );
                    pst.setString(3, textField_1.getText() );
                    pst.setString(4, textField_2.getText() );

                    pst.execute();

                    JOptionPane.showMessageDialog(null, "Succesfully Registered");

                    pst.close();

                }catch (Exception e)
                {

                    e.printStackTrace();

                }
            }
        });
        btnNewButton_1.setBounds(130, 40, 100, 25);
        contentPane.add(btnNewButton_1);


    }
}

I am currently doing an assignment and everything is OK up until i hit the add button then i get the error message saying:

java.sql.SQLException: The prepared statement has been finalized
    at org.sqlite.core.NativeDB.throwex(NativeDB.java:429)
    at org.sqlite.core.NativeDB.reset(Native Method)
    at org.sqlite.jdbc3.JDBC3PreparedStatement.execute(JDBC3PreparedStatement.java:57)
    at Gui.accounts$3.actionPerformed(accounts.java:136)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

I cant figure out whats exactly wrong. Saw a few people before having similar issue but nothing helped me.

Thanks in advance

Look at the source for net.proteanit.sql.DbUtils .

I suspect that what is happening is the following:

  1. You do table_1.setModel(DbUtils.resultSetToTableModel(rs));
  2. resultSetToTableModel saves a reference to the ResultSet but expects it to remain valid so that the data can be lazily loaded.
  3. When control exits the try block both the PreparedStatement and ResultSet go out of scope. For some reason the PreparedStatement is getting garbage collected (maybe there's only a weak reference to it in the ResultSet ?)

You should probably declare the PreparedStatement and ResultSet at top level so they don't go out of scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM