package de.gurkengewuerz.termbin; import java.sql.*; /** * Created by gurkengewuerz.de on 08.04.2017. */ public class Database { private String sUrl = null; private int iTimeout = 30; private Connection conn = null; private Statement statement = null; public Database(String sUrlToLoad) throws Exception { setUrl(sUrlToLoad); setConnection(); setStatement(); } private void setUrl(String sUrlVar) { sUrl = sUrlVar; } private void setConnection() throws Exception { conn = DriverManager.getConnection("jdbc:sqlite:" + sUrl); } public Connection getConnection() { return conn; } private void setStatement() throws Exception { if (conn == null) { setConnection(); } statement = conn.createStatement(); statement.setQueryTimeout(iTimeout); // set timeout to 30 sec. } public PreparedStatement getPreparedStatement(String sql) throws SQLException { return conn.prepareStatement(sql); } public void executeUpdate(String instruction) throws SQLException { statement.executeUpdate(instruction); } public ResultSet executeQuery(String instruction) throws SQLException { return statement.executeQuery(instruction); } public void closeConnection() { try { conn.close(); } catch (Exception ignore) { } } }