Package database additions and deletions this common method

Source: Internet
Author: User

 Public classJdbcTemplate {/** Add and delete package * @param sql: The SQL statement to execute * @param params sql corresponding parameter value, must be corresponding to question mark one by one * @return rows increase the number of lines affected * @t Hrows SQLException*/     Public Static intUpdate (String Sql,object []params) throws sqlexception{//call the encapsulated method to register the driver and establish a link to the databaseConnection conn =connectionfactory.getconnection (); //Create the Preparestatement () method of the SQL statement that needs to be executedPreparedStatement pstmt =conn.preparestatement (SQL); //determine if the array that is passed in is the correct SQL statement        if(params!=NULL&&params.length>0){             for(inti =0; I <params. length; i++) {                //since the subscript of the array is starting from 0, the subscript of the database starts from 1 and must be subscript +1//Params[i] is the corresponding parameter typePstmt.setobject (i+1,params[i]); }        }        //Call method returns the number of affected rows modified        introws =pstmt.executeupdate (); //To invoke a method of closing resources encapsulated in Factory modeConnectionfactory.close (PSTMT, conn); //return to Rows        returnrows; }         Public Static voidMain (string[] args) {//create the SQL statement you want to executeString sql ="update t_user set password=? and id=?"; Try {            //New object[] {"333", 1} "333" is the modified password 1 is the user of id=1 in the table            introws = jdbctemplate.update (sql,NewObject[] {"333",1}); System. out. println (rows); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }    }

Package database additions and deletions this common method

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.