How would I use string.format() to omit the + string concatenation + in this Java/SQL code.
String sql = "SELECT * FROM "+p_tableName+" WHERE 1 = 0";
How would I use string.format() to omit the + string concatenation + in this Java/SQL code.
String sql = "SELECT * FROM "+p_tableName+" WHERE 1 = 0";
String sql = String.format("SELECT * FROM %s WHERE 1 = 0", p_tableName);
Always be careful when making SQL statements this way as they can very easily be used against you in SQL injection attacks.
You can use String.format("SELECT * FROM %s WHERE 1 = 0", table_name)
. But, I strongly advise you to use PreparedStatement
instead (See How do I make a prepared statement?, https://stackoverflow.com/a/396765/130224, PreparedStatements and performance, and Using Prepared Statements). PreparedStatement
yields higher performance and security.