· 4 years ago · Jul 20, 2021, 10:34 PM
1import java.util.Scanner;
2
3public class Main {
4 public static void main(String[] args) {
5 Scanner sc = new Scanner(System.in);
6 DBConnector dbc = new DBConnector("jdbc:mysql://192.168.0.116:3306",
7 "martin",
8 "password");
9
10// dbc.getData();
11// dbc.insertData();
12
13 dbc.createDB();
14 LoginScreen loginScreen = new LoginScreen();
15 }
16}
17//System.out.println("Please enter the first and last name of the person you want to add in the Database:");
18// dbc.insertData(sc.nextLine(), sc.nextLine());
19// dbc.getData();
20
21import javax.swing.*;
22import java.awt.*;
23import java.awt.event.ActionEvent;
24import java.awt.event.ActionListener;
25import java.util.HashMap;
26import java.util.Map;
27
28public class LoginScreen implements ActionListener {
29
30 /**
31 * Fields
32 */
33 private JButton button;
34 private JFrame frame;
35 private JPanel panel;
36 private JLabel usernameLabel;
37 private JLabel passwordLabel;
38 private JTextField userText;
39 private JPasswordField passwordText;
40
41 Map<String, String> accounts = DBConnector.getMap();
42
43 public LoginScreen() {
44 frame = new JFrame();
45 panel = new JPanel();
46
47 /**Frame setting*/
48 frame.setSize(350, 200);
49 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
50 frame.setTitle("User Verification");
51 frame.add(panel, BorderLayout.CENTER);
52
53 panel.setLayout(null);
54
55 /**Username Label*/
56 usernameLabel = new JLabel("Username:");
57 usernameLabel.setBounds(10, 20, 80, 25);
58 panel.add(usernameLabel);
59
60 /**Password Label*/
61 passwordLabel = new JLabel("Password:");
62 passwordLabel.setBounds(10, 50, 80, 25);
63 panel.add(passwordLabel);
64
65 /**Username Text Box*/
66 userText = new JTextField();
67 userText.setBounds(100, 20, 170, 25);
68 panel.add(userText);
69
70 /**Password Text Box*/
71 passwordText = new JPasswordField();
72 passwordText.setBounds(100, 50, 170, 25);
73 panel.add(passwordText);
74
75 /**Button*/
76 button = new JButton("Login");
77 button.setBounds(145, 80, 80, 25);
78 button.addActionListener(this);
79 panel.add(button);
80
81 /**Fill the map with accounts*/
82 //...
83 frame.setVisible(true);
84 }
85
86
87 @Override
88 public void actionPerformed(ActionEvent e) {
89 String username = userText.getText();
90 String password = passwordText.getText();
91
92 boolean isValidAccount = false;
93 String usernameMap, passwordMap;
94 for(int i = 0; i < accounts.size(); i++){
95 usernameMap = accounts.keySet().toArray()[i].toString();
96 passwordMap = accounts.values().toArray()[i].toString();
97 if(username.equals(usernameMap) && password.equals(passwordMap)){
98 System.out.println("Correct password!");
99 isValidAccount = true;
100 break;
101 }
102 }
103 if(!isValidAccount){
104 System.out.println("Incorrect Username or Password");
105 }
106 }
107}
108// private void fillMap() { //read from db / txt file
109// accounts.put("username", "password");
110// accounts.put("mrBeast", "youtube");
111// accounts.put("destroyerOfPu$$y123", "s3x");
112// accounts.put("martin_koleff", "parola20");
113// accounts.put("Boyko_Tikvata", "gerber");
114// accounts.put("Burgaska-Batka-1946", "acab");
115// accounts.put("Sezona_na_Dinite", "0157030609");
116// accounts.put("vojda27", "audiA62006");
117// }
118
119// String[] usernames = (String[]) accounts.values().toArray(); //TO BE FIXED
120// String[] passwords = (String[]) accounts.keySet().toArray();
121// for (int i = 0; i < accounts.size(); i++) {
122// if(usernames[i].equals(username) && passwords[i].equals(password)){
123// System.out.println("Cheese");
124// }
125// }
126
127import java.sql.*;
128import java.util.HashMap;
129import java.util.Map;
130import java.util.Scanner;
131
132public class DBConnector {
133 /**
134 * Fields
135 */
136
137 /**
138 * jdbcURL resets every time!!!
139 */
140 private String jdbcURL; //"jdbc:mysql://192.168.0.116:3306"
141 private String user; //"martin"
142 private String password; //"password"
143 private Connection connection = null;
144
145 private Scanner sc = new Scanner(System.in);
146 private boolean hasConnected = false;
147 private String tableName = null; //"test.test_table"
148 private StringBuilder sql = new StringBuilder();
149
150 private Statement statement;
151 private ResultSet result;
152 private PreparedStatement preparedStatement;
153
154 /**
155 * Getting all accounts from the DB
156 */
157 private static Map<String, String> accounts = new HashMap<>();
158
159
160 /**
161 * Constructor
162 */
163 public DBConnector(String jdbcURL, String user, String password) {
164 this.jdbcURL = jdbcURL;
165 this.user = user;
166 this.password = password;
167 }
168
169 /**
170 * Constructor Chaining
171 */
172 public DBConnector(String jdbcURL, String user, String password, String tableName) {
173 //constructor chaining...
174 this.tableName = tableName;
175 }
176
177 /**
178 * Getter for the HashMap
179 */
180 public static Map<String, String> getMap() {
181 return accounts;
182 }
183
184 /**
185 * Connect to DB server
186 */
187 private void connect() {
188 try {
189 Class.forName("com.mysql.cj.jdbc.Driver");
190 connection = DriverManager.getConnection(jdbcURL, user, password);
191
192 if (connection != null) {
193 System.out.println("Successfully connected to MySQL database test");
194 hasConnected = true;
195 }
196 } catch (SQLException | ClassNotFoundException ex) {
197 System.out.println("An error occurred while connecting MySQL database");
198 ex.printStackTrace();
199 }
200 }
201
202 /**
203 * Create DB if table with the tableName name doesn't exists
204 */
205 public void createDB() {
206 if (hasConnected) {
207 try {
208 if (tableName == null) {
209 setTableName();
210 }
211 sql.append("CREATE DATABASE IF NOT EXISTS ").append(tableName.split("[.]")[0]);
212// .append(tableName);//"test.test_table"
213 statement = connection.createStatement();
214
215 statement.execute(sql.toString());
216
217 fillMap();
218 } catch (SQLException e) { //SQLSyntaxErrorException
219 System.out.println("Database creation failed.");
220 e.printStackTrace();
221 }
222 } else {
223 connect();
224 createDB();
225 }
226 }
227
228 /**
229 * Set the table name of the DB (custom name) / can be removed
230 */
231 private void setTableName() {
232 System.out.println("Please type down your Database name:");
233 tableName = sc.nextLine();
234
235 if (tableName.equals("Default".toLowerCase())) {
236 tableName = "test.test_table";
237 }
238 }
239
240 /**
241 * Modify to get specific data (first / last name | username / password | everything)
242 */
243 public void getData() {
244 if (hasConnected) {
245 try {
246 statement = connection.createStatement();
247 resetLengthOfSQL(sql);
248
249 if (tableName == null) {
250 setTableName();
251 }
252
253 sql.append("SELECT * FROM ").append(tableName);
254
255 result = statement.executeQuery(sql.toString());
256 while ((result.next())) {
257 System.out.println(result.getString("first_name") + " " + result.getString("last_name"));
258 }
259 } catch (SQLException e) {
260 System.out.println("An error occurred while getting data from the MySQL database");
261 e.printStackTrace();
262 }
263 } else {
264 connect();
265 getData();
266 }
267 }
268
269 /**
270 * SQL string resetter
271 */
272 private void resetLengthOfSQL(StringBuilder sql) {
273 sql.setLength(0);
274 }
275
276 /**
277 * Insert a row in DB
278 */
279 public void insertData(String... str) {
280 if (hasConnected) {
281 resetLengthOfSQL(sql);
282
283 if (tableName == null) {
284 setTableName();
285 }
286
287 sql.append("INSERT INTO ").append(tableName).append(" VALUES (DEFAULT, ?, ?, ?, ?)");
288 try {
289 preparedStatement = connection.prepareStatement(sql.toString());
290 preparedStatement.setString(1, str[0]);
291 preparedStatement.setString(2, str[1]);
292 preparedStatement.setString(3, str[3]);
293 preparedStatement.setString(4, str[4]);
294
295 preparedStatement.execute();
296 } catch (SQLException e) {
297 e.printStackTrace();
298 }
299 } else {
300 connect();
301 insertData(str);
302 }
303 }
304
305 /**
306 * Delete ALL DATA in DB
307 */
308 public void resetDB() { //clear the DB
309
310 }
311
312 /**
313 * Fill the HashMap with DB data
314 */
315 public void fillMap() {
316 if (hasConnected) {
317 resetLengthOfSQL(sql);
318
319 sql.append("SELECT * FROM ").append(tableName);
320 try {
321 statement = connection.createStatement();
322 result = statement.executeQuery(String.valueOf(sql));
323 while (result.next()) {
324// int id = result.getInt("id");
325// String first_name = result.getString("first_name");
326// String last_name = result.getString("last_name");
327 String username = result.getString("username");
328 String password = result.getString("password");
329
330 accounts.put(username, password);
331 }
332 } catch (SQLException e) {
333 e.printStackTrace();
334 }
335 } else {
336 connect();
337 fillMap();
338 }
339 }
340
341 /**
342 * Register account / add to DB user and have them to login after registering
343 */
344 public void registerUser(String... data) {
345
346 }
347}
348
349
350
351public class ToDoListScreen {
352
353}
354
355
356public class RegisterScreen {
357
358}
359
360import java.util.HashMap;
361import java.util.Map;
362
363class Scratch {
364 public static void main(String[] args) {
365 Map<String, String> accounts = new HashMap<>();
366
367 accounts.put("username", "password");
368 accounts.put("mrBeast", "youtube");
369 accounts.put("destroyerOfPu$$y123", "s3x");
370 accounts.put("martin_koleff", "parola20");
371 accounts.put("Boyko_Tikvata", "gerber");
372
373 String[] values = (String[]) accounts.values().stream()
374 .map(String::valueOf)
375 .toArray();
376 String[] AllAccounts = (String[]) accounts.keySet().stream().toArray();
377 }
378}
379
380import jdk.swing.interop.SwingInterOpUtils;
381
382import java.util.*;
383
384class Scratch {
385 public static void main(String[] args) {
386 Map<String, String> accounts = new HashMap<>();
387 Thread th = new Thread();
388
389 accounts.put("username", "password");
390 accounts.put("mrBeast", "youtube");
391 accounts.put("destroyerOfPu$$y123", "s3x");
392 accounts.put("martin_koleff", "parola20");
393 accounts.put("Boyko_Tikvata", "gerber");
394
395// Set passwords = accounts.keySet();
396// Collection<String> userNames = accounts.values();
397 //Collections.singleton(accounts.values());
398
399 int counter = 0;
400 for(int i = 0; i < accounts.size(); i++){
401 System.out.println(accounts.keySet().toArray()[i] + " " + accounts.values().toArray()[i]);
402 }
403
404// for(Object username : userNames){
405// System.out.println(username);
406// }
407 }
408}