This repository has been archived by the owner on Mar 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.java
224 lines (200 loc) · 6.18 KB
/
Database.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import java.sql.*;
public class Database{
private Connection con;
private Encryption encryption;
private Statement st;
private String query;
private ResultSet rs;
private int i=0;
private int id;
private Handler handler;
public Database(Handler handler){
this.handler= handler;
encryption = new Encryption();
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/runnu","root","");
st = con.createStatement();
}catch(Exception ex){
System.out.println("Error : "+ex);
}
setI();
}
public void insert(long score, String username){
try{
query = "INSERT INTO highscore (`Player_Id`, `Score`, `Time`, `Level`) VALUES ('"+ getPlayerId(username) +"', '"+score+"', '7', '1')";
st.executeUpdate(query);
}catch(Exception ex){
System.out.println("Error insert " + ex);
}
}
public boolean insert(String name, String password){
try{
query = "INSERT INTO player_information (`Player_Id`, `User_Name`, `Password`) VALUES ('"+ (++i) +"', '"+name+"', '"+encryption.toMD5(password)+"')";
st.executeUpdate(query);
query = "INSERT INTO highscore (`Player_Id`, `Score`, `Time`, `Level`, `Medal`) VALUES ('"+ (i) +"' , '0', '0', '1', '0')";
st.executeUpdate(query);
query = "INSERT INTO highscore (`Player_Id`, `Score`, `Time`, `Level`, `Medal`) VALUES ('"+ (i) +"' , '0', '0', '2', '0')";
st.executeUpdate(query);
query = "INSERT INTO highscore (`Player_Id`, `Score`, `Time`, `Level`, `Medal`) VALUES ('"+ (i) +"' , '0', '0', '3', '0')";
st.executeUpdate(query);
id = i;
return true;
}catch(Exception ex){
System.out.println("Error insert " + ex);
return false;
}
}
public void setI(){
try{
String query = "SELECT * FROM `player_information`";
rs = st.executeQuery(query);
while(rs.next()){
i = rs.getInt("Player_Id");
}
}catch(SQLException ex){
System.out.println(ex);
}
}
public void update(long score, String username){
int count = 0;
System.out.println("scores updated");
try {
query = "UPDATE highscore SET Score ="+score+" where Player_Id =" + getPlayerId(username);
System.out.println("Done");
count = st.executeUpdate(query);
System.out.println("Updated queries: "+count);
}catch (SQLException e){
e.printStackTrace();
}
}
public void updateHighscore(long score,int timeEllapsed, int levelIndex, int medal, String username){
int count = 0;
System.out.println("scores updated");
try {
String query = "UPDATE highscore SET Score ="+score+", Time ="+ timeEllapsed+", Medal ="+medal+" where Level =" + levelIndex +" AND Player_Id ="+ getPlayerId(username);
count = st.executeUpdate(query);
}catch (SQLException e){
e.printStackTrace();
}
}
public void select(){
try{
String query = "SELECT * FROM `player_information`";
rs = st.executeQuery(query);
System.out.println("Records Displayed");
while(rs.next()){
String name=rs.getString("User_Name");
int levelflag = rs.getInt("Level_flag");
}
}catch(SQLException ex){
System.out.println(ex);
}
}
public boolean isUsernameTaken(String name){
try{
String query = "SELECT * FROM `player_information`";
rs = st.executeQuery(query);
while(rs.next()){
if(name.toUpperCase().equals(rs.getString("User_Name").toUpperCase())){
return true;
}
}
}catch(SQLException ex){
System.out.println(ex);
return true;
}
return false;
}
public boolean isPasswordValid(String username, String password){
try{
String query = "SELECT * FROM `player_information`";
rs = st.executeQuery(query);
while(rs.next()){
if(username.toUpperCase().equals(rs.getString("User_Name").toUpperCase())){
if(encryption.toMD5(password).equals(rs.getString("Password"))){
id = i;
return true;
}
}
}
}catch(SQLException ex){
System.out.println(ex);
return true;
}
return false;
}
public int getPlayerId(String username){
try{
String query = "SELECT * FROM `player_information`";
rs = st.executeQuery(query);
while(rs.next()){
if(username.toUpperCase().equals(rs.getString("User_Name").toUpperCase())){
return rs.getInt("Player_Id");
}
}
}catch(SQLException ex){
System.out.println(ex);
}
return 0;
}
public String getUsername(int id){
try{
String query = "SELECT * FROM `player_information`";
rs = st.executeQuery(query);
while(rs.next()){
if(rs.getInt("Player_Id") == id){
return rs.getString("User_Name");
}
}
}catch(SQLException ex){
System.out.println(ex);
}
return "0";
}
public long getCurrentScore(int levelIndex){
try{
String query = "SELECT * FROM `highscore`";
rs = st.executeQuery(query);
while(rs.next()){
if(rs.getInt("Player_Id") == id && rs.getInt("Level") == levelIndex){
return rs.getInt("Score");
}
}
}catch(SQLException ex){
System.out.println(ex);
}
return 0;
}
public boolean hasMedal(int levelIndex){
try{
String query = "SELECT * FROM `highscore`";
rs = st.executeQuery(query);
while(rs.next()){
if(rs.getInt("Player_Id") == id && rs.getInt("Level") == levelIndex){
if(rs.getInt("Medal") > 0){
return true;
}
}
}
}catch(SQLException ex){
System.out.println(ex);
}
return false;
}
//fetching from database ----------> highscore <------------
public ResultSet getResultHighscore(int levelIndex){
try{
String sql = "SELECT Player_Id, Score, Time, Level FROM Highscore where Level="+levelIndex+" ORDER BY Score DESC";
ResultSet rs = st.executeQuery(sql);
Utility.writeFile(rs);
return rs;
}catch(SQLException ex){
System.out.println(ex);
}
return null;
}
public int getId(){
return id;
}
}