Coverage Report - org.owasp.dependencycheck.utils.DBUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
DBUtils
33%
8/24
50%
2/4
2.5
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.utils;
 19  
 
 20  
 import java.sql.PreparedStatement;
 21  
 import java.sql.ResultSet;
 22  
 import java.sql.SQLException;
 23  
 import java.sql.Statement;
 24  
 import java.util.logging.Level;
 25  
 import java.util.logging.Logger;
 26  
 import org.owasp.dependencycheck.data.nvdcve.CveDB;
 27  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
 28  
 
 29  
 /**
 30  
  *
 31  
  * @author Jeremy Long <jeremy.long@owasp.org>
 32  
  */
 33  
 public final class DBUtils {
 34  
 
 35  
     /**
 36  
      * Private constructor for a utility class.
 37  
      */
 38  0
     private DBUtils() {
 39  0
     }
 40  
 
 41  
     /**
 42  
      * Returns the generated integer primary key for a newly inserted row.
 43  
      *
 44  
      * @param statement a prepared statement that just executed an insert
 45  
      * @return a primary key
 46  
      * @throws DatabaseException thrown if there is an exception obtaining the key
 47  
      */
 48  
     public static int getGeneratedKey(PreparedStatement statement) throws DatabaseException {
 49  0
         ResultSet rs = null;
 50  0
         int id = 0;
 51  
         try {
 52  0
             rs = statement.getGeneratedKeys();
 53  0
             rs.next();
 54  0
             id = rs.getInt(1);
 55  0
         } catch (SQLException ex) {
 56  0
             throw new DatabaseException("Unable to get primary key for inserted row");
 57  
         } finally {
 58  0
             closeResultSet(rs);
 59  0
         }
 60  0
         return id;
 61  
     }
 62  
 
 63  
     /**
 64  
      * Closes the given statement object ignoring any exceptions that occur.
 65  
      *
 66  
      * @param statement a Statement object
 67  
      */
 68  
     public static void closeStatement(Statement statement) {
 69  350
         if (statement != null) {
 70  
             try {
 71  350
                 statement.close();
 72  0
             } catch (SQLException ex) {
 73  0
                 Logger.getLogger(CveDB.class
 74  
                         .getName()).log(Level.FINEST, statement.toString(), ex);
 75  350
             }
 76  
         }
 77  350
     }
 78  
 
 79  
     /**
 80  
      * Closes the result set capturing and ignoring any SQLExceptions that occur.
 81  
      *
 82  
      * @param rs a ResultSet to close
 83  
      */
 84  
     public static void closeResultSet(ResultSet rs) {
 85  360
         if (rs != null) {
 86  
             try {
 87  360
                 rs.close();
 88  0
             } catch (SQLException ex) {
 89  0
                 Logger.getLogger(CveDB.class
 90  
                         .getName()).log(Level.FINEST, rs.toString(), ex);
 91  360
             }
 92  
         }
 93  360
     }
 94  
 }