0

I have the following SQL script (stored in a .sql file) imported into my java program as a string (I changed some variable names so its easier to read):

SET @variable1 := (
 //setting the value of this variable here using some values in the database
);

SET @variable2 := (
 //setting variable2 with the help of variable1 and some more data
);

SELECT ...
 //selecting something using the values of both variables

When I execute this script in MySQL I get the desired result, no errors whatsoever, but I cant do this in JDBC as the script consists of multiple statements. I could execute them as separate statements but they rely on each other (2. statement needs variable1's value to assign variable2's value and so on)

How could I execute this script in JDBC so that I get one ResultSet at the end (which contains the same result as when I execute it in MySQL)? Im essentialy asking how can I "connect" these statements, so that they can use these variables from each other.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Gtomika
  • 845
  • 7
  • 24
  • 1
    Be aware that in most cases, you should be able construct a single query instead of having to execute three queries. – Mark Rotteveel Sep 21 '18 at 14:46
  • @MarkRotteveel thanks for help. I followed the instruction in the marked question's answer and I have another question. How can java throw SQLSyntaxErrorException if the script runs perfectly in MySQL? – Gtomika Sep 21 '18 at 15:19
  • Usually it means that you have a syntax error, for example missing whitespace, or other things that are not valid for the execution context. – Mark Rotteveel Sep 21 '18 at 15:21

0 Answers0