检查表中已存在的值的问题

时间:2017-08-14 12:27:42

标签: mysql sql

我想检查我的3个文本字段中的值已经存在于我的表中,然后在我的语句中继续进行,但它在eclipse中不起作用但在heidisql中工作? prepareStatement和resultset在顶部是正确的。

我已经尝试过使用了?和preStatement.setString(1,get_status_update)但它给了我相同的错误结果。

String get_status_update = stats_status_txta.getText();
        String get_username= friend_username_txtf.getText();
        String get_friendname= friend_list_txta.getText();

        DB_connection db_connection = new DB_connection(); 

            try{

                if(get_username.length() < 3) {

                    JOptionPane.showMessageDialog(statusPanel,"You need to insert a username here","Post Lodge Status Error", JOptionPane.INFORMATION_MESSAGE);

                } else if (get_status_update.length() < 3) {

                    JOptionPane.showMessageDialog(statusPanel,"Please insert the status update you wish to like","Post Lodge Status Error", JOptionPane.INFORMATION_MESSAGE);

                } else {

                    String sql = " SELECT sut.status_update_id, up.user_id , fd.friend_id "
                            + "FROM tb_logde_status_update as tsu "
                            + "INNER JOIN status_update_tb as sut ON sut.status_update_id = tsu.status_update_id "
                            + "INNER JOIN user_profile as up ON tsu.user_id = up.user_id "
                            + "INNER JOIN friend_details as fd ON tsu.friend_id = fd.friend_id "
                            + "WHERE sut.status_update_msg='"+get_status_update+"' AND up.username='"+get_username+"' AND fd.friend_username='"+get_friendname+"' ";

                    preStatement = db_connection.connect().prepareStatement(sql);

                    res = preStatement.executeQuery();

                    boolean send_logde_status = false;

                            while (res.next()) {
                                if(res.getString("status_update_msg").equalsIgnoreCase(get_status_update) 
                                        && res.getString("username").equalsIgnoreCase(get_username) 
                                        && res.getString("friend_username").equalsIgnoreCase(get_friendname)) {

                                    JOptionPane.showMessageDialog(statusPanel, "Status update has already been like by you, choose another one!");
                                    send_logde_status = true;

                                    break; // Get out of the loop. No more need for it.

//也许错误就在这里?

        } 
    } ...

当我直接从数据库查询使用查询时,它可以工作!

SELECT sut.status_update_id, up.user_id , fd.friend_id
FROM tb_logde_status_update as tsu
INNER JOIN status_update_tb as sut ON sut.status_update_id = tsu.status_update_id
INNER JOIN user_profile as up ON tsu.user_id = up.user_id
INNER JOIN friend_details as fd ON tsu.friend_id = fd.friend_id
WHERE sut.status_update_msg="not hallo world" AND up.username="username" AND fd.friend_username="paul";

我正在插入3个文本字段中的确切值。当我使用数据库中的查询而不是我的动作监听器按钮时,我收到status_update_id,user_id,friend_id

错误:找不到列'status_update_msg'。

1 个答案:

答案 0 :(得分:0)

可能参数与直接查询中传递的参数不完全相同,我建议如下:

  • 在控制台
  • 中记录参数值
  • 如果参数值只需小写,请尝试使用trim()和/或toLowerCase()

此外,对setXXX()使用preparedStatement方法比将参数值附加到String查询更好。