mysql - How to execute composite sql queries in java? -
how can execute following query , retrieve result via prepared statement:
insert vcvisitors (sid) values (?); select last_insert_id();
is there way execute both 2 statements @ once?
i've tried following:
connection con = dbmanager.getconnection(); preparedstatement ps = con.preparestatement( "insert vcvisitors (sid) values (?); select last_insert_id();"); ps.setint(1, 10); resultset rs = ps.exequtequery(); rs.next(); return rs.getint("last_insert_id()");
but gives me error executequery can't execute such query, i've tried replace executequery following:
ps.execute(); rs = ps.getresultset();
but gives me sql syntax error:
you have error in sql syntax; check manual corresponds mysql server version right syntax use near 'select last_insert_id()' @ line 1
but there no problems executing query "insert vcvisitors (sid) values ('10'); select last_insert_id();" directly mysql console.
while updating (inserting) data use executeupdate
instead of executequery
. try executing select last_insert_id()
query.
but not portable query. suggest using statement.getgeneratedkeys
instead. please here: jdbc (mysql) retrieving auto_increment column values.
here example of used last_insert_id():
statement stmt = null; resultset rs = null; try { // // create statement instance can use // 'normal' result sets. stmt = conn.createstatement(); // // issue ddl queries table example // stmt.executeupdate("drop table if exists autoinctutorial"); stmt.executeupdate( "create table autoinctutorial (" + "prikey int not null auto_increment, " + "datafield varchar(64), primary key (prikey))"); // // insert 1 row generate auto increment // key in 'prikey' field // stmt.executeupdate( "insert autoinctutorial (datafield) " + "values ('can auto increment field?')"); // // use mysql last_insert_id() // function same thing getgeneratedkeys() // int autoinckeyfromfunc = -1; rs = stmt.executequery("select last_insert_id()"); if (rs.next()) { autoinckeyfromfunc = rs.getint(1); } else { // throw exception here } rs.close(); system.out.println("key returned " + "'select last_insert_id()': " + autoinckeyfromfunc); } { if (rs != null) { try { rs.close(); } catch (sqlexception ex) { // ignore } } if (stmt != null) { try { stmt.close(); } catch (sqlexception ex) { // ignore } } }
and here same getgeneratedkeys:
statement stmt = null; resultset rs = null; try { // // create statement instance can use // 'normal' result sets assuming have // connection 'conn' mysql database // available stmt = conn.createstatement(java.sql.resultset.type_forward_only, java.sql.resultset.concur_updatable); // // issue ddl queries table example // stmt.executeupdate("drop table if exists autoinctutorial"); stmt.executeupdate( "create table autoinctutorial (" + "prikey int not null auto_increment, " + "datafield varchar(64), primary key (prikey))"); // // insert 1 row generate auto increment // key in 'prikey' field // stmt.executeupdate( "insert autoinctutorial (datafield) " + "values ('can auto increment field?')", statement.return_generated_keys); // // example of using statement.getgeneratedkeys() // retrieve value of auto-increment // value // int autoinckeyfromapi = -1; rs = stmt.getgeneratedkeys(); if (rs.next()) { autoinckeyfromapi = rs.getint(1); } else { // throw exception here } rs.close(); rs = null; system.out.println("key returned getgeneratedkeys():" + autoinckeyfromapi); } { if (rs != null) { try { rs.close(); } catch (sqlexception ex) { // ignore } } if (stmt != null) { try { stmt.close(); } catch (sqlexception ex) { // ignore } } }
Comments
Post a Comment