In C # methods, you can use the params parameter[] values to dynamically get an array of parameter values in an SQL statement.
A method similar to C # can be encapsulated in Java itself
1. Get the result set
1 /**2 * Get result set3 * @paramSQL SQL statements4 * @paramparams SQL statement data array5 * @returnresult set6 */7 Public StaticResultSet Getresultset (String sql,object[] params) {8ResultSet rs=NULL;9 //getting connection objects from DbutilTenConnection conn=getconnection (); One Try { A //get PreparedStatement Object -PreparedStatement ps=conn.preparestatement (SQL); - //Iterating through an array of data the for(inti=0;i<params.length;i++) { -Ps.setobject (i+1, Params[i]); - } - //return result set +rs=ps.executequery (); -}Catch(SQLException e) { + //TODO auto-generated Catch block A System.out.println (E.getmessage ()); at returnrs; - } - returnrs; -}
2, the implementation of additions and deletions change
1 /**2 * Perform database additions and deletions and change statements3 * @paramSQL database Execution Statement4 * @paramparams SQL statement data array5 * @returnnumber of affected rows6 */7 Public Static intgetUpdate (String sql,object[] params) {8 intN=0;9 //getting connection objects from DbutilTenConnection conn=getconnection (); OnePreparedStatement ps=NULL; A Try { - //get PreparedStatement Object -ps=conn.preparestatement (SQL); the //Iterating through an array of data - for(inti=0;i<params.length;i++) { -Ps.setobject (i+1, Params[i]); - } +n=ps.executeupdate (); -}Catch(SQLException e) { + //TODO auto-generated Catch block A e.printstacktrace (); at } - returnN; -}
SQL Dynamic Parameters for Java