简体   繁体   中英

Substituting value of mysql function stored in string in resultset statement using jsp

In Java I have a defined a string named year:

  String year="year(now())-1"

Now I want the value of this string to be subsituted below using where like clause:

 ResultSet rs = stmt.executeQuery("select abc from table where year like '"+year+"' " )

But using above statement I get no result. Can anyone please tell where I am going wrong?

To minus 1 year

DATE_SUB(NOW(),INTERVAL 1 YEAR)

So you can use

String year="DATE_SUB(NOW(),INTERVAL 1 YEAR)"

Final:

ResultSet rs = stmt.executeQuery("select abc from table where year = "+year+"");

Have you tried using the comparison operator = instead of like? Also, have you checked the input parameter in relation to the data? Log the query and see what is actually being generated.

尝试使用%在你喜欢的地方:

stmt.executeQuery("select abc from table where year like '%"+year+"%' " )

包括%in ...

ResultSet rs = stmt.executeQuery("select abc from table where year like '%"+year+"%' " )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM