java - DB2 和 Java。通过 GUI 将数据添加到数据库。

标签 java database user-interface db2

首先,我在程序中创建了一个搜索函数,并在将数据添加到数据库时实现了相同的逻辑,但搜索函数有效,而添加函数无效(SQLException)。我从 DB2 创建了一个名为 Names 的表,其中只有一列 FullName。您是否仍需要创建新查询来将数据添加到表中?或不?我想通过 GUI 将数据添加到数据库中。

这是我的 Java 代码:

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

public class ConnectAndSearchDB extends JFrame implements ActionListener
{
    private JTextField fieldSearch,fieldAdd;
    private JButton searchB,addB;
    private Connection connection;
    private String name;
    private ResultSet rs,rs1;

    public ConnectAndSearchDB() throws SQLException,ClassNotFoundException
    {
        setLocationRelativeTo(null);
        setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        setLayout(new GridLayout(2,2));

        fieldSearch = new JTextField(20);
        searchB = new JButton("Search");
        fieldAdd = new JTextField(20);
        addB = new JButton("Add");

        add(searchB);
        add(fieldSearch);
        add(addB);
        add(fieldAdd);

        searchB.addActionListener(this);
        addB.addActionListener(this);

        establishConnection();

        pack();

        setResizable(false);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        Object act = e.getSource();

        if(act.equals(searchB))
        {
            name = fieldSearch.getText();
            searchData();
        }else if(act.equals(addB))
        {
            try {
                addData();
            } catch (ClassNotFoundException e1)
            {
                e1.printStackTrace();
                System.out.println("ClassNotFound");
            } catch (SQLException e1)
            {   
                e1.printStackTrace();
                System.out.println("SQLError");
            }
        }   
    }

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


    private void searchData()
    {
        try
        {
            PreparedStatement s = null;
            String query;
            query = "SELECT * from NAMES";

            s=connection.prepareStatement(query);
            rs = s.executeQuery();

            boolean matchfound = false;

            while(rs.next())
            {
                if(rs.getString(1).equals(name))
                {
                    matchfound = true;
                    System.out.println("The name "+name+" is found in the Database");
                    break;
                }
            }

            if(matchfound == false)
            {
                System.out.println("Match Not Found");
            }   
        }
        catch(SQLException e)
        {
            e.printStackTrace();

        }
    }

    public void addData() throws ClassNotFoundException,SQLException
    {
        PreparedStatement ps = null;
        String query;
        query = "INSERT INTO NAMES VALUES('"+fieldAdd.getText()+"')";

        ps = connection.prepareStatement(query);
        rs1 = ps.executeQuery();

        System.out.println("Written Successfully");
    }

    public static void main (String args[]) throws SQLException,ClassNotFoundException
    {
        EventQueue.invokeLater(new Runnable() 
        {
            public void run()
            {
                try 
                {
                    new ConnectAndSearchDB();

                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}

这是名称表:

DB2 Table names Names

最佳答案

我高度怀疑使用rs1 = ps.executeQuery();插入/更新数据库是您的问题的过程,您应该使用 int count = ps.executeUpdate();

参见 PreparedStatement#executeUpdate 了解更多详情

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

相关文章:

java - 接收焦点时清除文本框

java - 测量 Web 应用程序 session 资源消耗

java - 使用基于字段的流计算 Java 8 中的重复项

database - 如何防止数据库docker被重建而丢失生产数据

mysql - 静态资源的访问控制

java - 线性布局与另一个线性布局重叠

java - PHP 在 Ubuntu 上运行 jar 文件

java - 这个自定义接口(interface)实现线程安全吗

Mysql查询小知识

ios - 向下滚动时如何隐藏导航栏?