gpt4 book ai didi

java - 在 JAVA 中使用 session 用户的 MYSQL 查询

转载 作者:行者123 更新时间:2023-11-30 23:45:44 25 4
gpt4 key购买 nike

我正在尝试从 Eclipse 运行此查询:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session = request.getSession();
String name = session.getAttribute("user").toString();
String query = "SELECT DISTINCT t.text, t.user, t.date"
+ " FROM users u, tweets t, follows f"
+ " Where t.parent is null"
+ " AND u.id ="+name
+ " AND ( f.follower = u.id"
+ " AND f.followed = t.user"
+ " OR t.user = u.id)"
+ " ORDER BY t.date DESC;";

但是我得到以下错误:

Sesion(user):Takeshi
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'Takeshi' in 'where clause'

我可以想象错误是因为我做错了 u.id 和 name 之间的比较,但我应该怎么做呢?有一些特殊的性格?提前致谢。

最佳答案

能否请您使用以下代码段:

String name = session.getAttribute("user").toString();
name = name.replace("\'", "''");

String query = " SELECT DISTINCT t.text, t.user, t.date"
+ " FROM users u, tweets t, follows f"
+ " Where t.parent is null"
+ " AND u.id = '"+name+"'"
+ " AND ( f.follower = u.id"
+ " AND f.followed = t.user"
+ " OR t.user = u.id)"
+ " ORDER BY t.date DESC";

关于java - 在 JAVA 中使用 session 用户的 MYSQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37861795/

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