如何在插入到(SQLite)數據庫之前檢查數據是否存在。我目前使用Java作爲平臺。如何在插入到(SQLite)數據庫之前檢查數據是否存在
Statement stmt = conn.createStatement();
String query ="SELECT * FROM tb_hall Where Activity_Name'"+txtTime+"'";
ResultSet rs = stmt.executeQuery(query);
if(rs.next()) {
String sql = "Insert into tb_hall (Activity_Name, Organization, Venue, Date, Time) values(?, ?, ?, ?, ?)";
pst = conn.prepareStatement(sql);
pst.setString(1, txtAct.getText());
pst.setString(2, txtOrg.getText());
pst.setString(3, rgbGroup.getSelection().getActionCommand());
pst.setString(4, ((JTextField) date.getDateEditor().getUiComponent()).getText());
pst.setString(5, txtTime.getText());
pst.execute();
JOptionPane.showMessageDialog(null, "Saved!");
txtAct.setText(null);
txtOrg.setText(null);
rgbGroup.clearSelection();
date.setDate(null);
txtTime.setText(null);
// ...
使用SELECT。除了[可能的SQL注入](http://bobby-tables.com/)你當前的代碼有什麼問題? –
嗨,不幸的是,它顯示了一個非常長的錯誤消息 – jjang
我明白了,因爲我們大多數人看不到你的屏幕,你可以複製/粘貼消息在你的問題([編輯](http://stackoverflow.com/posts/) 43026437 /編輯)它) –