无法使用sql查询连接变量

时间:2015-09-24 08:59:32

标签: java mysql concatenation intellij-14

以下是示例代码...

    Statement stmt = con.createStatement();
    String query = "select * from work_product where product_name ='" + ch + "' ";
System.out.println(query); // displaying only `
    ResultSet rs = stmt.executeQuery(query);
    System.out.println(query);
    while (rs.next()){
    System.out.println(rs.getInt(1)+" "+rs.getString(2));
    }


如果String传递而不是传递变量那么它就可以工作......比如

ResultSet rs = stmt.executeQuery("select * from work_product where product_name ='product' ");

我也使用了preparedStatement ......但没有工作......

PreparedStatement statement = con.prepareStatement("select * from work_thing_db.work_product where product_name = ? ");
statement.setString(1,ch);

这是完整的代码....

@FXML protected void keyReleased(KeyEvent evt)throws Exception {
        //SetTimer();

        if (evt.getCode() != KeyCode.BACK_SPACE) {
            String ch = evt.getText();
            //runThread();
            concateString = concateString + ch; //concateString has scope
            if (evt.getCode() == KeyCode.ENTER) {
                System.out.println("Enter Key Fired ");
                System.out.println(concateString);
                dbSearch(concateString);
            }
        }
}
private void dbSearch(String ch){
        System.out.println("In dbSearch");
        System.out.println("Concate String :"+ch);
        String query = "select * from work_product where product_name ='" + ch + "' ";
        System.out.println("Query is :"+query);
        dbConnector conn = new dbConnector();
        Connection con = conn.dbConnection();
        try {

            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(query);

            while (rs.next()){
                System.out.println(rs.getString(1)+" "+rs.getString(2));
            }
        }catch(Exception e){System.out.println(e);}
    }



使用:IntelliJ IDEA 14 CE

输出:
输入键拨打
    产品
    在dbSearch中     Concat String:产品
    '

请善意指出我的错误...我是java的新手......而且我还需要使用likeor ....请提供解释和解释...提前致谢。

3 个答案:

答案 0 :(得分:0)

数据中可能没有匹配项。您可以先运行非筛​​选查询,然后查看表中对product_name的值。

答案 1 :(得分:0)

连接字符串

的方式有问题
    if (evt.getCode() != KeyCode.BACK_SPACE) {
        String ch = evt.getText();
        //runThread();
        concateString = concateString + ch; //concateString has scope
        if (evt.getCode() == KeyCode.ENTER) {
            System.out.println("Enter Key Fired ");
            System.out.println(concateString);
            dbSearch(concateString);
        }
    }

所以当用户输入" ENTER" (可以是\ n或\ r)您将在键值的条件之前连接,因此concateString将始终包含您的字符串+" ENTER" (即回车)。这就是你打印查询时只能得到报价的原因

不要修改你的大部分代码,你可以

    if (evt.getCode() != KeyCode.BACK_SPACE) {
        String ch = evt.getText();
        //runThread();
        if (evt.getCode() == KeyCode.ENTER) {
            System.out.println("Enter Key Fired ");
            System.out.println(concateString);
            dbSearch(concateString);
        } else {
            concateString = concateString + ch; //concateString has scope
        }
    }

所以你将传递正确的字符串

答案 2 :(得分:0)

我还没有想到它......

private void dbSearch(String ch){
        System.out.println("In dbSearch");
        System.out.println("Concate String :"+ch);
        ch = ch.trim().toString(); // trim and type cast ... its working
        String query = "select * from work_product where product_name ='" + ch + "' ";
        System.out.println("Query is :"+query);
        dbConnector conn = new dbConnector();
        Connection con = conn.dbConnection();
        try {

            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(query);

            while (rs.next()){
                System.out.println(rs.getString(1)+" "+rs.getString(2));
            }
        }catch(Exception e){System.out.println(e);}
    }

现在它正在正确地获取数据......