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

Copyright © 2012-2016 Basis Technology. Generated on: Mon Jan 2 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.