-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaveData.java
325 lines (290 loc) · 10.2 KB
/
SaveData.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package ita;
import javax.swing.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
public class SaveData {
public void dispDB(int a) {
String dis="Date Time UserName PassWord Mail_Id Action\n";
int count=0,countu=0;
Connection con=null;
try {
Scanner s=new Scanner(System.in);
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/trying","root","");
if(!con.isClosed());
Statement stmt;
stmt=con.createStatement();
if(a==1) {
ResultSet rsc=stmt.executeQuery("SELECT count(*) FROM `trying_to_log` WHERE 1");
while(rsc.next())
count=Integer.parseInt(rsc.getString(1));
if(count>0) {
String data[][]=new String[count][5];
String col[]= {"UserName","Password","Gmail ID"," Action"," Time"};
int row=0;
ResultSet rs=stmt.executeQuery("SELECT * FROM `trying_to_log` WHERE 1");
while(rs.next()) {
//dis+="\'"+rs.getString(2)+"\' has Password= "+rs.getString(3)+" and gmail_id= "+rs.getString(5)+" has "+rs.getString(6)+" at "+rs.getString(1)+"\n ";
data[row][0]=rs.getString(2);
data[row][1]=rs.getString(3);
data[row][2]=rs.getString(5);
data[row][3]=rs.getString(6);
data[row][4]=rs.getString(1);
row++;
}
JFrame f=new JFrame();
JTable jt=new JTable(data,col);
JScrollPane sp=new JScrollPane(jt);
jt.setBounds(100, 100, 800, 400);
//f.add(jt);
f.add(sp);
f.setSize(1000, 1000);
f.setVisible(true);
}
}
else if(a==2) {
ResultSet rscu=stmt.executeQuery("SELECT count(*) FROM `log_in` WHERE 1");
while(rscu.next())
countu=Integer.parseInt(rscu.getString(1));
if(countu>0) {
String data[][]=new String[countu][3];
String col[]= {"UserName","Password","Gmail ID"};
int row=0;
ResultSet rs=stmt.executeQuery("SELECT * FROM `log_in` WHERE 1");
while(rs.next()) {
//dis+="\'"+rs.getString(2)+"\' has Password= "+rs.getString(3)+" and gmail_id= "+rs.getString(5)+" has "+rs.getString(6)+" at "+rs.getString(1)+"\n ";
data[row][0]=rs.getString(1);
data[row][1]=rs.getString(2);
data[row][2]=rs.getString(3);
row++;
}
JFrame f=new JFrame();
JTable jt=new JTable(data,col);
JScrollPane sp=new JScrollPane(jt);
jt.setBounds(100, 100, 800, 400);
f.add(sp);
f.setSize(1000, 1000);
f.setVisible(true);
}
}
con.close();
}
catch(Exception e) {
System.out.println(e);
}
}
public void signup(String usna, String pasw, String log) {
Connection con=null;
try {
Scanner s=new Scanner(System.in);
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/trying","root","");
if(!con.isClosed());
Statement stmt;
DateTimeFormatter dft=DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
DateTimeFormatter dft1=DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDateTime now=LocalDateTime.now();
stmt=con.createStatement();
String email123 = "";
ResultSet rs=stmt.executeQuery("SELECT `gmail` FROM `log_in` WHERE username='"+usna+"' ");
while(rs.next())
email123=rs.getString(1);
stmt.executeUpdate("INSERT INTO `trying_to_log`(`DateTime`, `date`, `UserName`, `PassWord`, `gmail_id`, `Trytolog`) VALUES ('"+dft.format(now)+"','"+dft1.format(now)+"','"+usna+"','"+pasw+"','"+email123+"','"+log+"')");
con.close();
}
catch(Exception e) {
System.out.println(e);
}
}
public void log(String email,String usna, String pasw) {
Connection con=null;
try {
Scanner s=new Scanner(System.in);
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/trying","root","");
if(!con.isClosed());
Statement stmt;
stmt=con.createStatement();
stmt.executeUpdate("INSERT INTO `log_in`(`UserName`, `PassWord`, `gmail`) VALUES ('"+usna+"','"+pasw+"','"+email+"')");
con.close();
}
catch(Exception e) {
System.out.println(e);
} }
public void trytolog(String email,String usna, String pasw) {
Connection con=null;
try {
Scanner s=new Scanner(System.in);
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/trying","root","");
if(!con.isClosed());
Statement stmt;
DateTimeFormatter dft=DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
DateTimeFormatter dft1=DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDateTime now=LocalDateTime.now();
stmt=con.createStatement();
stmt.executeUpdate("INSERT INTO `trying_to_log`(`DateTime`, `date`, `UserName`, `PassWord`, `gmail_id`, `Trytolog`) VALUES ('"+dft.format(now)+"','"+dft1.format(now)+"','"+usna+"','"+pasw+"','"+email+"','tried')");
con.close();
}
catch(Exception e) {
System.out.println(e);
}
}
public int checkemail(String email,String User) {
int c=0,n=0,d=0;
Connection con=null;
try {
Scanner s=new Scanner(System.in);
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/trying","root","");
if(!con.isClosed());
Statement stmt;
LocalDateTime now=LocalDateTime.now();
stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT `gmail`,`username` FROM `log_in` WHERE 1 ");
while(rs.next()) {
if(rs.getString(1).equalsIgnoreCase(email))
c++;
if(rs.getString(2).equalsIgnoreCase(User))
n++;
}
con.close();
}
catch(Exception e) {
System.out.println(e);
}
if(c>0 || n>0) {
if(c>n)
return c;
else
return n;
}
else
return 0;
}
public int checkus(String us) {
int c=0;
Connection con=null;
try {
Scanner s=new Scanner(System.in);
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/trying","root","");
if(!con.isClosed());
Statement stmt;
LocalDateTime now=LocalDateTime.now();
stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT `username` FROM `log_in` WHERE username='"+us+"'");
while(rs.next()) {
if(rs.getString(1).compareTo(us)==0)
c++;
}
con.close();
}
catch(Exception e) {
System.out.println(e);
}
return c;
}
public void createdb(String us) {
int c=0;
Connection con=null;
try {
Scanner s=new Scanner(System.in);
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/trying","root","");
if(!con.isClosed());
Statement stmt;
stmt=con.createStatement();
stmt.executeUpdate("CREATE TABLE `trying`.`"+us+"` ( `file` BLOB NOT NULL ) ENGINE = InnoDB; ");
con.close();
System.out.println("database created succesfully\n");
}
catch(Exception e) {
System.out.println(e);
}
}
public void insertfile(String us,String fp) {
Connection con=null;
try {
Scanner s=new Scanner(System.in);
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/trying","root","");
if(!con.isClosed());
Statement stmt;
stmt=con.createStatement();
String sql = "INSERT INTO `"+us+"`(`file`) VALUES (?)";
InputStream is = new FileInputStream(new File(fp));
PreparedStatement ps = con.prepareStatement(sql);
ps.setBlob(1, is);
// ps.set
int row = ps.executeUpdate();
if (row > 0) {
JFrame mes=new JFrame("Inserted");
JOptionPane.showMessageDialog(mes,"The selected file is inserted in database sucessfully","UpdatedSuccessfully",JOptionPane.PLAIN_MESSAGE);
}
else {
JFrame mes=new JFrame("Alert");
JOptionPane.showMessageDialog(mes,"The selected file is not inserted in database sucessfully","Alert",JOptionPane.WARNING_MESSAGE);
}
con.close();
}
catch(Exception e) {
System.out.println(e);
}
}
public int checkuspas(String us,String pas) {
int c=0;
Connection con=null;
try {
Scanner s=new Scanner(System.in);
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/trying","root","");
if(!con.isClosed());
Statement stmt;
LocalDateTime now=LocalDateTime.now();
stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT `username`,`PassWord` FROM `log_in` WHERE username='"+us+"'");
while(rs.next()) {
if((rs.getString(1).compareTo(us)==0)&&(rs.getString(2).compareTo(pas)==0))
c++;
}
con.close();
}
catch(Exception e) {
System.out.println(e);
}
return c;
}
/*public static void main(String[] args) {
Connection con=null;
try {
Scanner s=new Scanner(System.in);
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/trying","root","");
if(!con.isClosed());
System.out.println("Connected");
Statement stmt;
DateTimeFormatter dft=DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
DateTimeFormatter dft1=DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDateTime now=LocalDateTime.now();
stmt=con.createStatement();
stmt.executeUpdate("INSERT INTO `trying_to_log`(`date`, `DateTime`, `UserName`, `PassWord`) VALUES ('"+dft1.format(now) +"',' " +dft.format(now)+"','"+us+"','"+pw+"')");
ResultSet rs=stmt.executeQuery("Select * from trying_to_log where date = '"+dft1.format(now)+"' ");
while(rs.next())
System.out.println(rs.getString(1)+" "+rs.getString(2)+", "+rs.getString(3)+", "+rs.getString(4) );
con.close();
}
catch(Exception e) {
System.out.println(e);
}
}*/
}