MySql连接器j允许用户变量

时间:2011-03-01 12:03:19

标签: java mysql jdbc

请帮助我了解如何允许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中做到这一点?

1 个答案:

答案 0 :(得分:8)

我发现了如何让它发挥作用。只需设置datasource属性allowMultiQueries = true

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