Advertisement
Guest User

Untitled

a guest
Aug 25th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. package Login_Sys;
  2.  
  3. import java.awt.Component;
  4. import java.awt.EventQueue;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JLabel;
  8. import javax.swing.JOptionPane;
  9. import javax.swing.JTextField;
  10.  
  11. import RanSanMoi.FrameScreen;
  12.  
  13. import javax.swing.JPasswordField;
  14. import javax.swing.JButton;
  15. import java.awt.event.ActionListener;
  16. import java.sql.Connection;
  17. import java.sql.DriverManager;
  18. import java.sql.PreparedStatement;
  19. import java.awt.event.ActionEvent;
  20. import javax.swing.JSeparator;
  21. import java.awt.Font;
  22. import java.awt.Color;
  23. import java.awt.Window.Type;
  24. import java.sql.ResultSet;
  25. import java.sql.SQLException;
  26.  
  27. import Register_Sys.Register_S;
  28. import java.awt.SystemColor;
  29.  
  30. public class Login_S {
  31.  
  32. protected static Component frmLoginSystem = null;
  33. private JFrame frmLogin;
  34. private JTextField textField;
  35. private JPasswordField passwordField;
  36. Connection connection=null;
  37.  
  38. /**
  39. * Launch the application.
  40. */
  41. public static void main(String[] args) {
  42. EventQueue.invokeLater(new Runnable() {
  43. public void run() {
  44. try {
  45. Login_S window = new Login_S();
  46. window.frmLogin.setVisible(true);
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. });
  52. }
  53.  
  54. /**
  55. * Create the application.
  56. */
  57. public Login_S() {
  58. initialize();
  59. }
  60.  
  61. /**
  62. * Initialize the contents of the frame.
  63. */
  64. private void initialize() {
  65. try {
  66. Class.forName("org.h2.Driver");
  67. connection= DriverManager.getConnection("jdbc:h2:tcp://localhost/~/test","sa","");
  68. }
  69. catch (ClassNotFoundException | SQLException e1) {
  70. // TODO Auto-generated catch block
  71. e1.printStackTrace();
  72. }
  73. frmLogin = new JFrame();
  74. frmLogin.setType(Type.POPUP);
  75. frmLogin.setTitle("Login");
  76. frmLogin.setBackground(Color.BLUE);
  77. frmLogin.getContentPane().setBackground(new Color(106, 90, 205));
  78. frmLogin.setForeground(Color.BLACK);
  79. frmLogin.getContentPane().setForeground(Color.DARK_GRAY);
  80. frmLogin.getContentPane().setFont(new Font("Tahoma", Font.BOLD, 15));
  81. frmLogin.setBounds(200, 200, 500, 300);
  82. frmLogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  83. frmLogin.getContentPane().setLayout(null);
  84.  
  85. JLabel lblNewLabel = new JLabel("Login Systems");
  86. lblNewLabel.setBounds(167, 11, 186, 26);
  87. lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 25));
  88. frmLogin.getContentPane().add(lblNewLabel);
  89.  
  90. JLabel lblUsername = new JLabel("Username");
  91. lblUsername.setBounds(66, 70, 90, 30);
  92. lblUsername.setFont(new Font("Tahoma", Font.BOLD, 15));
  93. frmLogin.getContentPane().add(lblUsername);
  94.  
  95. JLabel lblPassword = new JLabel("Password");
  96. lblPassword.setBounds(66, 135, 75, 30);
  97. lblPassword.setFont(new Font("Tahoma", Font.BOLD, 15));
  98. frmLogin.getContentPane().add(lblPassword);
  99.  
  100. textField = new JTextField();
  101. textField.setBounds(151, 71, 161, 30);
  102. textField.setFont(new Font("Tahoma", Font.BOLD, 15));
  103. frmLogin.getContentPane().add(textField);
  104. textField.setColumns(10);
  105.  
  106. passwordField = new JPasswordField();
  107. passwordField.setBounds(151, 136, 161, 30);
  108. passwordField.setFont(new Font("Tahoma", Font.BOLD, 15));
  109. frmLogin.getContentPane().add(passwordField);
  110.  
  111. JButton btnLogin = new JButton("Login");
  112. btnLogin.setForeground(Color.BLUE);
  113. btnLogin.setBackground(Color.LIGHT_GRAY);
  114. btnLogin.setBounds(10, 210, 89, 23);
  115. btnLogin.setFont(new Font("Tahoma", Font.BOLD, 15));
  116. btnLogin.addActionListener(new ActionListener() {
  117. @SuppressWarnings("deprecation")
  118. public void actionPerformed(ActionEvent arg0) {
  119.  
  120.  
  121. try{
  122. String validate= "SELECT * FROM USERTABLE WHERE USER=? AND PASS=? ";
  123. PreparedStatement statement = connection.prepareStatement(validate);
  124. statement.setString(1, textField.getText());
  125. statement.setString(2, passwordField.getText());
  126. ResultSet set = statement.executeQuery();
  127. if(set.next()){
  128.  
  129. frmLogin.setVisible(false);
  130. FrameScreen info= new FrameScreen();
  131. FrameScreen.main(null);
  132. }
  133. else{
  134. JOptionPane.showMessageDialog(null, "Invalid Login Details","Login System Error",JOptionPane.ERROR_MESSAGE);
  135. passwordField.setText(null);
  136. textField.setText(null);
  137. }
  138. }
  139. catch (SQLException e) {
  140. e.printStackTrace();
  141. }
  142.  
  143. }
  144. });
  145. frmLogin.getContentPane().add(btnLogin);
  146.  
  147. JButton btnReset = new JButton("Reset");
  148. btnReset.setForeground(Color.BLUE);
  149. btnReset.setBackground(Color.LIGHT_GRAY);
  150. btnReset.setBounds(135, 210, 89, 23);
  151. btnReset.setFont(new Font("Tahoma", Font.BOLD, 15));
  152. btnReset.addActionListener(new ActionListener() {
  153. public void actionPerformed(ActionEvent e) {
  154.  
  155. passwordField.setText(null);
  156. textField.setText(null);
  157. }
  158. });
  159. frmLogin.getContentPane().add(btnReset);
  160.  
  161. JButton btnExit = new JButton("Exit");
  162. btnExit.setForeground(Color.BLUE);
  163. btnExit.setBackground(Color.LIGHT_GRAY);
  164. btnExit.setBounds(385, 210, 89, 23);
  165. btnExit.setFont(new Font("Tahoma", Font.BOLD, 15));
  166. btnExit.addActionListener(new ActionListener() {
  167. public void actionPerformed(ActionEvent e) {
  168. frmLoginSystem= new JFrame("Exit");
  169. if(JOptionPane.showConfirmDialog(frmLoginSystem,"Confirm if you want to exit","Login Systems",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_NO_OPTION){
  170. System.exit(1);
  171. }
  172. }
  173. });
  174. frmLogin.getContentPane().add(btnExit);
  175.  
  176. JSeparator separator = new JSeparator();
  177. separator.setBounds(10, 188, 464, 2);
  178. frmLogin.getContentPane().add(separator);
  179.  
  180. JSeparator separator_1 = new JSeparator();
  181. separator_1.setBounds(10, 35, 464, 2);
  182. frmLogin.getContentPane().add(separator_1);
  183.  
  184. JButton btnRegister = new JButton("Register");
  185. btnRegister.setBackground(Color.LIGHT_GRAY);
  186. btnRegister.setForeground(Color.BLUE);
  187. btnRegister.setFont(new Font("Tahoma", Font.BOLD, 15));
  188. btnRegister.addActionListener(new ActionListener() {
  189. public void actionPerformed(ActionEvent e) {
  190. Register_S info = new Register_S();
  191. Register_S.main(null);
  192. frmLogin.setVisible(false);
  193. }
  194. });
  195. btnRegister.setBounds(256, 210, 97, 23);
  196. frmLogin.getContentPane().add(btnRegister);
  197. }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement