Guest User

Untitled

a guest
Oct 14th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. package client.system;
  2.  
  3. import java.awt.HeadlessException;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import javax.swing.JOptionPane;
  10.  
  11.  
  12. public class LoginForm extends javax.swing.JPanel {
  13. Connection con = null;
  14. PreparedStatement pst = null;
  15. ResultSet rs = null;
  16.  
  17. /**
  18. * Creates new form LoginForm
  19. */
  20. public LoginForm() {
  21. initComponents();
  22. }
  23.  
  24. /**
  25. * This method is called from within the constructor to initialize the form.
  26. * WARNING: Do NOT modify this code. The content of this method is always
  27. * regenerated by the Form Editor.
  28. */
  29. @SuppressWarnings("unchecked")
  30. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  31. private void initComponents() {
  32.  
  33. jPanel1 = new javax.swing.JPanel();
  34. jLabel1 = new javax.swing.JLabel();
  35. jLabel2 = new javax.swing.JLabel();
  36. username = new javax.swing.JTextField();
  37. jLabel3 = new javax.swing.JLabel();
  38. password = new javax.swing.JPasswordField();
  39. login = new javax.swing.JButton();
  40.  
  41. jLabel1.setFont(new java.awt.Font("Tahoma", 1, 20)); // NOI18N
  42. jLabel1.setText("Log In");
  43.  
  44. jLabel2.setLabelFor(username);
  45. jLabel2.setText("Username:");
  46.  
  47. username.setText("Username");
  48.  
  49. jLabel3.setLabelFor(password);
  50. jLabel3.setText("Password:");
  51.  
  52. password.setText("Password");
  53.  
  54. login.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
  55. login.setText("Login");
  56. login.addActionListener(new java.awt.event.ActionListener() {
  57. public void actionPerformed(java.awt.event.ActionEvent evt) {
  58. loginActionPerformed(evt);
  59. }
  60. });
  61.  
  62. javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  63. jPanel1.setLayout(jPanel1Layout);
  64. jPanel1Layout.setHorizontalGroup(
  65. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  66. .addGroup(jPanel1Layout.createSequentialGroup()
  67. .addGap(50, 50, 50)
  68. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  69. .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  70. .addGroup(jPanel1Layout.createSequentialGroup()
  71. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  72. .addComponent(password, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
  73. .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
  74. .addComponent(username))
  75. .addContainerGap())
  76. .addGroup(jPanel1Layout.createSequentialGroup()
  77. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  78. .addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
  79. .addComponent(jLabel3))
  80. .addGap(0, 43, Short.MAX_VALUE))))
  81. );
  82. jPanel1Layout.setVerticalGroup(
  83. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  84. .addGroup(jPanel1Layout.createSequentialGroup()
  85. .addContainerGap()
  86. .addComponent(jLabel1)
  87. .addGap(18, 18, 18)
  88. .addComponent(jLabel2)
  89. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  90. .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
  91. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  92. .addComponent(jLabel3)
  93. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  94. .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
  95. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  96. .addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  97. .addContainerGap(70, Short.MAX_VALUE))
  98. );
  99.  
  100. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
  101. this.setLayout(layout);
  102. layout.setHorizontalGroup(
  103. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  104. .addGroup(layout.createSequentialGroup()
  105. .addContainerGap()
  106. .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  107. .addContainerGap())
  108. );
  109. layout.setVerticalGroup(
  110. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  111. .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  112. );
  113. }// </editor-fold>
  114.  
  115. private void loginActionPerformed(java.awt.event.ActionEvent evt) {
  116. // TODO add your handling code here:
  117. String sql="select * from login where username=? and password=?";
  118. try{
  119. con = DriverManager.getConnection("jdbc:mysql://localhost/testsql","root","mysql");
  120. pst = con.prepareStatement(sql);
  121. pst.setString(1, username.getText());
  122. pst.setString(2,password.getText());
  123. rs = pst.executeQuery();
  124. if(rs.next()){
  125. JOptionPane.showMessageDialog(null,"username and password matched");
  126. }else{
  127. JOptionPane.showMessageDialog(null, "Incorrect Username or Password");
  128. }
  129. }catch(SQLException | HeadlessException ex){
  130. JOptionPane.showMessageDialog(null,ex);
  131. }
  132. }
  133. public static void main(String args[]){
  134. java.awt.EventQueue.invokeLater(new Runnable(){
  135. public void run(){
  136. new LoginForm().setVisible(true);
  137. }
  138. });
  139. }
  140.  
  141. // Variables declaration - do not modify
  142. private javax.swing.JLabel jLabel1;
  143. private javax.swing.JLabel jLabel2;
  144. private javax.swing.JLabel jLabel3;
  145. private javax.swing.JPanel jPanel1;
  146. private javax.swing.JButton login;
  147. private javax.swing.JPasswordField password;
  148. private javax.swing.JTextField username;
  149. // End of variables declaration
  150. }
  151.  
  152. public class ExampleFrame extends JFrame {
  153.  
  154. public ExampleFrame() {
  155. MyPanel panel = new MyPanel();
  156. getContentPane().add(panel);
  157.  
  158. setTitle("My Cool Custom Panel");
  159. setSize(300, 200);
  160. setLocationRelativeTo(null);
  161. setDefaultCloseOperation(EXIT_ON_CLOSE);
  162. }
  163.  
  164. public static void main(String[] args) {
  165. new ExampleFrame().setVisible(true);
  166. }
  167. }
Add Comment
Please, Sign In to add comment