Im using the following code to generate search results from a relational database, depending on several (optional) search parameters from the web client.
I am currently using " java.sql.Statement " to achieve functionality, but I need this to be achieved using " java.sql.PreparedStatement " to prevent SQL injection.
Let me know how to change the code.
eg.
User logins from the web client.
- param1 - Optional
- param2 - Optional
- dateParamFr - optional
- dateParamTo - optional
Pseudocode of SQL templates depending on search parameters as follows
IF (WITHOUT ANY SEARCH PARAMETER){ SELECT * FROM TEST_TABLE; } ELSE IF(WITH param1){ SELECT * FROM TEST_TABLE WHERE COLUMN1= param1; } ELSE IF(WITH param1 & param2){ SELECT * FROM TEST_TABLE WHERE COLUMN1= param1 AND COLUMN2= param2 } SO ON β¦β¦β¦
Below is a snippet of Java code in my EJB
Connection cnBOP = null; Statement stmt = null; StringBuffer sb = new StringBuffer(""); try { cnBOP = jdbcBOP.getConnection();
source share