【发布时间】:2013-01-28 15:16:50
【问题描述】:
如果没有硬编码值,我将如何编写这个 sql 语句?
resultSet = statement
.executeQuery("select * from myDatabase.myTable where name = 'john'");
// this works
有类似的东西:
String name = "john";
resultSet = statement
.executeQuery("select * from myDatabase.myTable where name =" + name);
// Unknown column 'john' in 'where clause' at
// sun.reflect.NativeConstructorAccessorImpl.newInstance0...etc...
提前谢谢..
【问题讨论】:
-
使用准备好的语句
-
人们告诉您使用prepared statements 的原因之一是因为您在第二个示例中尝试执行的操作很容易受到Sql Injection Attack 的攻击。
-
@Giwrgos_ 也许你应该打开 eclipse 并尝试一下......然后你会发现你错了......