DB2和Java。通过GUI将数据添加到数据库。 [英] DB2 and Java. Adding data to database through GUI.

查看:258
本文介绍了DB2和Java。通过GUI将数据添加到数据库。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

First I create a search function in my program and I implemented the same logic in adding data into the database but the search function works and the add function didn't (SQLException). I created a table from DB2 named Names with only one column FullName. Do you still need to create a new query to add data into the table? or not? I want to add data into the database through GUI.

这是我的Java代码:

Here is my Java Code:

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();
                }
            }
        });
    }
}

这是Names表:

推荐答案

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

I highly suspect that using rs1 = ps.executeQuery(); to insert/update the database is the course of your issue, you should be using int count = ps.executeUpdate();

请参阅 PreparedStatement#executeUpdate 了解更多详情

See PreparedStatement#executeUpdate for more details

这篇关于DB2和Java。通过GUI将数据添加到数据库。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆