· 8 years ago · Feb 01, 2018, 03:54 PM
1public class DbConnection {
2 private static DbConnection ourInstance = new DbConnection();
3
4 public static DbConnection getInstance() {
5 return ourInstance;
6 }
7
8 private final static String DRIVER = "org.sqlite.JDBC";
9 private final static String DATABASE = "jdbc:sqlite:Test.db";
10
11 private Connection connection = null;
12
13 private DbConnection() {
14 try {
15 Class.forName(DRIVER);
16 connection = DriverManager.getConnection(DATABASE);
17 createTable();
18 } catch (ClassNotFoundException e) {
19 e.printStackTrace();
20 } catch (SQLException e) {
21 e.printStackTrace();
22 }
23 }
24
25 public Connection getConnection() {
26 return connection;
27 }
28
29 public void createTable()
30 {
31 if (connection == null)
32 {
33 return;
34 }
35
36 try {
37 String sqlDoctor = "CREATE TABLE IF NOT EXISTS Doctor (" +
38 "id INTEGER PRIMARY KEY NOT NULL," +
39 "name VARCHAR(30)," +
40 "surname VARCHAR(30)," +
41 "specialization VARCHAR(20)" +
42 ")";
43
44 String sqlPatient = "CREATE TABLE IF NOT EXISTS Patient (" +
45 "id INTEGER PRIMARY KEY NOT NULL," +
46 "name VARCHAR(30)," +
47 "surname VARCHAR(30)," +
48 "disease VARCHAR(20)" +
49 ")";
50
51 String sqlVisit = "CREATE TABLE IF NOT EXISTS Visit (" +
52 "id INTEGER PRIMARY KEY NOT NULL," +
53 "doctor_id INTEGER NOT NULL," +
54 "FOREIGN KEY (doctor_id) REFERENCES Doctor (id)," +
55 "patient_id INTEGER NOT NULL," +
56 "FOREIGN KEY (patient_id) REFERENCES Patient (id),"+
57 "cost DOUBLE NOT NULL," +
58 "date DATE" +
59 ")";
60
61 Statement statement = connection.createStatement();
62 statement.execute(sqlDoctor);
63 statement.execute(sqlPatient);
64 statement.execute(sqlVisit);
65
66 } catch (SQLException e) {
67 e.printStackTrace();
68 }
69
70 }
71
72}