gpt4 book ai didi

java - java中mysql的语法错误

转载 作者:太空宇宙 更新时间:2023-11-03 12:18:10 25 4
gpt4 key购买 nike

我不明白为什么 eclipse 告诉我这个查询的这个错误:

ResultSet rs = st.executeQuery("select * from '"+ value3+ "' where Name='" + value1 + "' and Password='"+ value2 + "'");

错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 ''f' where Name='f' and Password='f'' at line 1

最佳答案

1) 试试这个。

ResultSet rs = st.executeQuery("select * from `" + value3 + "` where Name='" +
value1 + "' and Password='"+ value2 + "'");

看这里:
MySQL Identifiers

标识符引号字符在 MySQL 中是反引号(“`”)。

2) 此外,由于您没有使用 PreparedStatement,如果您的姓名或
密码包含 ' 你会遇到问题。您的代码易受攻击
到 SQL 注入(inject)。

另见:
PreparedStatement
SQL injection

关于java - java中mysql的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21206865/

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