Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.*;
- public class client {
- public static void main(String args[])throws SQLException {
- Scanner keyboard = new Scanner(System.in);
- try {
- Class.forName("org.postgresql.Driver");
- System.out.println("Connected");
- }
- catch(ClassNotFoundException e) {
- System.out.println("error class not found");
- }
- //Connecting to the database
- Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres","postgres","????????????");
- PreparedStatement st;
- Statement stmt = con.createStatement();
- ResultSet rs;
- String user = "";
- String password = "";
- String cuser = "";
- int num1 =0;
- System.out.println("Press 1 to create account and 2 to check if account already exists: ");
- num1 = keyboard.nextInt();
- keyboard.nextLine();
- switch(num1){
- case 1:
- System.out.print("Please Enter in a username: ");
- user = keyboard.nextLine();
- System.out.print("Please Enter in a password: ");
- password = keyboard.nextLine();
- user u1 = new user(user,password);
- System.out.println(u1);
- st = con.prepareStatement("INSERT INTO user1 VALUES(?,?)");
- st.setString(1,u1.getUser());
- st.setString(2,u1.getPassword());
- st.executeUpdate();
- case 2:
- System.out.print("Please enter in Username: ");
- cuser = keyboard.nextLine();
- System.out.print("Please Enter in a Password: ");
- String cpassword = keyboard.nextLine();
- user u2 = new user(cuser,cpassword);
- rs = stmt.executeQuery("SELECT username FROM user1 where username = username");
- System.out.println("Selected from the table");
- while(rs.next()) {
- System.out.println("Entered into the loop");
- String firstname = rs.getString("username");
- //This is statement right here
- if(u2.getUser() == firstname) {
- System.out.println("The username "+ u2.getUser() +" Exists");
- }
- }
- con.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement