SQL Dynamic Parameters for Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.