java - How can I use DECLARE clause in a statement on jdbc? -


i trying use declare clause in preparedstatent jdbc. code wrote is:

         statement.executeupdate(" declare @variable int set @variable = "+timer+" insert table1 values (ip, protocol, counter, timer) select ip,protocol,counter,@variable table2 order counter desc limit 5 offset 0 ;"); 

what i'm trying create new table (that table1) includes top 5 table2 (every 5 secs e.g), predefined interval. interval timer variable. timer variable passed through method.

note: don't know if makes difference use preparedstatement. tried both.

assuming need create new table select, should use query instead:

create table table1 select ip,protocol,counter,@variable table2 order counter desc limit 5 offset 0 

but if in java , using preparedstatement can pass value of @variable parameter, getting rid of previous query. so, query in java code:

string sql =     "create table table1"     + " select ip,protocol,counter,?"     + " table2"     + " order counter desc"     + " limit 5 offset 0"; 

assuming have table table1 created , you're adding latest results table2, query this:

insert table1 values (ip, protocol, counter, timer) select ip,protocol,counter,@variable table2 order counter desc limit 5 offset 0 

again, can pass value of @variable parameter. query in java code:

string sql =      "insert table1 (ip, protocol, counter, timer)"     + " select ip,protocol,counter,?"     + " table2"     + " order counter desc"     + " limit 5 offset 0"; 

then, prepare query this:

preparedstatement pstmt = con.preparestatement(sql); //setting variable parameter in query pstmt.setstring(1, timer); 

in end, use preparedstatement#execute or preparedstatement#executeupdate:

//the former query ddl query pstmt.execute(); //the latter query dml query pstmt.executeupdate(); 

Comments

Popular posts from this blog

python - Mongodb How to add addtional information when aggregating? -

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

java - Incorrect order of records in M-M relationship in hibernate -