0

我尝试运行此 mysql 查询,但出现此错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 'SET @r2 = GeomFromText('Polygon((1 1,5 1,5 5,1 5,1 1))'); 附近使用正确的语法;在第 2 行选择 MBROVERLAPS(@r1'

代码:

public String getoverlap(Rectangle recA, Rectangle recB){
    String result = "-1";
    String query;

    query =    
            "SET @r1 = GeomFromText(\'Polygon((1 1, 5 1,5 5,1 5,1 1))\');\n" +
            "SET @r2 = GeomFromText(\'Polygon((1 1,5 1,5 5,1 5,1 1))\');\n" +
           "select MBROVERLAPS(@r1,@r2);" ;

     System.out.println("query--:\n "+query);


     try {
        this.statement = this.connection.createStatement();
        ResultSet  rs = this.statement.executeQuery(query);
        System.out.println("kkk:" +rs.toString());

        if(rs!=null){
            result = rs.getString("MBROVERLAPS(@r1,@r2)");
        }
        else{
            System.out.println("Debug: lathos sthn getoverlap");
        }

         statement.close();
    } catch (SQLException ex) {
        System.out.println("debug: sqlEcxeption");
        Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
    }

     return result;
}

哪里有问题?

4

1 回答 1

1

JDBC API 不模拟您的典型 SQL 客户端 - 您不能以您拥有的方式将多个语句合并在一起。具体来说,这个查询:

query =    
    "SET @r1 = GeomFromText(\'Polygon((1 1, 5 1,5 5,1 5,1 1))\');\n" +
    "SET @r2 = GeomFromText(\'Polygon((1 1,5 1,5 5,1 5,1 1))\');\n" +
    "select MBROVERLAPS(@r1,@r2);" ;

需要拆分,每个语句单独运行。如图所示,通过语句直接运行每个部分:

statement = connection.createStatement();
statement.executeQuery("SET @r1 = GeomFromText(\'Polygon((1 1, 5 1,5 5,1 5,1 1))\')";
statement.executeQuery("SET @r2 = GeomFromText(\'Polygon((1 1,5 1,5 5,1 5,1 1))\')");
ResultSet  rs = statement.executeQuery("SELECT MBROVERLAPS(@r1,@r2)");
System.out.println(rs.toString());

A dead giveaway is that you should never have delimiting semi-colons in your JDBC query strings.

于 2013-03-30T20:28:24.770 回答