如何将 TextFields 中的数据插入 SQLite 数据库?

How to Insert Data from TextFields into SQLite Database?

我的应用程序中有一个表格需要填写,其中输入的内容需要插入到名为 BookJ 的 .sqlite table 中。

这是我的代码示例,我使用了以前学到的方法,但没有得到我想要的结果,而是得到了错误 "java.sql.SQLException: Query does not return results"

Connection connection = null;

import java.sql.*;
import javax.swing.*;

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

import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PopulateTables extends JFrame {

private JPanel contentPane;

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

Connection connection = null;

private JTextField textFieldSName;
private JTextField textFieldSSName;
private JTextField textFieldAName;
private JTextField textFieldASName;
private JTextField textFieldBookT;
private JTextField textFieldGenre;
private JTextField textField;

/**
* Create the frame
*/
public PopulateTables() {

connection = SqlConnection.dbConnector1();

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(350, 350, 700, 550);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    tabbedPane.setBounds(6, 43, 688, 479);
    contentPane.add(tabbedPane);

    JPanel panel = new JPanel();
    tabbedPane.addTab("New Reading Journal Entry", null, panel, null);
    panel.setLayout(null);

    JLabel lblYourName = new JLabel("Your Name: ");
    lblYourName.setBounds(77, 26, 91, 16);
    panel.add(lblYourName);

    textFieldSName = new JTextField();
    textFieldSName.setBounds(46, 46, 151, 37);
    panel.add(textFieldSName);
    textFieldSName.setColumns(10);

    JLabel lblYourSurname = new JLabel("Your Surname: ");
    lblYourSurname.setBounds(67, 95, 130, 16);
    panel.add(lblYourSurname);

    textFieldSSName = new JTextField();
    textFieldSSName.setColumns(10);
    textFieldSSName.setBounds(46, 113, 151, 37);
    panel.add(textFieldSSName);

    JLabel lblAuthorsFirstName = new JLabel("Author's First Name:");
    lblAuthorsFirstName.setBounds(465, 26, 161, 16);
    panel.add(lblAuthorsFirstName);

    JLabel lblAuthorsLastName = new JLabel("Author's Last Name:");
    lblAuthorsLastName.setBounds(465, 95, 161, 16);
    panel.add(lblAuthorsLastName);

    textFieldAName = new JTextField();
    textFieldAName.setColumns(10);
    textFieldAName.setBounds(461, 46, 151, 37);
    panel.add(textFieldAName);

    textFieldASName = new JTextField();
    textFieldASName.setColumns(10);
    textFieldASName.setBounds(461, 113, 151, 37);
    panel.add(textFieldASName);

    JLabel lblTitleOfBook = new JLabel("Title of Book: ");
    lblTitleOfBook.setBounds(67, 188, 91, 16);
    panel.add(lblTitleOfBook);

    textFieldBookT = new JTextField();
    textFieldBookT.setBounds(46, 216, 151, 37);
    panel.add(textFieldBookT);
    textFieldBookT.setColumns(10);

    JLabel lblGenre = new JLabel("Genre:");
    lblGenre.setBounds(77, 276, 61, 16);
    panel.add(lblGenre);

    textFieldGenre = new JTextField();
    textFieldGenre.setColumns(10);
    textFieldGenre.setBounds(46, 304, 151, 37);
    panel.add(textFieldGenre);

    JLabel lblEntrywriteA = new JLabel("Write a couple words about what you thought");
    lblEntrywriteA.setBounds(323, 188, 303, 16);
    panel.add(lblEntrywriteA);

    JLabel lblNewLabel = new JLabel("the book was like:");
    lblNewLabel.setBounds(323, 202, 289, 16);
    panel.add(lblNewLabel);

    textField = new JTextField();
    textField.setBounds(323, 221, 289, 120);
    panel.add(textField);
    textField.setColumns(10);

    JButton btnEnter = new JButton("Enter ");
    btnEnter.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
            String journalquery = "insert into BookJ (StudentFirst, StudentLast, TitleOfBook, AuthorFirst, AuthorLast, Genre, Entry) values (?, ?, ?, ?, ?, ?, ?)";
            PreparedStatement journal = connection.prepareStatement(journalquery);

            journal.setString(1, textFieldSName.getText());
            journal.setString(2, textFieldSSName.getText());
            journal.setString(3, textFieldBookT.getText());
            journal.setString(4, textFieldAName.getText());
            journal.setString(5, textFieldASName.getText());
            journal.setString(6, textFieldGenre.getText());
            journal.setString(7, textField.getText());


            journal.executeQuery();

            JOptionPane.showMessageDialog(null, "Entry Saved!");
            }
            catch(Exception e3){
                JOptionPane.showMessageDialog(null, e3);    
            }

        }
    });
    btnEnter.setBounds(273, 378, 117, 29);
    panel.add(btnEnter);

    JPanel panel_1 = new JPanel();
    tabbedPane.addTab("New Book Recommendation", null, panel_1, null);
}
}

抱歉,代码太长了,我只是觉得我需要包含所有内容。顺便说一句,我与 .sqlite 数据库的实际连接是正确的并且始终有效(它在不同的 class 中)。我需要在那里有一个 ResultSet 吗?如果我这样做,我该如何使用它?

谢谢!

这是堆栈跟踪:

java.sql.SQLException: Query does not return results
at org.sqlite.jdbc3.JDBC3PreparedStatement.executeQuery(JDBC3PreparedStatement.java:68)
at version1.PopulateTables.actionPerformed(PopulateTables.java:149)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:709)
at java.awt.EventQueue.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue.run(EventQueue.java:731)
at java.awt.EventQueue.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
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)
journal.executeQuery();

您的 SQL 没有查询数据库。

您正在尝试 "update" 数据库,因此您需要使用:

journal.executeUpdate();