2016-10-30 56 views
0

所以這是我的代碼:我目前在做值java.sql.SQLException:預處理語句已經敲定

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); 


    } 
} 

一個任務,並一切正常,直到我打的添加按鈕,然後我得到的錯誤消息說:

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) 

我無法弄清楚什麼是完全錯誤的。在遇到類似問題之前先看到幾個人,但沒有任何幫助。

在此先感謝

+0

這似乎是SQLite JDBC驅動程序中的一個錯誤。您可能正在收到違反約束的例外情況,但SQLite JDBC驅動程序似乎在處理此問題時自行跳過。看到類似的問題:http://stackoverflow.com/questions/40184674/sqlite-preparedstatement-finalized –

回答

0

看看源爲net.proteanit.sql.DbUtils

我懷疑是正在發生的事情是這樣的:

  1. 你做table_1.setModel(DbUtils.resultSetToTableModel(rs));
  2. resultSetToTableModel保存到ResultSet參考,但希望它仍然有效,這樣的數據可以被延遲加載。
  3. 當控制權退出try塊時,PreparedStatementResultSet超出範圍。出於某種原因,PreparedStatement被垃圾收集得到(也許有隻是一個弱引用時在ResultSet?)

你或許應該申報PreparedStatementResultSet在頂層,使他們不超出範圍。