gpt4 book ai didi

java - 从表中选择 *,其中 = "string with linebreaks"

转载 作者:行者123 更新时间:2023-11-29 07:48:57 25 4
gpt4 key购买 nike

我正在使用 Java 和 MySQL,我必须从数据库中选择这样的东西:

String sql = "SELECT type FROM test_case where input=" + inputTestCase;

问题是 inputTestCase 字符串有时会有换行符。在这些情况下,我得到一个异常(exception),例如:

You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near '5
8
10' at line 2

哪里:

'5
8
10'

是我的inputTestCase

我应该怎么做才能使 Select 正常工作。谢谢大家的帮助!

P.S: 我没有初始化 inputTestCase 。该字符串的值来自数据库。

最佳答案

如果您使用的是 JDBC,在查询中包含动态数据时,始终总是使用 PreparedStatement。如果不这样做,您很容易受到 SQL Injection 的攻击。攻击,恶意用户可以在您的数据库上执行任意 SQL 命令。

作为对此的奖励,使用 PreparedStatement 将为您转义您的输入,因此您不必像本例那样担心对其进行清理。

有关详细信息,请查看 Java 教程 - Using Prepared Statements .

关于java - 从表中选择 *,其中 = "string with linebreaks",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22851221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com