I'm trying to create a Database using JDBC from Java.
The DB and Table creation goes easily, however now I need to create functions and later a trigger and I keep hitting the wall of exceptions with this function as example:
USE ratercases;
DROP FUNCTION IF EXISTS `IMC`;
DELIMITER $$
CREATE FUNCTION `IMC` (altura INT,peso INT)
RETURNS DOUBLE
BEGIN
DECLARE AltM DOUBLE;
DECLARE AltSqr DOUBLE;
SET AltM=(altura/100);
SET AltSqr=AltM*AltM;
RETURN peso/AltSqr;
END $$
DELIMITER ;
If i run the script above on MySQL Workbench there are no problems; However runnning it from Java gets me SQL Syntax Error Exceptions.
I use the following Java code:
// ps is a SQL Statement; ratercases is the target Database;
ps.execute("USE ratercases;"
+ "DROP FUNCTION IF EXISTS `IMC`;"
+ " DELIMITER $$"
+ " CREATE FUNCTION `IMC` (altura INT,peso INT)"
+ " RETURNS DOUBLE"
+ " BEGIN"
+ " DECLARE AltM DOUBLE;"
+ " DECLARE AltSqr DOUBLE;"
+ " SET AltM=(altura/100);"
+ " SET AltSqr=AltM*AltM;"
+ " RETURN peso/AltSqr;"
+ " END $$"
+ " DELIMITER ;");
//
Java/SQL is apparently having problems with the Delimiter $$ ...
Can anyone enlighten me?
Thanks