Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.15 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package study_java_swing;
  7.  
  8. import java.sql.*;
  9. import javax.swing.JOptionPane;
  10.  
  11. /**
  12.  *
  13.  * @author FPT SHOP
  14.  */
  15. public class LoginForm extends javax.swing.JFrame {
  16.     private Connection conn = null;
  17.     private Statement stmt = null;
  18.     private ResultSet res = null;
  19.    
  20.     private final String ip_DB = "jdbc:mysql://localhost:3306/";
  21.     private final String user_DB = "root";
  22.     private final String pass_DB = "";
  23.     /**
  24.      * Creates new form LoginForm
  25.      */
  26.     public LoginForm() {
  27.         initComponents();
  28.         setLocationRelativeTo(null);
  29.     }
  30.    
  31.     private void getDB(String sql){
  32.         try{
  33.             res = stmt.executeQuery(sql);
  34.             System.out.println("Lấy Database thành công!!");
  35.         }
  36.         catch(Exception e){
  37.             System.out.println("Lấy Database thất bại!!");
  38.         }
  39.     }
  40.    
  41.     private boolean UpdateDB(String sql){
  42.         try{
  43.             stmt.execute(sql);
  44.             System.out.println("Update Database thành công!!");
  45.             return true;
  46.         }
  47.         catch(Exception e){
  48.             System.out.println("Update Database thất bại!!");
  49.             return false;
  50.         }
  51.     }
  52.    
  53.     private void CheckDrive(){
  54.         try{
  55.             Class.forName("com.mysql.jdbc.Driver");
  56.         }
  57.         catch(Exception e){
  58.             System.out.println("Không tìm thấy Driver mySql!!");
  59.         }
  60.     }
  61.    
  62.     private void OpenDB(){
  63.         try{
  64.             String url = ip_DB + "logindatabase?useUnicode=true&characterEncoding=UTF-8";
  65.             conn = DriverManager.getConnection(url, user_DB, pass_DB);
  66.             stmt = conn.createStatement();
  67.             System.out.println("Kết nối thành công Database!!!");
  68.         }
  69.         catch(Exception e){
  70.             System.out.println("Error: " + e.toString() + "** Không thể kết nối Database!!!");
  71.         }
  72.     }
  73.    
  74.     private void CloseDB(){
  75.         try{
  76.             if(conn != null){
  77.                 conn.close();
  78.                 conn = null;
  79.             }
  80.             if(stmt != null){
  81.                 stmt.close();
  82.                 stmt = null;
  83.             }
  84.             System.out.println("Đã đóng Database!!");
  85.         }
  86.         catch(Exception e){
  87.             System.out.println("Không thể đóng Database!!");
  88.         }
  89.     }
  90.  
  91.     /**
  92.      * This method is called from within the constructor to initialize the form.
  93.      * WARNING: Do NOT modify this code. The content of this method is always
  94.      * regenerated by the Form Editor.
  95.      */
  96.     @SuppressWarnings("unchecked")
  97.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  98.     private void initComponents() {
  99.  
  100.         jPanel1 = new javax.swing.JPanel();
  101.         txtusername = new javax.swing.JTextField();
  102.         txtpassword = new javax.swing.JPasswordField();
  103.         jLabel1 = new javax.swing.JLabel();
  104.         jLabel2 = new javax.swing.JLabel();
  105.         jButton1 = new javax.swing.JButton();
  106.         jButton2 = new javax.swing.JButton();
  107.         jScrollPane1 = new javax.swing.JScrollPane();
  108.         jTable1 = new javax.swing.JTable();
  109.  
  110.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  111.  
  112.         jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 255), 20));
  113.  
  114.         txtusername.addActionListener(new java.awt.event.ActionListener() {
  115.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  116.                 txtusernameActionPerformed(evt);
  117.             }
  118.         });
  119.  
  120.         txtpassword.addActionListener(new java.awt.event.ActionListener() {
  121.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  122.                 txtpasswordActionPerformed(evt);
  123.             }
  124.         });
  125.  
  126.         jLabel1.setIcon(new javax.swing.ImageIcon("E:\\chua_web\\DoAnWeb1\\hinh\\login.png")); // NOI18N
  127.  
  128.         jLabel2.setIcon(new javax.swing.ImageIcon("E:\\chua_web\\Do_an\\icon\\icons8-spam-50.png")); // NOI18N
  129.  
  130.         jButton1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  131.         jButton1.setText("SIGNIN");
  132.  
  133.         jButton2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  134.         jButton2.setText("RESET");
  135.  
  136.         jTable1.setModel(new javax.swing.table.DefaultTableModel(
  137.             new Object [][] {
  138.  
  139.             },
  140.             new String [] {
  141.  
  142.             }
  143.         ));
  144.         jScrollPane1.setViewportView(jTable1);
  145.  
  146.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  147.         jPanel1.setLayout(jPanel1Layout);
  148.         jPanel1Layout.setHorizontalGroup(
  149.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  150.             .addGroup(jPanel1Layout.createSequentialGroup()
  151.                 .addContainerGap(178, Short.MAX_VALUE)
  152.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  153.                     .addGroup(jPanel1Layout.createSequentialGroup()
  154.                         .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  155.                         .addGap(98, 98, 98)
  156.                         .addComponent(txtusername))
  157.                     .addGroup(jPanel1Layout.createSequentialGroup()
  158.                         .addComponent(jLabel2)
  159.                         .addGap(98, 98, 98)
  160.                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  161.                             .addGroup(jPanel1Layout.createSequentialGroup()
  162.                                 .addComponent(jButton1)
  163.                                 .addGap(68, 68, 68)
  164.                                 .addComponent(jButton2))
  165.                             .addComponent(txtpassword, javax.swing.GroupLayout.PREFERRED_SIZE, 336, javax.swing.GroupLayout.PREFERRED_SIZE)))
  166.                     .addComponent(jScrollPane1))
  167.                 .addContainerGap(178, Short.MAX_VALUE))
  168.         );
  169.         jPanel1Layout.setVerticalGroup(
  170.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  171.             .addGroup(jPanel1Layout.createSequentialGroup()
  172.                 .addGap(51, 51, 51)
  173.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  174.                     .addComponent(jLabel1)
  175.                     .addComponent(txtusername, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
  176.                 .addGap(49, 49, 49)
  177.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  178.                     .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
  179.                     .addComponent(txtpassword, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
  180.                 .addGap(35, 35, 35)
  181.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  182.                     .addComponent(jButton1)
  183.                     .addComponent(jButton2))
  184.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
  185.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
  186.                 .addGap(48, 48, 48))
  187.         );
  188.  
  189.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  190.         getContentPane().setLayout(layout);
  191.         layout.setHorizontalGroup(
  192.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  193.             .addGroup(layout.createSequentialGroup()
  194.                 .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  195.                 .addContainerGap())
  196.         );
  197.         layout.setVerticalGroup(
  198.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  199.             .addGroup(layout.createSequentialGroup()
  200.                 .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  201.                 .addGap(0, 214, Short.MAX_VALUE))
  202.         );
  203.  
  204.         pack();
  205.     }// </editor-fold>                        
  206.  
  207.     private void txtusernameActionPerformed(java.awt.event.ActionEvent evt) {                                            
  208.         // TODO add your handling code here:
  209.     }                                          
  210.  
  211.     private void txtpasswordActionPerformed(java.awt.event.ActionEvent evt) {                                            
  212.         // TODO add your handling code here:
  213.     }                                          
  214.  
  215.     /**
  216.      * @param args the command line arguments
  217.      */
  218.     public static void main(String args[]) {
  219.         /* Set the Nimbus look and feel */
  220.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  221.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  222.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  223.          */
  224.         try {
  225.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  226.                 if ("Nimbus".equals(info.getName())) {
  227.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  228.                     break;
  229.                 }
  230.             }
  231.         } catch (ClassNotFoundException ex) {
  232.             java.util.logging.Logger.getLogger(LoginForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  233.         } catch (InstantiationException ex) {
  234.             java.util.logging.Logger.getLogger(LoginForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  235.         } catch (IllegalAccessException ex) {
  236.             java.util.logging.Logger.getLogger(LoginForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  237.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  238.             java.util.logging.Logger.getLogger(LoginForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  239.         }
  240.         //</editor-fold>
  241.  
  242.         /* Create and display the form */
  243.         java.awt.EventQueue.invokeLater(new Runnable() {
  244.             public void run() {
  245.                 new LoginForm().setVisible(true);
  246.             }
  247.         });
  248.     }
  249.  
  250.     // Variables declaration - do not modify                    
  251.     private javax.swing.JButton jButton1;
  252.     private javax.swing.JButton jButton2;
  253.     private javax.swing.JLabel jLabel1;
  254.     private javax.swing.JLabel jLabel2;
  255.     private javax.swing.JPanel jPanel1;
  256.     private javax.swing.JScrollPane jScrollPane1;
  257.     private javax.swing.JTable jTable1;
  258.     private javax.swing.JPasswordField txtpassword;
  259.     private javax.swing.JTextField txtusername;
  260.     // End of variables declaration                  
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement