2011-03-01 28 views
4

請幫我找出如何讓mysql的連接口J定義用戶變量,使該代碼有效:MySql的連接口J允許用戶變量

Statement s = conn.createStatement(); 
    s.executeQuery ("set @categoryId := (Select CategoryId from categories order by CategoryId desc LIMIT 1);\n" + 
         "set @categoryId := IF(@categoryId is Null, 1, @categoryId);"); 

現在它拋出一個異常:

MySQLSyntaxErrorException occured : 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 @categoryId := IF(@categoryId is Null, 1, @categoryId)' at line 2 

我知道在.net中你可以在連接字符串中定義「Allow User Variables = true」選項。如何在java中做到這一點?

回答

8

我發現如何得到它的工作。只需設置數據源屬性allowMultiQueries = true

jdbc:mysql://localhost:3306/DBS?allowMultiQueries=true