public static void main(String[] args) throws SQLException, ClassNotFoundException {
Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql="SET @mon=6; "
+ "SET @var1=(SELECT counter FROM electro.data_counter WHERE YEAR(date)= 2016 AND MONTH(date) = @mon-1); "
+ "SET @var2=(SELECT counter FROM electro.data_counter WHERE YEAR(date)= 2016 AND MONTH(date) = @mon); "
+ "SELECT @var2-@var1 AS Diff;";
stmt = conn.createStatement();
stmt.executeUpdate(sql);
//ResultSet rs = stmt.executeQuery(sql);
выдает эксепшн:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;
проверял в workbench, запрос работает.
из консоли тоже работает:
mysql> SET @mon=6;
Query OK, 0 rows affected (0.00 sec)
mysql> SET @var1=(SELECT counter FROM ele
6 AND MONTH(date) = @mon-1);
Query OK, 0 rows affected (3.41 sec)
mysql> SET @var2=(SELECT counter FROM ele
6 AND MONTH(date) = @mon);
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @var2-@var1 AS Diff;
+------+
| Diff |
+------+
| 184 |
+------+
1 row in set (0.00 sec)