Derby Transactions BEGIN and END - java

I created a trigger which works fine.
CREATE TRIGGER INSERT_USERID_TO_PERMISSIONS_TABLES
AFTER INSERT ON ALLUSERS
REFERENCING NEW AS NEW_USERID_ROW
FOR EACH ROW MODE DB2SQL
INSERT INTO ALLUSERS_HOMEPERMISSIONS(USERID)
VALUES(NEW_USERID_ROW.USERID);
Then I dropped it to recreate a new version of it that has 2 INSERT statements. When I added the other INSERT statement, it fails and returns an error. I know there's just some syntax error or keyword I missed. I googled it but can't get the equivalent of BEGIN and END (in MSSQL) for Derby.
So if I do,
CREATE TRIGGER INSERT_USERID_TO_PERMISSIONS_TABLES
AFTER INSERT ON ALLUSERS
REFERENCING NEW AS NEW_USERID_ROW
FOR EACH ROW MODE DB2SQL
INSERT INTO ALLUSERS_HOMEPERMISSIONS(USERID) --1st insert
VALUES(NEW_USERID_ROW.USERID);
INSERT INTO ALLUSERS_ADMIN_PERMISSIONS(USERID)-- 2nd insert
VALUES(NEW_USERID_ROW.USERID);
it returns an error.
I thought maybe I can add BEGIN and END
CREATE TRIGGER INSERT_USERID_TO_PERMISSIONS_TABLES
AFTER INSERT ON ALLUSERS
REFERENCING NEW AS NEW_USERID_ROW
FOR EACH ROW MODE DB2SQL
BEGIN
INSERT INTO ALLUSERS_HOMEPERMISSIONS(USERID) --1st insert
VALUES(NEW_USERID_ROW.USERID);
INSERT INTO ALLUSERS_ADMIN_PERMISSIONS(USERID)-- 2nd insert
VALUES(NEW_USERID_ROW.USERID);
END
I'd appreciate any help.
Thanks.

Related

Java prepared statement throwing SQLIntegrityConstraintViolationException: Invalid argument(s) in call

I am trying to insert a record into DB (Oracle) through Java code. When it is preparing statement at that time it is throwing an exception
java.sql.SQLIntegrityConstraintViolationException: Invalid argument(s) in call
on below line of code:
PreparedStatement ps = connection.prepareStatement(INSERT_QUERY);
and the insert query is:
private static final String INSERT_QUERY = "INSERT INTO ABC ( uid,created_datetime,status,update_datetime,b_id,ref_no,ref_dt,sor,b1_id,c_code,base,name,src,trn_date,country,pr,cv,features,scoring_time_ms,scoring_request_time_ms,preprocessing_time_ms,postprocessing_time_ms,overall_time_ms )VALUES ( ?,current_timestamp,?,current_timestamp,?,?,?,?,?,?,?,?,?,current_timestamp,?,?,?,?,?,?,?,?,? )";
When I debugged the code I found that when it is preparing statement connection.prepareStatement(INSERT_QUERY); insert query is showing as below:
INSERT INTO ABC ( uid,created_datetime,status,update_datetime,b_id,ref_no,ref_dt,sor,b1_id,c_code,base,name,src,trn_date,country,pr,cv,features,scoring_time_ms,scoring_request_time_ms,preprocessing_time_ms,postprocessing_time_ms,overall_time_ms )VALUES ( ?,systimestamp,?,systimestamp,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? )
It is changing current_timestamp to systimestamp and also I have 3 current_timestamp in the query and it is missing 3rd one while debugging.
I have deleted all the records from database. DB is empty and it has no constraints, only primary key.
Can anyone help me on this issue. Why is this happening and how can I resolve this?
SQLIntegrityConstraintViolationException points to a db constraint violation. Check the for primary/unique/foreign keys in the table "ABC". Just based on the insert statement, can assume uid must be unique.
This issue is resolved. It was build issue. It was not taking updated code so getting that error. Deleted everything from .m2 dir and build it again and this error got disappeared.

MySqlSyntaxErrorException wrong Query

I'm tring to create a table in mysql from java desktop program but I obtain a MySqlSyntaxErrorException.
The query is :
CREATE TABLE FileXFascia(fila0 Integer,fila1 Integer,fila2 Integer,fila3 Integer) VALUES ('3','4','3','3')
Anyone knows where I'm wrong?
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 'VALUES ('3','4','3','3')' at line 1
You need to split these as follows:
CREATE TABLE FileXFascia(fila0 Integer,fila1 Integer,fila2 Integer,fila3 Integer);
INSERT INTO FileXFascial (fila0, fila1, fila2, fila3) VALUES ('3','4','3','3');
In your question there are two different operations on a table, You are trying to create and insert data in single query even in a wrong way. First you need to create table then insert data into created table. Like below syntax.
create table tableName(col1 dataType,col2 dataType,col3 dataType,.......coln dataType);
After creation of table now you can insert data into table. Like below syntax.
insert into tableName(col1, col2,col3,......coln) values ('data1','data2','data3',......'datan');

Unable to DROP Index (Sybase) on a table from Java Code

I have a Stored Procedure which drops index on a particular table in the DB. This SP when executed from Database console works fine without errors.
When I use JDBC to execute the SP using a Callable Statement, it gives an SQL exception and says the index cannot be dropped.
When checked on the database, the indexes are dropped too.
CREATE PROCEDURE sp_drop_idx as
BEGIN
DROP INDEX sp_close_1.XPKusr1
DROP INDEX sp_close_1.ilxuser22
DROP INDEX sp_close_1.indx_billid1
DROP INDEX sp_close_1.cycle_ind
END
GO
This SP is called within another SP, let's call that outerSP from the java code using plain JDBC call.
cb = conn.prepareCall("{call outerSP}");
s = cb.executeQuery();
while(rs.next()){
num = (Integer)rs.getInt(1);
}
It was a simple change in the SP :
For every index we used an IF condition -
IF EXISTS(select * from sysindexes where id = object_id('sp_close_1') and name ='XPKusr1')
DROP INDEX sp_close_1.XPKusr1

MySQL: Create a trigger keeps giving me syntax error

I've been trying to create a trigger from java and it just won't work.
String trigger = String.format("CREATE TRIGGER `%s` AFTER %s ON %s BEGIN INSERT INTO `ndb_log` (`table_name`, `action`, `time`) VALUES ('%s', '%s', UNIX_TIMESTAMP()) END;",
name, this.event.toUppercase(), this.table, this.table, this.event.toLowercase());
CREATE TRIGGER `onnc_censorINSERT` AFTER INSERT ON nc_censor BEGIN INSERT INTO `ndb_log` (`table_name`, `action`, `time`) VALUES ('nc_censor', 'insert', UNIX_TIMESTAMP()); END;
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'BEGIN INSERT INTO ndb_log (table_name, action, time) VALUES ('nc_censor' at line 1
I am not running this in phpMyAdmin, a server console or anything like that. I need to run it from java in a String. I call this.mysql.update(trigger); in my class which "basically" runs it.
I have tried running the create trigger string in ssh mysql, but it also doesn't work.
Two things: (1) you're missing the FOR EACH ROW clause of the trigger declaration; (2) you don't need the BEGIN and the END, because your trigger executes just one statement. Try this instead:
CREATE TRIGGER `onnc_censorINSERT` AFTER INSERT ON nc_censor FOR EACH ROW INSERT INTO `ndb_log` (`table_name`, `action`, `time`) VALUES ('nc_censor', 'insert', UNIX_TIMESTAMP());

hibernate createSQLQuery bulk insert

i try to execute these 3 statements, 1 and 2 success, but when reach 3 statement, i get error, is it because i trying to insert into same table during statement 2 and cause statement 3 fail? by the way, all 3 statement is inside one #spring transaction
session.createSQLQuery(" select groupid from group_ where groupid = 888880005").executeQuery;//executed ok
session.createSQLQuery(" insert into layoutset (layoutsetid,groupid,companyid,privatelayout,logo,logoid,themeid,colorschemeid, wapthemeid,wapcolorschemeid,pagecount) values (888880005, 888880005, 1,0,0,0,'classic','03','mobile','01',0) ").executeUpdate();//executed ok
session.createSQLQuery(" insert into layoutset (layoutsetid,groupid,companyid,privatelayout,logo,logoid,themeid,colorschemeid, wapthemeid,wapcolorschemeid,pagecount) values (888890005, 888880005, 1,0,0,0,'classic','03','mobile','01',0) ").executeUpdate(); //this fail with
com.liferay.portal.kernel.dao.orm.ORMException: could not execute native bulk manipulation query
at com.liferay.portal.dao.orm.hibernate.ExceptionTranslator.translate(ExceptionTranslator.java:41)
at com.liferay.portal.dao.orm.hibernate.SQLQueryImpl.executeUpdate(SQLQueryImpl.java:70)
at com.company.service.companyPersistenceImpl.doTest(companyPersistenceImpl.java:53)
at com.company.service.companyLocalServiceImpl.doTest(companyLocalServic
the two inserts are exactly the same (same values for columns layoutsetid,groupid,companyid,privatelayout); the failure is due to a key-constraint I guess.
what is the primary key of your table?
Have you included the entire stacktrace? Seems to be some lines missing there.
However besides the different layoutsetid the two last queries are the same, which is pretty pointless. Probably there are some database constraints on the table to prevent this which result in the error.

Categories