0

I want to insert into some tables some values. I have an arrayList of the table information. I want to loop through this arrayList and insert values to diff tables. What should the string query be like?

For Eg:-

insert into accounts(id,balance) values(1,78900);
insert into customers(name,balance) values('bailey',9000);
insert into savings(name,remaining_balance) values('sean',8900.90);

I was thinking something like:-

String stmt = "Insert into %s (%s) values(%s)";

I am not sure this is the right insert statement in String. Do we have to use some regular expression ?

I am a beginner in Java and dbms so please be kind.

  • Please read about prepared statements in Java. – Tim Biegeleisen Aug 24 '22 at 15:24
  • Assuming you are using the Postgres JDBC driver there is a manual that covers this and more here [JDBC driver](https://jdbc.postgresql.org/documentation/head/index.html). – Adrian Klaver Aug 24 '22 at 15:29
  • @TimBiegeleisen If you meant specifying the table name as a parameter of a prepared statement, using a placeholder for table name [is not possible](https://stackoverflow.com/q/1208442/642706). – Basil Bourque Aug 24 '22 at 15:30
  • Using Prepared statements I can make string query sql with variable column values,but here the table name is also variable. So want to place some placeholder for it. – Just_CODING Aug 24 '22 at 15:30
  • 1
    You can't do that from a prepared statement and most likely having the need to do this is a design smell. – Tim Biegeleisen Aug 24 '22 at 15:32
  • At runtime, select one of several queries, each aimed at a different table. See [this Answer](https://stackoverflow.com/a/57474980/642706). – Basil Bourque Aug 24 '22 at 15:33

0 Answers0