Advertisement
Guest User

Untitled

a guest
May 30th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Scanner;
  3.  
  4. public class Login {
  5.  
  6. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  7. static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/Account"; // Account -> Connection Name
  8.  
  9. static final String USERNAME = "root"; // User name
  10. static final String PASSWORD = "password"; // Password
  11.  
  12. public static void main(String[] args) {
  13. Connection conn = null;
  14. Statement stmt = null;
  15.  
  16. Scanner s = new Scanner(System.in);
  17.  
  18. System.out.print("ID : ");
  19. String id = s.next();
  20. System.out.print("PW : ");
  21. String pw = s.next();
  22.  
  23. try{
  24. Class.forName(JDBC_DRIVER);
  25. conn = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);
  26. System.out.println("\n- MySQL Connection");
  27. stmt = conn.createStatement();
  28.  
  29. String sql;
  30. sql = "SELECT number,id, pw FROM account";
  31. ResultSet rs = stmt.executeQuery(sql);
  32.  
  33. while(rs.next()){
  34. int num = rs.getInt("number");
  35. String ID = rs.getString("ID");
  36. String PW = rs.getString("PW");
  37.  
  38. if(ID.equals(id) || PW.equals(pw)) {
  39. System.out.println("Login Success!!");
  40. System.out.println("Num : " + num + " ID : " + ID + " PW : " + PW);
  41. break;
  42. }
  43.  
  44. System.out.println("Login Failed!!");
  45.  
  46. }
  47. rs.close();
  48. stmt.close();
  49. conn.close();
  50. }catch(SQLException se1){
  51. se1.printStackTrace();
  52. }catch(Exception ex){
  53. ex.printStackTrace();
  54. }finally{
  55. try{
  56. if(stmt!=null)
  57. stmt.close();
  58. }catch(SQLException se2){
  59. }
  60. try{
  61. if(conn!=null)
  62. conn.close();
  63. }catch(SQLException se){
  64. se.printStackTrace();
  65. }
  66. }
  67. System.out.println("\n\n- MySQL Connection Close");
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement