首先,我在程序中创建了一个搜索功能,我在将数据添加到数据库时实现了相同的逻辑,但搜索功能正常,而添加功能没有(SQLException)。我创建了一个名为名称的 DB2 表,只有一列 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();
}
}
});
}
}
以下是姓名表:
答案 0 :(得分:3)
我非常怀疑使用rs1 = ps.executeQuery();
插入/更新数据库是您的问题,您应该使用int count = ps.executeUpdate();
有关详细信息,请参阅PreparedStatement#executeUpdate
答案 1 :(得分:2)
不是本身的答案,而是关于整体方法的一些问题的一系列扩展评论......
如果你打开它,你应该关闭它......
String query = "SELECT * from NAMES";
try (PreparedStatement stmt = connection.prepareStatement(query)) {
try (ResultSet 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();
}
有关详细信息,请参阅The try-with-resources Statement ...
我还会重新考虑使用单个Connection
,而Connection
池可能对此问题有点过分,您只需根据需要打开和关闭Connection
,但请记住,这样做会产生一些开销......
String query = "SELECT count(*) from NAMES where name=?";
try (PreparedStatement stmt = connection.prepareStatement(query)) {
stmt.setString(1, name);
try (ResultSet rs = s.executeQuery()) {
boolean matchfound = false;
if (rs.next())
{
if(rs.getLong(1) > 0)
{
matchfound = true;
System.out.println("The name "+name+" is found in the Database");
}
}
if(!matchfound)
{
System.out.println("Match Not Found");
}
}
catch(SQLException e)
{
e.printStackTrace();
}
PreapredStatement
... 而不是......
query = "INSERT INTO NAMES VALUES('"+fieldAdd.getText()+"')";
使用...
query = "INSERT INTO NAMES VALUES(?)";
//...
ps.setString(1, fieldAdd.getText());