java将txt文件存到DB2,DB2和Java。通过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.

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

}

}

});

}

}

Here is the Names table:

JXpIu.png

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值