gpt4 book ai didi

java - 通过Java的GUI将数据添加到数据库表中。

转载 作者:行者123 更新时间:2023-11-29 07:42:53 27 4
gpt4 key购买 nike

我在 IBM DB2 中创建了一个名为 "NAMES" 的简单表,其中包含单列“FullName”,数据类型为 VARCHAR(20) 。我还创建了一个带有 JTextfield 和 JButton 的 GUI,以通过 GUI 将数据添加到表中。单击该按钮时,文本字段中的文本将插入到表 NAMES 中。但是当我点击按钮时出现错误。这是java代码。

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

public class FirstClass extends JFrame implements ActionListener
{
private Connection connection;
private JTextField fieldTF;
private JButton addB;

public FirstClass() throws SQLException , ClassNotFoundException
{
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

fieldTF = new JTextField(20);
addB = new JButton("Add");

Container cont = this.getContentPane();
cont.setLayout(new FlowLayout());
cont.add(fieldTF);
cont.add(addB);

setConnection();

addB.addActionListener(this);

pack();
validate();
setVisible(true);
}

public void setConnection() throws SQLException , ClassNotFoundException
{
Class.forName("com.ibm.db2.jcc.DB2Driver");
connection = DriverManager.getConnection("jdbc:db2://localhost:50000/COLINN","Colinn","ezioauditore");
System.out.print("Connected Succesfully");
}

public void write(String name) throws SQLException , ClassNotFoundException
{
PreparedStatement statement = null;
String query = null;
query = "INSERT INTO NAMES VALUES (?)";

statement.setString(1,name);
statement.executeUpdate();

}

public void actionPerformed(ActionEvent e)
{
try
{
write(fieldTF.getText());
}catch(Exception ex)
{
ex.printStackTrace();
}
}

public static void main(String args[]) throws SQLException , ClassNotFoundException
{
new FirstClass();
}

}

错误如下:

java.lang.NullPointerException
at FirstYearProject.FirstClass.write(FirstClass.java:49)
at FirstYearProject.FirstClass.actionPerformed(FirstClass.java:58)
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$400(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$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.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$1.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)

我已经从 DB2 导入了外部 Jars 来建立与 Java 的连接。我认为查询有问题或者执行有问题。

最佳答案

这里有一个问题:

    PreparedStatement statement = null; // **** set to null here
String query = null;
query = "INSERT INTO NAMES VALUES (?)";

statement.setString(1,name); // **** use it here!

statement 在使用之前会在哪里变为非空?

您需要根据异常堆栈跟踪消息 FirstClass.java:49 在 FirstClass.java 类的第 49 行和第 58 行键入。

此外,您还需要了解如何调试 NPE (NullPointerException) 的一般概念。您应该严格读取异常的堆栈跟踪,以找到出错的代码行,即引发异常的行,然后仔细检查该行,找出哪个变量为空,然后追溯到您的代码以了解原因。你会一次又一次地遇到这些,相信我。

所以这里:

java.lang.NullPointerException
at FirstYearProject.FirstClass.write(FirstClass.java:49)
at FirstYearProject.FirstClass.actionPerformed(FirstClass.java:58)

堆栈跟踪告诉您仔细检查第 49 行和第 58 行。

<小时/>

请注意,您需要在尝试使用之前创建一个PreparedStatement 对象。首先,您将创建一个 Connection,然后使用该 Connection 通过 Connection 的 preparedStatement(...) 方法创建PreparedStatement。 Java JDBC Tutorials 中对此进行了详细描述。 。特别注意PreparedStatement subsection .

关于java - 通过Java的GUI将数据添加到数据库表中。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28565461/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com