gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:00:27 26 4
gpt4 key购买 nike

首先,我在程序中创建了一个搜索函数,并在将数据添加到数据库时实现了相同的逻辑,但搜索函数有效,而添加函数无效(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/

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