-
Notifications
You must be signed in to change notification settings - Fork 0
/
jdbc.gs
62 lines (47 loc) · 1.58 KB
/
jdbc.gs
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
/* host ip of the database -- replace <host.ip.addr> */
var host = 'host.ip.addr';
/* port number to conenct to -- default port 3306 */
var port = '3306';
/* MySQL user to authenticate as -- replace <username> */
var user = 'username';
/* password of that MySQL user -- replace <password> */
var pwd = 'password';
/* name of the database to use -- replace <db_name> */
var db = 'db_name';
function insertSubmission(input) {
var fields = ['discord_name', 'real_name', 'email_address'];
var values = ['?', '?', '?'];
// Add zID to statement if submitted
if (input[zID].toString().length > 0) {
fields.push('zid');
values.push('?');
}
// Add phone number to statement if submitted
if (input[phone].toString().length > 0) {
fields.push('phone_number');
values.push('?');
}
// Make connection
var info = '?useUnicode=yes&characterEncoding=UTF-8';
var url = 'jdbc:mysql://' + host + ':' + port + '/' + db + info;
var conn = Jdbc.getConnection(url, user, pwd);
// Prepare statement
var stmt = conn.prepareStatement('INSERT INTO submissions '
+ '(' + fields.join() + ') '
+ 'values (' + values.join() + ')');
stmt.setString(1, input[discord]);
stmt.setString(2, input[name]);
stmt.setString(3, input[email]);
// Set zID
if (fields.includes('zid')) {
stmt.setString(4, input[zID]);
}
// Set phone number
if (fields.includes('phone_number')) {
stmt.setString(fields.length, input[phone]);
}
// Execute statement
var results = stmt.executeUpdate();
// Log result
Logger.log(results + " rows affected");
}