为什么MySQL会抱怨此查询中的未知列?

时间:2015-07-25 11:54:10

标签: java mysql database

我在尝试使用mysql查询时遇到以下错误

Problem in Query
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'abc123' in 'where clause'

这就是我正在使用的

public Student validate_Student(String s, String t) {

    Student obj = new Student();

    int w = Integer.parseInt(s);

        String query = "SELECT s.* FROM student s JOIN login l on s.id = l.Student_ID WHERE l.Student_ID = " + w + " and l.Password= " + t;

        try
        {
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {
                obj.setID(rs.getString("ID"));
                obj.setName(rs.getString("NAME"));
                obj.setAddress(rs.getString("ADDRESS"));
                obj.setPhone(rs.getString("PHONE_NO"));
                obj.setEmail(rs.getString("EMAIL"));
                obj.setDOB(rs.getString("DOB"));
                obj.setDegree(rs.getString("DEGREE"));

            }

        }
        catch(SQLException e)
        {
            System.out.println("Problem in Query");
            e.printStackTrace();
        }


    }
    return obj;
}

1 个答案:

答案 0 :(得分:0)

尝试替换以下行。

String query = "SELECT s.* FROM student s JOIN login l on s.id = l.Student_ID WHERE l.Student_ID = " + w + " and l.Password = '" + t +"'";

更新:在上面的示例中,您应该使用PrepareStatement代替Statement,这将有助于您轻松处理此类情况。

此外,还有更多Advantages of Prepare Statement

Prepare Statement Demo

我希望它有所帮助。