· 8 years ago · Mar 07, 2018, 12:24 PM
1package tdt4140.gr1844.app.core;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.ResultSet;
6import java.sql.SQLException;
7import java.sql.Statement;
8
9public class dbExample {
10
11 public static void main(String[] args)
12 {
13 Connection connection = null;
14 try
15 {
16 // create a database connection
17 connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
18 Statement statement = connection.createStatement();
19 statement.setQueryTimeout(30); // set timeout to 30 sec.
20
21 statement.executeUpdate("drop table if exists person");
22 statement.executeUpdate("create table if not EXISTS users (id integer, role varchar, name varchar, email varchar, password_hash varchar," +
23 " salt varchar, PRIMARY KEY(id))");
24 statement.executeUpdate("create table if not EXISTS patientdata (id integer, patientID integer, doctorID integer, rating integer, extrainfo varchar," +
25 " timestamp integer, PRIMARY KEY(id), FOREIGN KEY(patientID) references users(id), FOREIGN KEY(doctorID) references users(id) )");
26/*
27 ResultSet rs = statement.executeQuery("select * from person");
28 while(rs.next())
29 {
30 // read the result set
31 System.out.println("name = " + rs.getString("name"));
32 System.out.println("id = " + rs.getInt("id"));
33 }*/
34 }
35 catch(SQLException e)
36 {
37 // if the error message is "out of memory",
38 // it probably means no database file is found
39 System.err.println(e.getMessage());
40 }
41 finally
42 {
43 try
44 {
45 if(connection != null)
46 connection.close();
47 }
48 catch(SQLException e)
49 {
50 // connection close failed.
51 System.err.println(e);
52 }
53 }
54 }
55 }