I am trying to find out which is the best practice when organising the
PreparedStatement strings in a application which uses JDBC to connect to the database.
Usually this is the way I retrieve the data from the database:
PreparedStatement select = null; String prepQuery = "SELECT * FROM user JOIN details ON user.id = details.user_id WHERE details.email = ? AND details.password = ?"; select = con.prepareStatement(prepQuery); select.setString(1, email); select.setString(2, pass); ResultSet result = select.executeQuery(); // Handle the result.
This is a method which retrieves a user entity using the email and a password. For all my methods this is how I save the query strings.
The first drawback which comes to mind is that this is quite messy and difficult to manage if I'm changing the SQL provider (Maybe if I would place all the queries in a Constants file would be a better solution). Furthermore JPA offers tools like
@NamedQuery which is said to improve the performance of the application.
Is there a best practice when it comes to organising the query strings in an application using JDBC. Thank you!