-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.java
131 lines (117 loc) · 3.37 KB
/
User.java
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
package com.otrocol.app;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import javax.servlet.ServletException;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
public class User{
public String message;
public HashMap<String,String> VALUES = new HashMap<String,String>();
public DataSource d;
public Connection con;
public Context i;
public Context e;
public Statement st;
public User(){
};
public void insertValue(String fieldName, String fieldValue){
VALUES.put(fieldName, fieldValue);
}
public int generateId(){
int newId = 1;
try{
st = con.createStatement();
ResultSet rs = st.executeQuery("select * from users order by userid desc");
rs.next();
int prevId = rs.getInt("userid");
newId = prevId+1;
st.close();
}catch(SQLException s){}
return (newId);
};
public void closeDbConnection(){
try{
con.close();
}catch(SQLException s){
message = "Registration failed!Check stacktrace.";
s.printStackTrace();
};
}
public void startDbConnection(){
try{
i = new InitialContext(); //javax.naming
e = (Context) i.lookup("java:/comp/env"); //javax.naming
d = (DataSource) e.lookup("jdbc/mydb"); //javax.sql
try{
con = d.getConnection(); //java.sql
}catch(SQLException s){
message = "Registration failed!Check stacktrace.";
s.printStackTrace();
};
}catch(NamingException n){
message = "Registration failed!Check stacktrace.";
n.printStackTrace();
};
}
public void insertUser(int id){
try{
st = con.createStatement(); //java.sql
String insertSql;
insertSql = "insert into users values("+id+",'"+VALUES.get("userName")+"','"+VALUES.get("passWord")+"','"+VALUES.get("email")+"','"+VALUES.get("firstName")+"','"+VALUES.get("lastName")+"','"+VALUES.get("age")+"','"+ VALUES.get("avatar")+"','"+false+"')";
st.executeUpdate(insertSql);
st.close();
}catch(SQLException s){
message = "Registration failed!Check stacktrace.";
s.printStackTrace();
};
}
public void generateUser(HttpServletRequest request,HttpServletResponse response, JspWriter out){
message = "Registration successful";
startDbConnection();
insertUser(generateId());
closeDbConnection();
};
private boolean check(String userName, String passWord) throws SQLException{
st = con.createStatement(); //java.sql
ResultSet rs = st.executeQuery("select * from users where username = '" + userName+"' and password = '" + passWord + "'");
int count=0;
while(rs.next()){
count++;
}
if(count>0){
return true;
}
else {
return false;
}
}
public boolean exists(String userName, String passWord) throws SQLException{
startDbConnection();
boolean exists = check(userName, passWord);
closeDbConnection();
return exists;
}
public void confirm(String user, String code){
startDbConnection();
try{
Statement st3 = con.createStatement(); //java.sql
String insertSql;
insertSql = "update users set confirmed = true where username = '"+user+"'";
if(code.equals("18934_897600")){
st3.executeUpdate(insertSql);
}
}catch(SQLException s){
message = "Registration failed!Check stacktrace.";
s.printStackTrace();
};
closeDbConnection();
}
};