Answer the question
In order to leave comments, you need to log in
How to correctly pass such a request through jdbc?
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 @[email protected] AS Diff;";
stmt = conn.createStatement();
stmt.executeUpdate(sql);
//ResultSet rs = stmt.executeQuery(sql);
Answer the question
In order to leave comments, you need to log in
You have 4 different queries in the sql variable at once. You need to execute each request with a separate executeUpdate() call. Workbench itself determines the number of requests passed to it, separates them and executes them sequentially, one after another. And in the console, you entered each command separately, so you have no problems with the workbench and the console client.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question