1 | package test.drs_tst.recs.utils.db; |
---|
2 | |
---|
3 | import java.sql.Connection; |
---|
4 | import java.sql.DriverManager; |
---|
5 | import java.sql.ResultSet; |
---|
6 | import java.sql.SQLException; |
---|
7 | import java.sql.Statement; |
---|
8 | |
---|
9 | public class SQLLiteManager { |
---|
10 | |
---|
11 | private static SQLLiteManager instance; |
---|
12 | |
---|
13 | public static String DB_PATH = "src/test/drs_tst/recs/data/all_db"; |
---|
14 | |
---|
15 | private Connection connection; |
---|
16 | |
---|
17 | private SQLLiteManager() { |
---|
18 | connection = null; |
---|
19 | try { |
---|
20 | // create a database connection |
---|
21 | Class.forName("org.sqlite.JDBC"); |
---|
22 | connection = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH); |
---|
23 | System.out.println(connection); |
---|
24 | } catch (ClassNotFoundException e1) { |
---|
25 | // TODO Auto-generated catch block |
---|
26 | e1.printStackTrace(); |
---|
27 | } catch (SQLException e) { |
---|
28 | // if the error message is "out of memory", |
---|
29 | // it probably means no database file is found |
---|
30 | System.err.println(e.getMessage()); |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | public static SQLLiteManager getManager() { |
---|
35 | if (instance == null) { |
---|
36 | instance = new SQLLiteManager(); |
---|
37 | } |
---|
38 | return instance; |
---|
39 | } |
---|
40 | |
---|
41 | public ResultSet executeQuery(String query) { |
---|
42 | Statement statement; |
---|
43 | try { |
---|
44 | statement = connection.createStatement(); |
---|
45 | //statement.setQueryTimeout(30); |
---|
46 | return statement.executeQuery(query); |
---|
47 | } catch (SQLException e) { |
---|
48 | // TODO Auto-generated catch block |
---|
49 | e.printStackTrace(); |
---|
50 | return null; |
---|
51 | } |
---|
52 | |
---|
53 | } |
---|
54 | } |
---|