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&¶ms.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 SQL statements that need to be executed (SQL statements can be changed to perform appropriate additions and deletions based on the actual incoming statements)String sql ="update t_user set password=? where 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 (); } }}
The database has been censored for generic method encapsulation (according to the SQL statements that have been censored to perform the corresponding business operations)