How to use MySQL like operator in JDBC?
2014-04-05 23:00
393 查看
I have the following syntax in my code, but it is not working when I am trying to use the
in JDBC. It works fine in this way, when it is just equal:
But if I want to use the
to search as a wildcard, I keep getting the error saying that "%" is not a valid character. How I can correctly use the LIKE operator?
From
the comments:
This does not compile. Assuming that
a variable, then it should rather look like
(pay attention to the syntax highlighting, the
to be part of the SQL string!)
However, concatenating user-controlled string variables like that in a SQL query puts doors wide open for successful SQL
injection attacks. Learn how
to use
use it instead.
from
StackOverFlow
End
LIKEoperator
in JDBC. It works fine in this way, when it is just equal:
ResultSet resultSet = statement.executeQuery("SELECT * FROM drawings WHERE name = '"+ DT +"'");
But if I want to use the
LIKEoperator
to search as a wildcard, I keep getting the error saying that "%" is not a valid character. How I can correctly use the LIKE operator?
From
the comments:
query=("SELECT * FROM drawings WHERE name LIKE '"%DT%"'");
This does not compile. Assuming that
DTis
a variable, then it should rather look like
query = "SELECT * FROM drawings WHERE name LIKE '%" + DT + "%'";
(pay attention to the syntax highlighting, the
%has
to be part of the SQL string!)
However, concatenating user-controlled string variables like that in a SQL query puts doors wide open for successful SQL
injection attacks. Learn how
to use
PreparedStatementand
use it instead.
String sql = "SELECT * FROM drawings WHERE name LIKE ?"; // ... preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, "%" + DT + "%"); resultSet = preparedStatement.executeQuery(); // ...
from
StackOverFlow
End
相关文章推荐
- How to use iBatis/NHibernate in medium trust/partial trust environments like Mosso
- How to install MySQL JDBC driver in Eclipse web project without java.lang.ClassNotFoundexception com
- How To Use MySQL Easily In PHP
- How-to use MySQL-python in Python
- (转贴)How to use JDBC and transactions in Tomcat with JOTM
- How to use NoSQL in MySQL
- How to Use a Function or a Procedure as a Parameter in another Function
- HOW TO: Change the Owner of a User-Defined Data Type That Is in Use in SQL Server 2000
- iOS 后台定位审核被拒How to clarify the purpose of its use in the locatio
- How To Use Cookies in an ASP Page
- How to use PL SQL Developer in Ubuntu
- Virtual Network (1) - How to use it in a guest
- How to Use Cocoa Bindings and Core Data in a Mac App
- How to use, monitor, and disable transparent hugepages in Red Hat Enterprise Linux 6
- How to use a reverse shell in Metasploit
- [zz]How to use *args and **kwargs in Python
- 【架构】How To Use HAProxy to Set Up MySQL Load Balancing
- How to use UTF-8 in Python
- how to use the curses library in unix?
- How to use plugin audio codecs in OpenH323 and OPAL