如何将值传递给?包含在java中的单引号内

时间:2013-12-01 03:24:58

标签: java

我有一个文本框,用户在其中输入日期,如29-NOV-2013。

我希望转换以下字符串:

String sql = "select * from review where creationdate like '?%' order by creationdate desc";

(进)

String sql = "select * from review where creationdate like '29-NOV-2013%' order by creationdate desc";

使用参数占位符。 我该怎么做?

2 个答案:

答案 0 :(得分:1)

PreparedStatement pstmt = 
     con.prepareStatement("select * from review where creationdate like ? order by creationdate desc");
pstmt.setString(1, "29-NOV-2013%")

答案 1 :(得分:0)

您需要使用setString(position,value) PreparedStatement来设置占位符?的值。

 String sql = "select * from review where creationdate like '?%' 
               order by creationdate desc";

PreparedStatement pstmt=con.createPreparedStatement(sql);
pstm.setString(1,textbox.getText());
                    //It will get the value from textbox e.g. 29-NOV-2013