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

时间:2017-06-19 13:27:22

标签: java eclipse swing sqlite windowbuilder

我有一个表单,在我的应用程序中,需要填写,内部输入的内容需要插入名为BookJ的.sqlite表中。

以下是我的代码示例,我使用了之前学过的方法,但是没有得到我想要的内容我得到错误“java.sql.SQLException:Query不返回结果”

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数据库的实际连接是正确的,并且始终有效(它在不同的类中)。我需要在那里有ResultSet吗?如果我这样做,我该如何使用它?

谢谢!

这是堆栈跟踪:

java.sql.SQLException: Query does not return results
at org.sqlite.jdbc3.JDBC3PreparedStatement.executeQuery(JDBC3PreparedStatement.java:68)
at version1.PopulateTables$2.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.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.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$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.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)

1 个答案:

答案 0 :(得分:2)

Jun 19, 2017 7:28:11 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/BuseetaCorporate] threw exception [An exception occurred processing JSP page /WEB-INF/views/onLoadAddDriverView.jsp at line 271

268:                                        <c:forEach var="domainOrGroupParentDetails"
269:                                            items="${ModelObjectKeys.allDomainOrGroupParentDetails}" varStatus="status">
270:                                            <option
271:                                                value="${domainOrGroupParentDetails.domainOrGroupParentPojo.name}">
272:                                                ${domainOrGroupParentDetails.domainOrGroupParentPojo.name}</option>
273:                                        </c:forEach>
274:                                    </select>
Stacktrace:] with root cause
javax.el.PropertyNotFoundException: Property 'domainOrGroupParentPojo' not found on type java.lang.String
    at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:268)

您的SQL不查询数据库。

您正在尝试“更新”数据库,因此您需要使用:

journal.executeQuery();