Advertisement
see613

Example #3: SQLDriver

Nov 5th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 6.63 KB | None | 0 0
  1. package see613.core.db;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.ResultSetMetaData;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10.  
  11. import see613.core.base.ModelData;
  12. import see613.core.log.ILog;
  13.  
  14. public class SQLDriver {
  15.       private static final Integer QUERY_TYPE_UPDATE = 1;
  16.       private static final Integer QUERY_TYPE_OTHERS = 2;
  17.    
  18.       public ResultSet resultSet = null;
  19.       public ArrayList<ModelData> resultData = null;
  20.       public ModelData rowData = null;
  21.       public Long lastInsertId = -1L;
  22.       public int affectedRows = 0;
  23.      
  24.       private PreparedStatement statement = null;
  25.       private Integer queryType;
  26.       private String sql = "";
  27.       private int paramsCount = 0;
  28.       private ILog log;
  29.       private IConnectionPool connectionPool;
  30.  
  31.      
  32.       /**
  33.        * @throws Exception
  34.        */
  35.       public SQLDriver prepareStatement(String ... params) throws Exception {
  36.           Connection connect = connectionPool.get();
  37.           resultSet = null;
  38.          
  39.           statement = connect.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
  40.          
  41.           for(int i=paramsCount; i<(paramsCount + params.length); i++) {          
  42.               statement.setString(i+1, params[i]);
  43.           }
  44.          
  45.           paramsCount += params.length;
  46.  
  47.           return this;
  48.       }
  49.      
  50.       /**
  51.        * @throws Exception
  52.        */
  53.       public SQLDriver run() throws Exception {
  54.           Connection connect = statement.getConnection();
  55.          
  56.           try {
  57.               if ( queryType == QUERY_TYPE_UPDATE ) {
  58.                   affectedRows = statement.executeUpdate();
  59.               }
  60.               else {
  61.                   resultSet = statement.executeQuery();
  62.               }
  63.           } catch (Exception e) {
  64.               close();
  65.               connectionPool.close(connect);
  66.              
  67.               throw e;
  68.           }
  69.          
  70.           connectionPool.add(connect);
  71.          
  72.           String sqlString = statement.toString();
  73.                  sqlString = sqlString.substring( sqlString.indexOf(": ") + 2 );
  74.              
  75.           getLog().notice("query executed: "+ sqlString, 2);
  76.          
  77.           return this;
  78.       }
  79.      
  80.       /**
  81.        * @throws Exception
  82.        */
  83.       public SQLDriver query(Integer type, String sql, Boolean runImmediately, String ... params) throws Exception {
  84.           queryType = type;
  85.           paramsCount = 0;
  86.           this.sql = sql;
  87.          
  88.           prepareStatement(params);
  89.          
  90.           if ( runImmediately ) {
  91.               run();
  92.           }
  93.          
  94.           return this;
  95.       }
  96.      
  97.       /**
  98.        * @throws Exception
  99.        */
  100.       public SQLDriver query(Integer type, String sql, String ... params) throws Exception {
  101.           query(type, sql, true, params);
  102.          
  103.           return this;
  104.       }
  105.      
  106.       /**
  107.        * @throws Exception
  108.        */
  109.       public SQLDriver query(String sql, Boolean runImmediately, String ... params) throws Exception {
  110.           query(QUERY_TYPE_OTHERS, sql, runImmediately, params);
  111.          
  112.           return this;
  113.       }
  114.      
  115.       /**
  116.        * @throws Exception
  117.        */
  118.       public SQLDriver query(String sql, String ... params) throws Exception {
  119.           query(QUERY_TYPE_OTHERS, sql, params);
  120.          
  121.           return this;
  122.       }
  123.      
  124.       /**
  125.        * @throws Exception
  126.        */
  127.       public SQLDriver update(String sql, Boolean runImmediately, String ... params) throws Exception {
  128.           query(QUERY_TYPE_UPDATE, sql, runImmediately, params);
  129.          
  130.           return this;
  131.       }
  132.      
  133.       /**
  134.        * @throws Exception
  135.        */
  136.       public SQLDriver update(String sql, String ... params) throws Exception {
  137.           update(sql, true, params);
  138.          
  139.           return this;
  140.       }
  141.      
  142.       /**
  143.        * @throws SQLException
  144.        */
  145.       public long getInsertId() throws SQLException {
  146.           if (affectedRows == 0) {
  147.               throw new SQLException( "insert failed, no rows affected" );
  148.           }
  149.          
  150.           ResultSet generatedKeys = statement.getGeneratedKeys();
  151.        
  152.           if ( generatedKeys.next() ) {
  153.               lastInsertId = generatedKeys.getLong(1);
  154.              
  155.               return lastInsertId;
  156.           } else {
  157.               throw new SQLException("insert failed, no generated key obtained");
  158.           }
  159.       }
  160.  
  161.       /**
  162.        * @throws Exception
  163.        */
  164.       protected ModelData getRow(ResultSetMetaData metaData, int columnCount) throws Exception {
  165.           rowData = null;
  166.           boolean rowExist = true;
  167.          
  168.           try {
  169.                 if (metaData == null) {
  170.                       metaData = resultSet.getMetaData();
  171.                       columnCount = metaData.getColumnCount();
  172.                       rowExist = resultSet.next();
  173.                 }
  174.                
  175.                 rowData = new ModelData();               
  176.                
  177.                 if ( rowExist ) {
  178.                     for(int i=1; i<=columnCount; ++i){          
  179.                         rowData.putString( metaData.getColumnLabel(i), resultSet.getString(i) );
  180.                     }
  181.                 }
  182.                
  183.           } catch (SQLException e) {
  184.               close();
  185.              
  186.               throw e;
  187.           }
  188.          
  189.           return rowData;
  190.       }
  191.      
  192.       /**
  193.        * @throws Exception
  194.        */
  195.       public ModelData getRow() throws Exception {
  196.           return getRow(null, 0);
  197.       }
  198.      
  199.       /**
  200.        * @throws Exception
  201.        */
  202.       public ArrayList<ModelData> getResult() throws Exception {
  203.           resultData = new ArrayList<ModelData>();       
  204.          
  205.           try {
  206.               ResultSetMetaData metaData = resultSet.getMetaData();
  207.               resultData = new ArrayList<ModelData>();
  208.               int columnCount = metaData.getColumnCount();
  209.              
  210.               while ( resultSet.next() ) {
  211.                   resultData.add( getRow( metaData, columnCount ) );
  212.               }
  213.           } catch (SQLException e) {
  214.               close();           
  215.              
  216.               throw e;
  217.           }
  218.  
  219.           return resultData;
  220.       }
  221.      
  222.       /**
  223.        * @throws Exception
  224.        */
  225.       public void close() throws Exception {
  226.           if (resultSet != null) {
  227.               resultSet.close();
  228.           }
  229.  
  230.           if (statement != null) {
  231.               statement.close();
  232.           }
  233.       }
  234.      
  235.       public SQLDriver addBoolean(boolean value) throws SQLException {
  236.           paramsCount++;
  237.           statement.setBoolean(paramsCount, value);
  238.          
  239.           return this;
  240.       }
  241.      
  242.       public SQLDriver addInt(int value) throws SQLException {
  243.           paramsCount++;
  244.           statement.setInt(paramsCount, value);
  245.          
  246.           return this;
  247.       }
  248.      
  249.       public SQLDriver addFloat(float value) throws SQLException {
  250.           paramsCount++;
  251.           statement.setFloat(paramsCount, value);
  252.          
  253.           return this;
  254.       }
  255.      
  256.       public SQLDriver addString(String value) throws SQLException {
  257.           paramsCount++;
  258.           statement.setString(paramsCount, value);
  259.          
  260.           return this;
  261.       }
  262.      
  263.      
  264.      
  265.       public void setLog(ILog value) {
  266.           log = value;
  267.       }
  268.        
  269.       protected ILog getLog() {
  270.           if ( log == null ) {
  271.               throw new NullPointerException("log is null");
  272.           }
  273.           return log;
  274.       }
  275.      
  276.       public void setConnectionPool(IConnectionPool value) {
  277.           connectionPool = value;
  278.       }
  279.        
  280.       protected IConnectionPool getConnectionPool() {
  281.           if ( connectionPool == null ) {
  282.               throw new NullPointerException("connectionPool is null");
  283.           }
  284.           return connectionPool;
  285.       }
  286.    
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement