Coverage Report - org.owasp.dependencycheck.utils.DBUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
DBUtils
39%
9/23
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.DatabaseException;
 27  
 
 28  
 /**
 29  
  *
 30  
  * @author Jeremy Long <jeremy.long@owasp.org>
 31  
  */
 32  
 public final class DBUtils {
 33  
 
 34  
     /**
 35  
      * The logger.
 36  
      */
 37  1
     private static final Logger LOGGER = Logger.getLogger(DBUtils.class.getName());
 38  
 
 39  
     /**
 40  
      * Private constructor for a utility class.
 41  
      */
 42  
     private DBUtils() {
 43  
     }
 44  
 
 45  
     /**
 46  
      * Returns the generated integer primary key for a newly inserted row.
 47  
      *
 48  
      * @param statement a prepared statement that just executed an insert
 49  
      * @return a primary key
 50  
      * @throws DatabaseException thrown if there is an exception obtaining the key
 51  
      */
 52  
     public static int getGeneratedKey(PreparedStatement statement) throws DatabaseException {
 53  0
         ResultSet rs = null;
 54  0
         int id = 0;
 55  
         try {
 56  0
             rs = statement.getGeneratedKeys();
 57  0
             rs.next();
 58  0
             id = rs.getInt(1);
 59  0
         } catch (SQLException ex) {
 60  0
             throw new DatabaseException("Unable to get primary key for inserted row");
 61  
         } finally {
 62  0
             closeResultSet(rs);
 63  0
         }
 64  0
         return id;
 65  
     }
 66  
 
 67  
     /**
 68  
      * Closes the given statement object ignoring any exceptions that occur.
 69  
      *
 70  
      * @param statement a Statement object
 71  
      */
 72  
     public static void closeStatement(Statement statement) {
 73  33
         if (statement != null) {
 74  
             try {
 75  33
                 statement.close();
 76  0
             } catch (SQLException ex) {
 77  0
                 LOGGER.log(Level.FINEST, statement.toString(), ex);
 78  33
             }
 79  
         }
 80  33
     }
 81  
 
 82  
     /**
 83  
      * Closes the result set capturing and ignoring any SQLExceptions that occur.
 84  
      *
 85  
      * @param rs a ResultSet to close
 86  
      */
 87  
     public static void closeResultSet(ResultSet rs) {
 88  35
         if (rs != null) {
 89  
             try {
 90  35
                 rs.close();
 91  0
             } catch (SQLException ex) {
 92  0
                 LOGGER.log(Level.FINEST, rs.toString(), ex);
 93  35
             }
 94  
         }
 95  35
     }
 96  
 }