2013-03-30 73 views
0

我嘗試運行此MySQL查詢和我有這個錯誤的java:MySQL查詢與MBROVERLAP

com.mysql.jdbc.exceptions.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 use near 'SET @r2 = GeomFromText('Polygon((1 1,5 1,5 5,1 5,1 1))'); select MBROVERLAPS(@r1' at line 2

代碼:

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

問題出在哪裏?

回答

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()); 

一個大破綻是,你不應該在你的JDBC查詢字符串界定分號。