MySQL MBRContains 语句抛出 MySQLExceptionError
MySQL MBRContains statement throwing MySQLExceptionError
我正在做一个 Java 项目,我们使用它的 sql 库函数从 MySQL 服务器访问和查询。
出现问题的代码是:
String setBox = "SET @box =
'Polygon((" + lx + " " + ry + ","
+ rx + " " + ry + ","
+ lx + " " + ly + ","
+ rx + " " + ly + ","
+ lx + " " + ry + "))';\n";
ResultSet regionResult = stmt.executeQuery(setBox +
"SELECT ItemID FROM ItemPoint WHERE MBRContains(GeomFromText(@box), Coords);\n");
错误发生在第二条语句(ResultSet regionResult = ...)
我收到的错误是:
"com.mysql.jdbc.exception.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to user near 'SELECT ItemID FROM ItemPoint WHERE MBRContains((GeomFromText(@box), Coords)' at line 2"
我不确定为什么会出现语法错误,因为我是基于它:http://dev.mysql.com/doc/refman/5.5/en/using-spatial-indexes.html。
我的MySQL服务器版本是:5.5.40-0ubuntu0.14.04.1 (Ubuntu)
你的问题不是MySql,是PreparedStatement
。你不能用它来 运行 两个语句,你必须使用 CallableStatement
对于您的问题,它将类似于:
String callString = "{ call SET @box = 'Polygon(( ? ?,
? ?,
? ?,
? ?,
? ?))';
SELECT ItemID
FROM ItemPoint
WHERE MBRContains(GeomFromText(@box), Coords); }";
CallableStatement callableStatement = con.prepareCall(callString);
callableStatement.setInt(1, lx);
callableStatement.setInt(2, ry);
callableStatement.setInt(3, rx);
callableStatement.setInt(4, ry);
callableStatement.setInt(5, lx);
callableStatement.setInt(6, ly);
callableStatement.setInt(7, rx);
callableStatement.setInt(8, ly);
callableStatement.setInt(9, lx);
callableStatement.setInt(10, ry);
ResultSet regionResult = callableStatement.executeQuery();
请注意,这可能无法正常工作。你可能需要改变一些东西,这是基本的想法。我是凭记忆做的,所以...
我可以通过这样做来让它工作:
String box = "Polygon(("
+ lx + " " + ry + ", "
+ rx + " " + ry + ", "
+ rx + " " + ly + ", "
+ lx + " " + ly + ", "
+ lx + " " + ry + "))";
ResultSet regionResult = stmt.executeQuery(//setBox +
"SELECT ItemID FROM ItemPoint WHERE MBRContains(GeomFromText(' " + box + " '), Coords);\n");
我正在做一个 Java 项目,我们使用它的 sql 库函数从 MySQL 服务器访问和查询。
出现问题的代码是:
String setBox = "SET @box =
'Polygon((" + lx + " " + ry + ","
+ rx + " " + ry + ","
+ lx + " " + ly + ","
+ rx + " " + ly + ","
+ lx + " " + ry + "))';\n";
ResultSet regionResult = stmt.executeQuery(setBox +
"SELECT ItemID FROM ItemPoint WHERE MBRContains(GeomFromText(@box), Coords);\n");
错误发生在第二条语句(ResultSet regionResult = ...)
我收到的错误是:
"com.mysql.jdbc.exception.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to user near 'SELECT ItemID FROM ItemPoint WHERE MBRContains((GeomFromText(@box), Coords)' at line 2"
我不确定为什么会出现语法错误,因为我是基于它:http://dev.mysql.com/doc/refman/5.5/en/using-spatial-indexes.html。
我的MySQL服务器版本是:5.5.40-0ubuntu0.14.04.1 (Ubuntu)
你的问题不是MySql,是PreparedStatement
。你不能用它来 运行 两个语句,你必须使用 CallableStatement
对于您的问题,它将类似于:
String callString = "{ call SET @box = 'Polygon(( ? ?,
? ?,
? ?,
? ?,
? ?))';
SELECT ItemID
FROM ItemPoint
WHERE MBRContains(GeomFromText(@box), Coords); }";
CallableStatement callableStatement = con.prepareCall(callString);
callableStatement.setInt(1, lx);
callableStatement.setInt(2, ry);
callableStatement.setInt(3, rx);
callableStatement.setInt(4, ry);
callableStatement.setInt(5, lx);
callableStatement.setInt(6, ly);
callableStatement.setInt(7, rx);
callableStatement.setInt(8, ly);
callableStatement.setInt(9, lx);
callableStatement.setInt(10, ry);
ResultSet regionResult = callableStatement.executeQuery();
请注意,这可能无法正常工作。你可能需要改变一些东西,这是基本的想法。我是凭记忆做的,所以...
我可以通过这样做来让它工作:
String box = "Polygon(("
+ lx + " " + ry + ", "
+ rx + " " + ry + ", "
+ rx + " " + ly + ", "
+ lx + " " + ly + ", "
+ lx + " " + ry + "))";
ResultSet regionResult = stmt.executeQuery(//setBox +
"SELECT ItemID FROM ItemPoint WHERE MBRContains(GeomFromText(' " + box + " '), Coords);\n");