如何将java的字符串值传递给sql查询

时间:2017-04-06 04:00:52

标签: java mysql sql netbeans

我试图在Java Netbeans中创建这个SQL查询:

=COUNTIF(B2:D2,"Your Condition" )>0

我正在使用这样的预备声明:

Sub Quicker()

Dim rng1 As Range
Dim DeleteStr As String
Dim rng2 As Range

Set rng1 = Application.InputBox("Range :", xTitleId, Selection.Address, , , , , 8)
DeleteStr = Application.InputBox("Delete Text", "Delete", , , , , , 2)

With rng1.Columns(1).Offset(0, rng1.Columns.Count)
     .EntireColumn.Insert
     .FormulaR1C1 = "=COUNTIF(RC[-" & rng1.Columns.Count + 1 & "]:RC[-2],""" & DeleteStr & """ )>0"
     .AutoFilter Field:=1, Criteria1:="FALSE"
     'preserve header row    
     .Offset(1, 0).Resize(rng1.Rows.Count - 1, 1).EntireRow.Delete
      On Error Resume Next
     .EntireColumn.Delete
     On Error GoTo 0
End With

End Sub

问题在于" setString"根本不起作用。 我发现"'"的价值实际上是" \'"所以查询变为

SELECT * FROM DBNAME WHERE NAME LIKE '%SOMETHING%'

1 个答案:

答案 0 :(得分:0)

试试这个:

doSomething

我在我的代码上尝试这个,这是有用的。 我使用Spring boot + mybatis + postgresql

SQL表&一些数据:

String sql = "select * from tablename like ?";
String sqlParasValue = "%" + "2015-03-23" + "%";
pStmt =conn.prepareStatement(sql);
pStmt.setString(1, sqlParasValue);//Automatically add single quotes
pStmt.execute();

sql查询:

name   h    w    f
xi  182  53  li
1xi 181  52  [NULL]
li  180  51  xh
zs  165  50  li
xh  190  52  [NULL]

sql查询结果:

select name from stu where name like '%x%'

java代码

name    h   w   f
xi  182 53  li
1xi 181 52  [NULL]
xh  190 52  [NULL]

console:

@Select("select name  from stu where name like #{name}")
List<String> test(@Param("name") String name);
List<String> s = serviceImpl.test("%x%");
for(String ss : s){
    System.out.println(ss);
}