PreparedStatement package with various SQL statements prepared

I am wondering if something is possible in Java

String A = "UPDATE blah set x=? y=? z=?"
String B = "UPDATE blah set a=? b=? c=? d=?"

I would like to prepare an expression first of all about speed, and secondly, about safety. I want to be able to populate the bind variables for A, execute A, bind the variables for B and execute B, and then commit the whole transaction. Is there a better way to do this?

+1
source share
3 answers

You can use several PreparedStatements to get the desired result:

// Prepare code for PreparedStatement #1
String varOne = "A";
String varTwo = "B";
String varThree = "C";

String queryOne = "UPDATE blah set x=? y=? z=?"
PreparedStatement firstStmt = conn.prepareStatement(queryOne);

firstStmt.setString(1, varOne);
firstStmt.setString(2, varTwo);
firstStmt.setString(3, varThree);

firstStmt.executeUpdate();
conn.commit();

// Prepare code for PreparedStatement #2
String varOneB = "X";
String varTwoB = "Y";
String varThreeB = "Z";
String varFourB = "A";

String queryOne = "UPDATE blah set a=? b=? c=? d=?"
PreparedStatement secondStmt = conn.prepareStatement(queryTwo);

secondStmt.setString(1, varOneB);
secondStmt.setString(2, varTwoB);
secondStmt.setString(3, varThreeB);

secondStmt.executeUpdate();
conn.commit();
0
source

PL/SQL, , PreparedStatement .

, , : 1) A, 2) A, 3) B, 4) B, 5) .

. , PL/SQL, A B. . , / ( ) PL/SQL.

, . , , , - . , - ( , , - , ).

, Statement, PreparedStatement.

http://docs.oracle.com/javase/6/docs/api/java/sql/Statement.html

clearBatch addBatch, PreparedStatement SQL, SQL .

0

, :

UPDATE blah set x =?, y =?, z =?, a =?, b =?, c =?, d =?

0

Source: https://habr.com/ru/post/1618272/


All Articles