Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. package quiz_windows;
  2.  
  3. import java.awt.Color;
  4. import java.awt.EventQueue;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JButton;
  8. import javax.swing.JLabel;
  9. import javax.swing.JTextField;
  10. import javax.swing.JPasswordField;
  11. import java.awt.Font;
  12. import java.awt.Window;
  13. import java.awt.event.ActionListener;
  14. import java.awt.event.ActionEvent;
  15.  
  16.  
  17. import java.sql.*;
  18. import javax.swing.*;
  19.  
  20. public class Login {
  21.  
  22. private JFrame LoginFrame;
  23. private JTextField textFieldUsN;
  24. private JPasswordField passwordField;
  25.  
  26. /**
  27. * Launch the application.
  28. */
  29. public static void main(String[] args) {
  30. EventQueue.invokeLater(new Runnable() {
  31. public void run() {
  32. try {
  33. Login window = new Login();
  34. window.LoginFrame.setVisible(true);
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. });
  40. }
  41.  
  42. Connection connection = null;
  43.  
  44. /**
  45. * Create the application.
  46. */
  47. public Login() {
  48. initialize();
  49. connection = sqliteConnection.dbConnector(); //Initialises connection
  50. }
  51.  
  52. /**
  53. * Initialize the contents of the frame.
  54. */
  55. private void initialize() {
  56. LoginFrame = new JFrame();
  57. LoginFrame.setBounds(100, 100, 691, 408);
  58. LoginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  59. LoginFrame.getContentPane().setLayout(null);
  60.  
  61. JButton LoginBtn = new JButton("Login ");
  62. LoginBtn.setActionCommand("Login");
  63. LoginBtn.addActionListener(new ActionListener() {
  64. public void actionPerformed(ActionEvent arg0) {
  65. try{
  66. String query = "select * from StudentDetailsTbl where Username=? and Password=? "; //Makes a query to the database
  67. PreparedStatement pst = connection.prepareStatement(query); //Creates and object and passed the query to the PreparedStatement
  68. pst.setString(1, textFieldUsN.getText() );
  69. pst.setString(2, passwordField.getText() );
  70.  
  71. ResultSet rs = pst.executeQuery();
  72. int count = 0;
  73. while(rs.next()){
  74. count = count+1;
  75.  
  76. }
  77. if (count == 1)
  78. {
  79. JOptionPane.showMessageDialog(null, "Username and Password is correct");
  80. LoginFrame.dispose();
  81. new Mainmenu();
  82. }
  83. else{
  84. JOptionPane.showMessageDialog(null, "Username and Password is incorrect, Try again");
  85. }
  86. rs.close();
  87. pst.close(); //Closes connection to database
  88. }catch(Exception e)
  89. {
  90. JOptionPane.showMessageDialog(null, e);
  91. }
  92. finally{
  93. try{
  94.  
  95. }catch(Exception e)
  96. {
  97. JOptionPane.showMessageDialog(null, e);
  98. }
  99. }
  100.  
  101. }
  102.  
  103. });
  104.  
  105. LoginBtn.setBounds(312, 293, 89, 23);
  106. LoginBtn.setBackground(new Color(59, 89, 182));
  107. LoginBtn.setForeground(Color.WHITE);
  108. LoginFrame.getContentPane().add(LoginBtn);
  109.  
  110. JLabel Usernamelbl = new JLabel("Username");
  111. Usernamelbl.setFont(new Font("Tahoma", Font.BOLD, 14));
  112. Usernamelbl.setBounds(237, 124, 89, 23);
  113. LoginFrame.getContentPane().add(Usernamelbl);
  114.  
  115. JLabel Passwordlbl = new JLabel("Password");
  116. Passwordlbl.setFont(new Font("Tahoma", Font.BOLD, 14));
  117. Passwordlbl.setBounds(237, 179, 78, 17);
  118. LoginFrame.getContentPane().add(Passwordlbl);
  119.  
  120. textFieldUsN = new JTextField();
  121. textFieldUsN.setBounds(347, 127, 97, 23);
  122. LoginFrame.getContentPane().add(textFieldUsN);
  123. textFieldUsN.setColumns(10);
  124.  
  125. passwordField = new JPasswordField();
  126. passwordField.setBounds(347, 179, 97, 23);
  127. LoginFrame.getContentPane().add(passwordField);
  128.  
  129. JButton btnRegister = new JButton("Register");
  130. btnRegister.setBackground(new Color(59, 89, 182));
  131. btnRegister.setForeground(Color.WHITE);
  132. btnRegister.addActionListener(new ActionListener() {
  133. public void actionPerformed(ActionEvent e) {
  134. LoginFrame.dispose();
  135. new Register();
  136. }
  137.  
  138. });
  139. btnRegister.setBounds(576, 11, 89, 23);
  140. LoginFrame.getContentPane().add(btnRegister);
  141.  
  142. LoginFrame.setVisible(true);
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement