Firstly, under the hood, each db uses prepared instructions, even if they are created on the fly and immediately thrown away.
Secondly, you should not be afraid of prepared statements. They offer a huge performance boost: after creation, they can be reused with various parameters, but all parsing, user authorization verification, plan and query optimization are calculated once and saved using a prepared statement.
If you are going to execute the same sql again and again, create a prepared statement, keep a reference to it and reuse it - providing different parameters for each call.
Here is an example of code that will give you an idea of ββhow to use them:
private PreparedStatement preparedStatement; public ResultSet getAccount(String id) throws SQLException { // Do this once if (preparedStatement == null) preparedStatement = conn.prepareStatement("select * from account where id = ?"); // Do this many times preparedStatement.setString(1, id); return preparedStatement.executeQuery(); } public static void main(String[] args) throws Exception { ResultSet rs = new MyClass().getAccount("00100000006ONCrAAO"); }
source share