-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseConnection.kt
More file actions
144 lines (124 loc) · 4.39 KB
/
DatabaseConnection.kt
File metadata and controls
144 lines (124 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package plusone.plusone
/**
* Created by Gabriel on 6/10/17.
*/
import java.sql.*
import java.util.Properties
object DatabaseConnection {
// Constructor
init{
getConnection()
}
internal var conn: Connection? = null // connection to db
// log user in. This will create a CurrentUser(static) with the user that has logged in
fun loginUser(user:String, pass:String):Boolean{
if (verifyUser(user, pass)) {
var stmt: Statement? = null
var resultset: ResultSet? = null
try{
print("misterblister $conn")
stmt = conn!!.createStatement()
var query = "SELECT * FROM plusone.users WHERE username = '$user';"
resultset = stmt!!.executeQuery(query)
resultset!!.next()
CurrentUser.email = resultset.getString("email")
CurrentUser.name = resultset.getString("name")
CurrentUser.userLoggedIn = true
CurrentUser.username = user
return true
}
catch (ex: SQLException){
ex.printStackTrace()
}
}
return false
}
//Get the last event.id on the event table
var lastEventId = null
fun getLastIdEvent() {
var stmt: Statement? = null
var resultset: ResultSet? = null
try{
stmt = conn!!.createStatement()
var query = "SELECT event_id FROM plusone.event WHERE event_id = (SELECT MAX(event_id) FROM plusone.event);"
resultset = stmt!!.executeQuery(query)
resultset!!.next()
lastEventId = resultset.getString(1) as Nothing?
} catch (ex:SQLException) {
// TODO: log this
ex.printStackTrace()
} catch (ex: SQLException){
// TODO: log this
ex.printStackTrace()
}
}
//Send informations of the event to the DB
fun createEventDB(){
var stmt: Statement? = null
var resultset: ResultSet? = null
try{
stmt = conn!!.createStatement()
var query = "INSERT INTO plusone.event" +
"VALUES ('$lastEventId','$title','$description','$date','$end_date','$location','$type','$max_ppl','$min_ppl';"
resultset = stmt!!.executeQuery(query)
resultset!!.next()
} catch (ex:SQLException) {
// TODO: log this
ex.printStackTrace()
} catch (ex: SQLException){
// TODO: log this
ex.printStackTrace()
}
}
// Returns true if password matches user on server.
fun verifyUser(user:String, pass:String): Boolean {
var stmt: Statement? = null
var resultset: ResultSet? = null
try{
stmt = conn!!.createStatement()
var query = "SELECT * FROM plusone.users WHERE username = '$user';"
resultset = stmt!!.executeQuery(query)
resultset!!.next()
try {
return pass == resultset.getString("pw")
}
catch (ex:SQLException) {
// TODO: log this
ex.printStackTrace()
}
}
catch (ex: SQLException){
// TODO: log this
ex.printStackTrace()
}
return false
}
// TODO: registers user
fun registerUser(user:String, pass:String){
}
/**
* This method makes a connection to MySQL Server and initialises conn.
*/
private fun getConnection():Boolean {
val connectionProps = Properties()
connectionProps.put("user", "plusone")
connectionProps.put("password", "plusone")
try {
Class.forName("com.mysql.jdbc.Driver").newInstance()
conn = DriverManager.getConnection(
"jdbc:" + "mysql" + "://" +
"plusoneapp.ddns.net" +
":" + "3306" + "/" +
"",
connectionProps)
return true
} catch (ex: SQLException) {
// handle any errors
ex.printStackTrace()
} catch (ex: Exception) {
// handle any errors
ex.printStackTrace()
}
return false
}
}