【问题标题】:MYSQL query using a session user in JAVA在 JAVA 中使用会话用户的 MYSQL 查询
【发布时间】:2016-10-18 02:59:22
【问题描述】:

我正在尝试从 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 之间的比较,但我应该怎么做呢?有一些特殊的性格?提前致谢。

【问题讨论】:

  • 你的连接错误;试试这个+ " AND u.id = '" + name + "'"

标签: java mysql sql eclipse jdbc


【解决方案1】:

你能用下面的sn-p吗:

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";

【讨论】:

  • ( cc: @Gera ) ... 当name = "O'Neill" ...时会发生什么?
  • @GordThompson 你能看看我编辑的答案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-10
  • 2019-09-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-02
  • 2014-03-12
  • 1970-01-01
相关资源
最近更新 更多