2016-10-30 22 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); 


    } 
} 

アップOKですメッセージ:

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がガベージコレクトなっている

彼らはスコープの外に出ないようにあなたはおそらくトップレベルでPreparedStatementResultSetを宣言する必要があります(多分?ResultSetで、それへの唯一の弱参照があります)。

関連する問題