paomo30000
2009-02-19 12:21
浏览 226
已采纳

大家帮忙看一下,这句带有变量的SQL怎么在JDBC上执行啊

就是这句
[code="sql"]SET @rank=0;
SELECT @rank:=@rank+1 AS rank, fruit, amount
FROM sales
ORDER BY amount DESC[/code]

我写成这样
[code="java"]String selectSql = "SET @rank=0; SELECT @rank:=@rank+1 AS rank, fruit, amount FROM sales ORDER BY amount DESC";
ResultSet = stms.executeQuery(selectSql);[/code]
但是系统报错啊
[color=red]java.sql.SQLException: 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 ';SELECT @rank:=@rank+1 AS rank, fruit, amount FROM sales ORDER BY amount DESC' at line 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2847)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1531)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1622)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2370)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2297)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1183)[/color]

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题