Advertisement
Guest User

Untitled

a guest
Feb 11th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.94 KB | None | 0 0
  1. package Login_Registration_Project;
  2.  
  3.  
  4. import java.sql.*;
  5. import java.util.*;
  6.  
  7. /**
  8.  * Created by Salek on 29.01.2017.
  9.  */
  10. public class Login {
  11.     public static void main (String[] args) {
  12.  
  13.  
  14.  
  15.         String url = "jdbc:mysql://localhost:3306/javabase";
  16.         String user = "java";
  17.         String passwd = "password";
  18.  
  19.         try {
  20.             //  1. Get a connection to the DB
  21.             Connection myConn = DriverManager.getConnection(url, user, passwd);
  22.             //  2. Create a statement
  23.             Statement myStmt = myConn.createStatement();
  24.             //  3. Execute SQL query
  25.  
  26.             Scanner input = new Scanner (System.in);
  27.  
  28.             System.out.println("Please Login to the Clinic.");
  29.             System.out.println("Login - Pesel: ");
  30.             String Login = input.nextLine();
  31.             System.out.println("Password: ");
  32.             String Password = input.nextLine();
  33.  
  34.             String myQuery = "select * from shadow" +
  35.                     " where Password = '" + Password + "' and Pesel = '" + Login + "'";
  36.  
  37.  
  38. //            System.out.println(myQuery);
  39.             ResultSet myRs = myStmt.executeQuery(myQuery);
  40.  
  41.             myRs.last(); // moves cursor to the last row, remember to do myRs.first() if you want to get it back to the first row,
  42.                         // it doesn't matter now because we are getting only 1 row so first row is a last row anyway.
  43.  
  44.             int rowCount = myRs.getRow();
  45. //            System.out.println(rowCount);
  46.  
  47.             while (myRs.next()){
  48.                  System.out.println(myRs.getString("Password") + " " + myRs.getString("Pesel"));
  49.  
  50.             }
  51.  
  52.             if (rowCount == 0) {
  53.                 System.out.println("Login or/and password are not correct! Please try again.");
  54.             }
  55.  
  56.             else System.out.println("Login succesfull!");
  57.         }
  58.  
  59.  
  60.         catch (Exception exc) {
  61.         exc.printStackTrace();
  62.         }
  63.  
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement