Advertisement
Guest User

Untitled

a guest
Sep 5th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. package com.mvc.view;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Container;
  5. import java.awt.Font;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8.  
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JPasswordField;
  13. import javax.swing.JTextField;
  14. import javax.swing.SwingConstants;
  15.  
  16. import com.mvc.controller.LoginController;
  17.  
  18. public class LoginView extends JFrame {
  19.  
  20. private static final long serialVersionUID = 3566038652320101414L;
  21. private JTextField txtUsername;
  22. private JPasswordField txtPassword;
  23. private JButton btnLogin;
  24. private JLabel lblErrorMessage;
  25.  
  26. public LoginView() {
  27. setTitle("MedIT: Login");
  28. setResizable(false);
  29. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30. setBounds(100, 100, 400, 250);
  31. Container contentPane = this.getContentPane();
  32. contentPane.setLayout(null);
  33.  
  34. JLabel lblUsername = new JLabel("Username:");
  35. lblUsername.setFont(new Font("Tahoma", Font.BOLD, 14));
  36. lblUsername.setHorizontalAlignment(SwingConstants.RIGHT);
  37. lblUsername.setBounds(10, 11, 120, 25);
  38. contentPane.add(lblUsername);
  39.  
  40. txtUsername = new JTextField();
  41. txtUsername.setFont(new Font("Tahoma", Font.PLAIN, 14));
  42. txtUsername.setBounds(140, 11, 200, 25);
  43. contentPane.add(txtUsername);
  44.  
  45. JLabel lblPassword = new JLabel("Password:");
  46. lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
  47. lblPassword.setFont(new Font("Tahoma", Font.BOLD, 14));
  48. lblPassword.setBounds(10, 52, 120, 25);
  49. contentPane.add(lblPassword);
  50.  
  51. txtPassword = new JPasswordField();
  52. txtPassword.setFont(new Font("Tahoma", Font.PLAIN, 14));
  53. txtPassword.setBounds(140, 52, 200, 25);
  54. contentPane.add(txtPassword);
  55.  
  56. LoginController controller = new LoginController(this);
  57. btnLogin = new JButton("Login");
  58. btnLogin.addActionListener(new ActionListener(){
  59. @Override
  60. public void actionPerformed(ActionEvent arg0) {
  61. controller.checkCredentials(txtUsername.getText(), new String(txtPassword.getPassword()));
  62. }
  63. });
  64. btnLogin.setFont(new Font("Tahoma", Font.PLAIN, 14));
  65. btnLogin.setBounds(251, 93, 89, 25);
  66. contentPane.add(btnLogin);
  67.  
  68. lblErrorMessage = new JLabel("");
  69. lblErrorMessage.setHorizontalAlignment(SwingConstants.RIGHT);
  70. lblErrorMessage.setForeground(Color.RED);
  71. lblErrorMessage.setFont(new Font("Tahoma", Font.PLAIN, 14));
  72. lblErrorMessage.setBounds(10, 151, 330, 25);
  73. contentPane.add(lblErrorMessage);
  74. }
  75.  
  76. public void setErrorMessage(String errorMessage) {
  77. lblErrorMessage.setText(errorMessage);
  78. }
  79.  
  80. public static void main(String args[]){
  81. LoginView view = new LoginView();
  82. view.setVisible(true);
  83. }
  84. }
  85.  
  86. package com.mvc.controller;
  87.  
  88. import com.mvc.model.LoginModel;
  89. import com.mvc.view.LoginView;
  90.  
  91. public class LoginController {
  92.  
  93. private LoginView view;
  94. private LoginModel model;
  95.  
  96. public LoginController(LoginView view){
  97. this.view = view;
  98. model = new LoginModel();
  99. }
  100.  
  101. public void checkCredentials(String username, String password){
  102. model.setUsername(username);
  103. model.getCredentials();
  104. if(password.equals(model.getPassword())){
  105. view.setErrorMessage("Login Success!");
  106. }
  107. else{
  108. view.setErrorMessage("Login Failed!");
  109. }
  110. }
  111. }
  112.  
  113. package com.mvc.model;
  114.  
  115. import java.sql.Connection;
  116. import java.sql.DriverManager;
  117. import java.sql.PreparedStatement;
  118. import java.sql.ResultSet;
  119. import java.sql.SQLException;
  120.  
  121. public class LoginModel {
  122. private String username, password;
  123. private Connection conn;
  124.  
  125. public void setUsername(String username) {
  126. this.username = username;
  127. }
  128.  
  129. public String getPassword() {
  130. return password;
  131. }
  132.  
  133. public void setPassword(String password) {
  134. this.password = password;
  135. }
  136.  
  137. public void getCredentials(){
  138. try {
  139. Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
  140. conn = DriverManager.getConnection("jdbc:derby:db/medit");
  141. PreparedStatement select = conn.prepareStatement("SELECT * FROM user_credentials WHERE username = ?");
  142. select.setString(1, username);
  143. ResultSet rs = select.executeQuery();
  144. if(rs.next()){
  145. password = rs.getString("password");
  146. }
  147. else{
  148. password = "";
  149. }
  150. } catch (ClassNotFoundException e) {
  151. e.printStackTrace();
  152. }catch (SQLException e) {
  153. e.printStackTrace();
  154. }
  155. finally{
  156. try {
  157. conn.close();
  158. } catch (SQLException e) {
  159. e.printStackTrace();
  160. }
  161. }
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement