Autopsy  3.1
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 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;
31 
35 public class SQLiteDBConnect {
36 
37  public String sDriver = "";
38  public String sUrl = null;
39  public int iTimeout = 30;
40  public Connection conn = null;
41  public Statement statement = null;
42  private static final Logger logger = Logger.getLogger(SQLiteDBConnect.class.getName());
43 
44  /* Stub constructor for quick instantiation o/t fly for using some of the ancillary stuff */
45  public SQLiteDBConnect() {
46  }
47 
48  /* quick and dirty constructor to test the database passing the DriverManager name and the fully loaded url to handle */
49  /* NB this will typically be available if you make this class concrete and not abstract */
50  public SQLiteDBConnect(String sDriverToLoad, String sUrlToLoad) throws SQLException {
51  init(sDriverToLoad, sUrlToLoad);
52  }
53 
54  public final void init(String sDriverVar, String sUrlVar) throws SQLException {
55  setDriver(sDriverVar);
56  setUrl(sUrlVar);
57  setConnection();
58  setStatement();
59  }
60 
61  private void setDriver(String sDriverVar) {
62  sDriver = sDriverVar;
63  }
64 
65  private void setUrl(String sUrlVar) {
66  sUrl = sUrlVar;
67  }
68 
69  public void setConnection() throws SQLException {
70  try {
71  Class.forName(sDriver);
72  }
73  catch (ClassNotFoundException e) {
74 
75  }
76  conn = DriverManager.getConnection(sUrl);
77  }
78 
79  public Connection getConnection() {
80  return conn;
81  }
82 
83  public void setStatement() throws SQLException {
84  if (conn == null) {
85  setConnection();
86  }
87  statement = conn.createStatement();
88  statement.setQueryTimeout(iTimeout); // set timeout to 30 sec.
89  }
90 
91  public Statement getStatement() {
92  return statement;
93  }
94 
95  public void executeStmt(String instruction) throws SQLException {
96  statement.executeUpdate(instruction);
97  }
98 
99 // processes an array of instructions e.g. a set of SQL command strings passed from a file
100 //NB you should ensure you either handle empty lines in files by either removing them or parsing them out
101 // since they will generate spurious SQLExceptions when they are encountered during the iteration....
102  public void executeStmt(String[] instructionSet) throws SQLException {
103  for (int i = 0; i < instructionSet.length; i++) {
104  executeStmt(instructionSet[i]);
105  }
106  }
107 
108  public ResultSet executeQry(String instruction) throws SQLException {
109  return statement.executeQuery(instruction);
110  }
111 
112  public void closeConnection() {
113  try {
114  conn.close();
115  } catch (Exception ignore) {
116  }
117  }
118 }
SQLiteDBConnect(String sDriverToLoad, String sUrlToLoad)
final void init(String sDriverVar, String sUrlVar)
static Logger getLogger(String name)
Definition: Logger.java:131

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.