请参阅下面的更新
你正在重新发明轮子。无论您使用什么与数据库对话,都应该有某种“准备好的语句”概念。在 Java 中,它的字面意思是PreparedStatement
类,但本质上任何数据库访问系统都应该有类似的东西。使用它们是为了避免将 SQL 语句构建为字符串。例如,从概念上来说:
preparedStatement = prepare("insert into mytable (id, name) values (?, ?)")
preparedStatement.setField(0, theId);
preparedStatement.setField(1, theName);
Not:
statement = "insert into mytable (id, name) values ('" + theId + "', '" + theName + "')"
...这是要求注入攻击。
The "prepared statement" concept centralizes escaping to the database link layer, which is well-prepared to handle it. If you tell people what you're using to access your database, they'll be able to point you at the relevant mechanism. See below.
只是为了避免疑问:你这样做是为了逃避在服务器上, 正确的?我的意思是,我就是这么读的,你实际上说过“我想在服务器端清理这些......”。但以防万一您考虑在客户端执行此操作:你不能。客户端发送给您的任何东西,绝对没有任何东西是可以信任的,因为它可以被伪造。您必须在服务器端执行此操作。
Update:您曾评论过您在应用程序服务器中使用 Rhino。出色的!然后就PreparedStatement http://download.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html并让您的 JDBC 驱动程序为您处理。(对于非 Java 潜伏者:Rhino 是用于 Java VM 的 JavaScript。这太棒了。)