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

时间:2015-02-17 15:51:49

标签: java mysql database db2

我在 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的连接。我认为执行它时查询或问题存在问题。

1 个答案:

答案 0 :(得分:2)

这里有一个问题:

    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)的一般概念。您应该批判性地将异常的堆栈跟踪读取到找到错误的代码行,抛出异常的行,然后仔细检查该行,找出哪个变量为null ,然后追溯到您的代码,看看为什么。你会一次又一次地碰到这些,相信我。

所以这里:

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