javarestjdbc

Which is better for Dynamic querying String Builder or string buffer?


I am dynamically building SQL queries in Java using JDBC and need to determine the best approach for handling these queries efficiently. Currently, I am debating between using StringBuilder or StringBuffer for constructing the queries. I understand that StringBuffer is synchronized and thread-safe, while StringBuilder is not.

The application generates multiple queries based on user input. Queries might involve conditions, joins, and subqueries, so the query building process can be dynamic and complex.


Solution

  • Looks definitely like a StringBuilder usage to me. The thread-safe StringBuffer is useful when multiple threads are putting data into the same instance of StringBuffer, which doesn't make sense for SQL query building.