Search code examples
javamysqlconcatenationintellij-14

unable to concatenate variable with sql query


Here is sample code ...

    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));
    }



If String passed Instead of passing variable then it works ...like

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

I also used preparedStatement...but not working ...

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

Here is full code ....

@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);}
    }



Using : IntelliJ IDEA 14 CE

Output :
Enter key Fired
product
In dbSearch
Concat String :product
'

kindly point out my mistake ... i'm new in java... and further i need to use like and or with it .... please provide answer with explanation... Thanks in advance.


Solution

  • I haven't thought of it....

    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);}
        }
    

    Now it is fetching data properly ...