SQL错误ORA-00933

时间:2012-07-27 07:02:15

标签: java sql oracle jdbc

我正在执行这样的声明

       ResultSet res =
            stmt.executeQuery("SELECT FIRST_NAME FROM  USER vu, USER_PROFILE vup"+
                              "WHERE vup.USER_ID = vu.USER_ID  "+
                              "AND vu.USER_ID = ( SELECT USER_ID  "+
                             " FROM USER"+ 
                             " WHERE EMAIL_ID = '"+userName+"'"+ 
                             "AND PW_ENCRYPTED = '"+password+"')");

但是它会返回一个像这样的错误

java.sql.SQLSyntaxErrorException:ORA-00933:SQL命令未正确结束

3 个答案:

答案 0 :(得分:2)

您需要在vupWHERE之间添加空格:

" WHERE vup.USER_ID = vu.USER_ID  " +

答案 1 :(得分:1)

在查询行的开头和结尾添加空格。如下所示

ResultSet res =
            stmt.executeQuery(" SELECT FIRST_NAME FROM  USER vu, USER_PROFILE vup "+
                              " WHERE vup.USER_ID = vu.USER_ID "+
                              "AND vu.USER_ID = ( SELECT USER_ID "+
                             " FROM USER "+ 
                             " WHERE EMAIL_ID = '"+userName+"' "+ 
                             " AND PW_ENCRYPTED = '"+password+"')");

答案 2 :(得分:1)

首先,使用PreparedStatement,其次,您在vupWHERE之间缺少空格字符。

它应该是这样的:

String sql = "SELECT FIRST_NAME FROM  USER vu, USER_PROFILE vup"
        + " WHERE vup.USER_ID = vu.USER_ID AND vu.USER_ID = "
        + "(SELECT USER_ID FROM USER WHERE EMAIL_ID = ? AND PW_ENCRYPTED = ?)";

ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, password);

rs = ps.executeQuery();
if (rs.next()) { }