Autopsy  4.20.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
SQLiteDBConnect.java
Go to the documentation of this file.
1 /*
2  *
3  * Autopsy Forensic Browser
4  *
5  * Copyright 2012-2019 Basis Technology Corp.
6  *
7  * Copyright 2012 42six Solutions.
8  * Contact: aebadirad <at> 42six <dot> com
9  * Project Contact/Architect: carrier <at> sleuthkit <dot> org
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 package org.sleuthkit.autopsy.coreutils;
24 
25 import java.sql.Connection;
26 import java.sql.DriverManager;
27 import java.sql.ResultSet;
28 import java.sql.SQLException;
29 import java.sql.Statement;
30 import java.util.logging.Level;
31 
37 public class SQLiteDBConnect implements AutoCloseable {
38 
39  private static final Logger logger = Logger.getLogger(SQLiteDBConnect.class.getName());
40  private static final int STMT_EXEC_TIMEOUT_SECS = 30;
41 
54  public SQLiteDBConnect(String driver, String url) throws SQLException {
55  sDriver = driver;
56  sUrl = url;
57  try {
58  Class.forName(sDriver);
59  } catch (ClassNotFoundException ex) {
60  throw new SQLException(ex);
61  }
62  conn = DriverManager.getConnection(sUrl);
63  statement = conn.createStatement();
64  statement.setQueryTimeout(STMT_EXEC_TIMEOUT_SECS);
65  }
66 
75  public void executeStmt(String sqlStatement) throws SQLException {
76  statement.executeUpdate(sqlStatement);
77  }
78 
87  public void executeStmt(String[] sqlStatements) throws SQLException {
88  for (String stmt : sqlStatements) {
89  executeStmt(stmt);
90  }
91  }
92 
104  public ResultSet executeQry(String sqlStatement) throws SQLException {
105  return statement.executeQuery(sqlStatement);
106  }
107 
114  public void closeConnection() {
115  if (conn == null) {
116  return;
117  }
118  try {
119  conn.close();
120  } catch (SQLException ex) {
121  logger.log(Level.WARNING, "Unable to close connection to SQLite DB at " + sUrl, ex);
122  }
123  }
124 
125  @Override
126  public void close() {
127  closeConnection();
128  }
129 
130  /*
131  * Partially constructs a utility object for doing basic operations on a
132  * SQLite database. The object is not in a usable state. Further
133  * initialization is required. See methods below.
134  *
135  * @deprecated Do not use.
136  */
137  @Deprecated
138  public SQLiteDBConnect() {
139  }
140 
153  @Deprecated
154  public final void init(String driver, String url) throws SQLException {
155  sDriver = driver;
156  sUrl = url;
157  closeConnection();
158  setConnection();
159  setStatement();
160  }
161 
171  @Deprecated
172  public void setConnection() throws SQLException {
173  if (sDriver == null || sDriver.isEmpty() || sUrl == null || sUrl.isEmpty()) {
174  throw new SQLException("Driver and or databse URl not initialized");
175  }
176  closeConnection();
177  try {
178  Class.forName(sDriver);
179  } catch (ClassNotFoundException ex) {
180  throw new SQLException(ex);
181  }
182  conn = DriverManager.getConnection(sUrl);
183  }
184 
192  @Deprecated
193  public Connection getConnection() {
194  return conn;
195  }
196 
206  @Deprecated
207  public void setStatement() throws SQLException {
208  if (conn == null) {
209  setConnection();
210  }
211  statement = conn.createStatement();
212  statement.setQueryTimeout(iTimeout);
213  }
214 
223  @Deprecated
224  public Statement getStatement() {
225  return statement;
226  }
227 
228  /*
229  * The lack of encapsulation of these fields is an error. Access to them
230  * outside of instances of this class is deprecated.
231  *
232  * @deprecated Do not access.
233  */
234  public String sDriver = "";
235  public String sUrl = null;
237  public Connection conn = null;
238  public Statement statement = null;
239 
240 }
final void init(String driver, String url)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2022 Basis Technology. Generated on: Tue Aug 1 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.