· 6 years ago · Mar 20, 2019, 01:22 PM
1import java.sql.*;
2import java.util.*;
3public class client {
4
5public static void main(String args[])throws SQLException {
6
7 Scanner keyboard = new Scanner(System.in);
8
9 try {
10 Class.forName("org.postgresql.Driver");
11 System.out.println("Connected");
12 }
13 catch(ClassNotFoundException e) {
14 System.out.println("error class not found");
15 }
16 //Connecting to the database
17 Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres","postgres","????????????");
18 PreparedStatement st;
19 Statement stmt = con.createStatement();
20 ResultSet rs;
21 String user = "";
22 String password = "";
23 String cuser = "";
24 int num1 =0;
25 System.out.println("Press 1 to create account and 2 to check if account already exists: ");
26 num1 = keyboard.nextInt();
27 keyboard.nextLine();
28 switch(num1){
29 case 1:
30 System.out.print("Please Enter in a username: ");
31 user = keyboard.nextLine();
32 System.out.print("Please Enter in a password: ");
33 password = keyboard.nextLine();
34 user u1 = new user(user,password);
35 System.out.println(u1);
36 st = con.prepareStatement("INSERT INTO user1 VALUES(?,?)");
37 st.setString(1,u1.getUser());
38 st.setString(2,u1.getPassword());
39 st.executeUpdate();
40 case 2:
41 System.out.print("Please enter in Username: ");
42 cuser = keyboard.nextLine();
43 System.out.print("Please Enter in a Password: ");
44 String cpassword = keyboard.nextLine();
45 user u2 = new user(cuser,cpassword);
46 rs = stmt.executeQuery("SELECT username FROM user1 where username = username");
47 System.out.println("Selected from the table");
48 while(rs.next()) {
49 System.out.println("Entered into the loop");
50 String firstname = rs.getString("username");
51 //This is statement right here
52 if(u2.getUser() == firstname) {
53 System.out.println("The username "+ u2.getUser() +" Exists");
54 }
55 }
56 con.close();
57
58 }
59}