所以这是我的代码:
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 :(得分:0)
查看allItems
的来源。
我怀疑发生的事情如下:
net.proteanit.sql.DbUtils
table_1.setModel(DbUtils.resultSetToTableModel(rs));
保存对resultSetToTableModel
的引用,但希望它保持有效,以便可以延迟加载数据。ResultSet
时,try
和PreparedStatement
都会超出范围。出于某种原因,ResultSet
正在收集垃圾(可能PreparedStatement
中只有弱引用?)您可能应该在顶级声明ResultSet
和PreparedStatement
,这样它们就不会超出范围。