forked from ramyaj876/SE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDB.java
More file actions
243 lines (225 loc) · 7.21 KB
/
DB.java
File metadata and controls
243 lines (225 loc) · 7.21 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package se;
import java.sql.*;
/**
*
* @author acer
*/
class DB {
Connection c;
Statement stmt;
void connDB()
{
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:UserInfo.db");
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Opened database successfully");
}
boolean createTable()
{
try {
stmt = c.createStatement();
String sql = "CREATE TABLE UInfo (" +
" ID CHAR(10) PRIMARY KEY NOT NULL," +
" NAME TEXT , " +
" Password CHAR(10) NOT NULL, " +
" HighScore INT DEFAULT(0)," +
" CaveColor INT check(CaveColor < 5 AND CaveColor > 0),"+
" CharColor INT check(CharColor < 5 AND CharColor > 0))";
stmt.executeUpdate(sql);
//stmt.close();
return true;
} catch (SQLException E)
{ System.out.println("Create Table Failed");
return false; }
}
boolean insertDB(String user, String name, String pass)
{
try {
stmt = c.createStatement();
String sql = "INSERT INTO UInfo(ID, NAME, Password)"+
"VALUES('"+user+"','"+name+"','"+pass+"')";
stmt.executeUpdate(sql);
//stmt.close();
//c.close();
return true;
} catch (SQLException E)
{ System.out.println("Insert Failed");
return false; }
}
boolean chkPass(String user, String pass)
{
try {
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT Password FROM UInfo where ID = '"+user+"'" );
String passD = rs.getString("Password");
if (passD.equals(pass))
return true;
else
return false;
} catch (SQLException E)
{
System.out.println("SQLException-chk Pass failed.");
return false;
}
}
int viewHS(String user)
{
try {
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT HighScore FROM UInfo where ID = '"+user+"'" );
return (rs.getInt("HighScore"));
} catch (Exception E)
{
System.out.println("Exception- view failed.");
return -1;
}
}
boolean updateHS(String user, int hs)
{
int HS = 0;
try {
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT * FROM UInfo where ID like '"+user+"'" );
while ( rs.next() ) {
HS = rs.getInt("HighScore");
}
}
catch (SQLException E)
{ System.out.println("Update Failed");
return false; }
//System.out.println(HS);
if (hs > HS) {
//stmt = c.createStatement();
String sql = "UPDATE UInfo set HighScore = "+hs+" where ID = '"+user+"'";
try {
stmt.executeUpdate(sql);
} catch (SQLException E)
{ System.out.println("Update Failed1");
return false; }
//stmt.close();
return true;
}
return false;
//c.close();
}
boolean updateColor(String user, int CaC, int ChC)
{
try
{
stmt = c.createStatement();
String sql = "UPDATE UInfo set CaveColor = "+CaC+" where ID = '"+user+"';";
String sql1 = "UPDATE UInfo set CharColor = "+ChC+" where ID = '"+user+"';";
stmt.executeUpdate(sql);
stmt.executeUpdate(sql1);
//stmt.close();
//c.commit();
System.out.println("Successful Color update");
return true;
} catch (SQLException E)
{
System.out.println(E);
return false;
}
}
short CacSelect(String user)
{
try {
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT Cavecolor FROM UInfo where ID = '"+user+"'" );
short Cac = rs.getShort("Cavecolor");
rs.close();
//stmt.close();
if (Cac == 0)
return -1;
return Cac;
} catch (SQLException E)
{ System.out.println("Select Failed");
return -1; }
}
short ChcSelect(String user)
{
try {
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT Charcolor FROM UInfo where ID = '"+user+"'" );
short Cac = rs.getShort("Charcolor");
rs.close();
//stmt.close();
if (Cac == 0)
return -1;
return Cac;
} catch (SQLException E)
{ System.out.println("Select Failed");
return -1; }
}
void selectInfo(String user)
{
try {
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT ID, Name, HighScore FROM UInfo" );
while ( rs.next() ) {
String id = rs.getString("ID");
String name = rs.getString("Name");
int hs = rs.getInt("HighScore");
System.out.println( "ID = " + id );
System.out.println( "NAME = " + name );
System.out.println( "HIGH SCORE = " + hs );
System.out.println();
}
rs.close();
//stmt.close();
} catch (SQLException E)
{ System.out.println("Select Failed"); }
}
void selectAll(String user)
{
try {
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT * FROM UInfo" );
while ( rs.next() ) {
String id = rs.getString("ID");
String name = rs.getString("Name");
String pass = rs.getString("Password");
int hs = rs.getInt("HighScore");
int CaC = rs.getInt("CaveColor");
int ChC = rs.getInt("CharColor");
System.out.println( "ID = " + id );
System.out.println( "NAME = " + name );
System.out.println( "HIGH SCORE = " + hs );
System.out.println( "CaveColor = " + CaC );
System.out.println( "CharColor = " + ChC );
System.out.println();
}
//rs.close();
//stmt.close();
//c.close();
} catch (SQLException E)
{ System.out.println("Select Failed"); }
}
void closeDB()
{
try {
c.close();
} catch (SQLException E)
{ System.out.println(E); }
}
public static void main(String Args[])
{
DB D = new DB();
D.connDB();
D.createTable();
boolean test; //= D.insertDB("admin", "Admin", "1234");
D.selectInfo("admin");
test = D.updateHS("admin", 20);
System.out.println(test);
D.selectInfo("admin");
//test = D.updateColor("admin", 1, 1);
D.selectAll("admin");
test = D.chkPass("admin", "878");
System.out.println(test);
D.closeDB();
}
}