Código:
public void creartriggers(){ String TriggerBarcode = "DROP TRIGGER IF EXISTS ACTCODIGO"; String CrearTrigger = "CREATE TRIGGER actcodigo BEFORE INSERT ON BC_ARTICULOS FOR EACH ROW BEGIN DECLARE digito INT DEFAULT 0; DECLARE resultado INT DEFAULT 0; DECLARE IDN INT DEFAULT 0; DECLARE NUMPREV VARCHAR(30) DEFAULT 0; IF NEW.BARCODE IS NULL THEN SET IDN = (SELECT MAX(ID) FROM BC_ARTICULOS); IF IDN IS NULL THEN SET IDN = 1; ELSE SET IDN = (SELECT MAX(ID) FROM BC_ARTICULOS) +1; END IF; SET NUMPREV = 750000000000 + IDN; SET DIGITO = 10-(((substr(NUMPREV,2,1)+substr(NUMPREV,4,1)+substr(NUMPREV,6,1)+substr(NUMPREV,8,1)+substr(NUMPREV,10,1)+substr(NUMPREV,12,1))*3)+((substr(NUMPREV,1,1)+substr(NUMPREV,3,1)+substr(NUMPREV,5,1)+substr(NUMPREV,7,1)+substr(NUMPREV,9,1)+substr(NUMPREV,11,1))))mod 10; IF DIGITO < 10 THEN SET NEW.BARCODE = CONCAT(NUMPREV,DIGITO); ELSE SET NEW.BARCODE = CONCAT(NUMPREV,'0'); ELSE SET NEW.BARCODE = CONCAT(NUMPREV,'0'); END IF; END"; ConexionMySQL ConexionMysql = new ConexionMySQL(); Connection cn = ConexionMysql.Conectar(); try{ PreparedStatement stTrigger = cn.prepareStatement(TriggerBarcode); stTrigger.execute(); PreparedStatement stCrearTrigger = cn.prepareStatement(CrearTrigger); stCrearTrigger.execute(); }catch(Exception e){ JOptionPane.showMessageDialog(null, e); } }
y al correrlo me lanza la excepcion de com.mysql.jdbc.exceptions.jdbc4.MySQL SyntaxErrorException : 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 'ELSE SET NEW.BARCODE = CONCAT(NUMPREV,'0'); END IF; END' at line 1
antes tenia definido delimiter ! asi pero lo elimine , al correrlo si me lo acepta en la consola de mysql pero aqui me tira ese error , agradeceria me pudieran orientar que es lo que esta fallando al pasar la syntaxis para crear el trigger , de antemano gracias por la atencion