Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. #
  2. #
  3. import java.awt.*;
  4. #
  5. import java.awt.event.*;
  6. #
  7. import javax.swing.*;
  8. #
  9. import java.sql.*;
  10. #
  11. import javax.swing.JOptionPane;
  12. #
  13.  
  14. #
  15. public class Login extends JFrame implements ActionListener {
  16. #
  17. private Container container;
  18. #
  19. private GridBagLayout layout;
  20. #
  21. private GridBagConstraints gbc;
  22. #
  23.  
  24. #
  25. private JButton cmdLogin, cmdCancel;
  26. #
  27. private JLabel lblUSer, lblPassword;
  28. #
  29. private JTextField txtUser;
  30. #
  31. private JPasswordField txtPassword;
  32. #
  33.  
  34. #
  35. public Login()
  36. #
  37. {
  38. #
  39.  
  40. #
  41. setTitle("Login Screen"); // or //super("Login window");
  42. #
  43. setExtendedState(JFrame.MAXIMIZED_BOTH);
  44. #
  45. setResizable(false); //disable resizing and Max button
  46. #
  47.  
  48. #
  49. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  50. #
  51. setSize(300,150);
  52. #
  53. setLocationRelativeTo(null);
  54. #
  55.  
  56. #
  57. container = getContentPane();
  58. #
  59. layout = new GridBagLayout();
  60. #
  61. container.setLayout(layout);
  62. #
  63.  
  64. #
  65. gbc = new GridBagConstraints();
  66. #
  67.  
  68. #
  69. lblUSer = new JLabel("Username:");
  70. #
  71. gbc.insets = new Insets(2,2,2,2);
  72. #
  73. container.add(lblUSer, gbc);
  74. #
  75.  
  76. #
  77. txtUser = new JTextField(15);
  78. #
  79. gbc.gridx = 1;
  80. #
  81. gbc.gridwidth = 3;
  82. #
  83. container.add(txtUser, gbc);
  84. #
  85.  
  86. #
  87. lblPassword = new JLabel("Password:");
  88. #
  89. gbc.gridy = 1;
  90. #
  91. gbc.gridx = 0;
  92. #
  93. gbc.gridwidth = 1;
  94. #
  95. container.add(lblPassword, gbc);
  96. #
  97.  
  98. #
  99. txtPassword = new JPasswordField(15);
  100. #
  101. gbc.gridx = 1;
  102. #
  103. gbc.gridwidth = 3;
  104. #
  105. container.add(txtPassword, gbc);
  106. #
  107.  
  108. #
  109. cmdLogin = new JButton("Login");
  110. #
  111. cmdLogin.addActionListener( this );
  112. #
  113. gbc.gridy = 2;
  114. #
  115. gbc.gridx = 1;
  116. #
  117. gbc.gridwidth = 1;
  118. #
  119. container.add(cmdLogin, gbc);
  120. #
  121.  
  122. #
  123. cmdCancel = new JButton("Cancel");
  124. #
  125. cmdCancel.addActionListener( this );
  126. #
  127. gbc.gridx = 2;
  128. #
  129. container.add(cmdCancel, gbc);
  130. #
  131. } //Login()
  132. #
  133.  
  134. #
  135. public void actionPerformed(ActionEvent e){
  136. #
  137. if(e.getActionCommand().equals("Login")){
  138. #
  139. JOptionPane.showMessageDialog(null, "Wrong Username or Password, try again", "Warning !!!", JOptionPane.WARNING_MESSAGE);
  140. #
  141. }
  142. #
  143. else
  144. #
  145. {
  146. #
  147. //default icon, custom title
  148. #
  149. int respond = JOptionPane.showConfirmDialog(null, "Would you like exiting the program ?", "Exiting", JOptionPane.YES_NO_OPTION);
  150. #
  151. //System.out.println(respond);
  152. #
  153.  
  154. #
  155. if(respond == 0){
  156. #
  157. dispose (); //closing the frame
  158. #
  159. }
  160. #
  161. }
  162. #
  163. } //actionPerformed()
  164. #
  165.  
  166. #
  167. public static void connect()
  168. #
  169. {
  170. #
  171. Connection conn = null;
  172. #
  173.  
  174. #
  175. try
  176. #
  177. {
  178. #
  179. String userName = "root";
  180. #
  181. String password = "root";
  182. #
  183. String url = "jdbc:mysql://localhost/Member";
  184. #
  185. Class.forName ("com.mysql.jdbc.Driver").newInstance ();
  186. #
  187. conn = DriverManager.getConnection (url, userName, password);
  188. #
  189. System.out.println ("Database connection established");
  190. #
  191. }
  192. #
  193. catch (Exception e)
  194. #
  195. {
  196. #
  197. System.err.println ("Cannot connect to database server");
  198. #
  199. }
  200. #
  201.  
  202. #
  203. finally
  204. #
  205. {
  206. #
  207. if (conn != null)
  208. #
  209. {
  210. #
  211. try{
  212. #
  213. conn.close ();
  214. #
  215. System.out.println ("Database connection terminated");
  216. #
  217. }
  218. #
  219. catch (Exception e)
  220. #
  221. {
  222. #
  223. /* ignore close errors */
  224. #
  225. }
  226. #
  227. }
  228. #
  229. }
  230. #
  231. } //connect()
  232. #
  233.  
  234. #
  235. public static void main(String args[]) {
  236. #
  237. new Login().setVisible(true);
  238. #
  239. connect();
  240. #
  241. } //main()
  242. #
  243.  
  244. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement