The JdbcTemplate a custom (CRUD tables in a database record)

demand:

  • JdbcTemplate a custom template, functional additions and deletions to the database table records
    . 1  Package demo03;
     2  
    . 3  Import utils.JDBC_DBCP_Utils;
     . 4  
    . 5  Import the javax.sql.DataSource;
     . 6  Import the java.sql.Connection;
     . 7  Import java.sql.ParameterMetaData;
     . 8  Import java.sql.PreparedStatement;
     . 9  Import the java.sql. SQLException;
     10  
    . 11  public  class MyJDBCTemplate {
     12 is      // 1. need to pass the data source 
    13 is      Private the dataSource the dataSource;
     14  
    15      // constructor 
    16      publicMyJDBCTemplate (the DataSource the dataSource) {
     . 17          the this .DataSource = the dataSource;
     18 is      }
     . 19  
    20 is      / ** 
    21 is       * a step of encapsulating + JDBC database operation metadata, the release of resources (the user does not release the resource of interest)
     22       * add, delete, modify
     23 is       *
     24       * @param SQL SQL statement
     25       * @param the params parameter
     26 is       * @return the Ctrl + the Alt + T
     27       * / 
    28      public  int Update (SQL String, Object ... the params) {
     29          Connection Connection = null ;
     30         = The preparedStatement the PreparedStatement null ;
     31 is          the try {
     32              // of 0. The non-empty judgment 
    33 is              IF (the dataSource == null ) the throw  new new a RuntimeException ( "Not null the dataSource MUST ..." );
     34 is  
    35              IF (SQL == null ) the throw  new new a RuntimeException ( "Not null sql MUST ..." );
     36  
    37 [              // 1. connection object obtained from the dataSource 
    38 is              connection = dataSource.getConnection ();
     39              // 2. Create Object precompiled sql statement insert into user values ( ?,?,?,?)
    40              the preparedStatement = Connection.prepareStatement (SQL);
     41 is  
    42 is              // 3. metadata objects obtained parameter 
    43 is              ParameterMetaData An ParameterMetaData = preparedStatement.getParameterMetaData ();
     44 is              // 4. number of the parameters obtained in 
    45              int parameterCount = parameterMetaData.getParameterCount ( );
     46 is  
    47              // 5 to each assignment? 
    48              for ( int I = 0; I <parameterCount; I ++ ) {
     49                  PreparedStatement.setObject (I +. 1 , the params [I]);
     50              }
     51 is  
    52 is             //6. 执行
    53             int i = preparedStatement.executeUpdate();
    54             return i;
    55         } catch (SQLException e) {
    56             e.printStackTrace();
    57         } finally {
    58             //释放资源
    59             JDBC_DBCP_Utils.release(null, preparedStatement, connection);
    60         }
    61         return -1;
    62     }
    63 }

     

Guess you like

Origin www.cnblogs.com/optimistic-cheerful/p/11033139.html