Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.util.*;
  5. import java.sql.*;
  6.  
  7. public class GuessAB {
  8.     public static void main(String args[]) {
  9.         new MVC_AB() ;
  10.     }
  11. }
  12.  
  13.  
  14. class MVC_AB implements ActionListener {
  15.     JFrame f;
  16.     Container c;
  17.     JTextField txt;
  18.     JButton btn;
  19.     JPasswordField  pss;
  20.  
  21.     MVC_AB() {
  22.  
  23.         f=new JFrame("GuessAB");
  24.         f.setLayout(new FlowLayout());
  25.         f.setSize(400,300);
  26.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.         c=f.getContentPane();
  28.  
  29.         txt=new JTextField(20);
  30.         c.add(txt);
  31.  
  32.         pss=new JPasswordField(20);
  33.         c.add(pss);
  34.  
  35.  
  36.         btn=new JButton("Submit");
  37.         c.add(btn);
  38.         btn.addActionListener(this);
  39.  
  40.         f.setVisible(true);
  41.     }
  42.  
  43.  
  44.  
  45.  
  46.  
  47.     public void actionPerformed(ActionEvent e) {
  48.         String str=txt.getText();
  49.         try {
  50.             Class.forName("com.mysql.jdbc.Driver");
  51.         } catch (ClassNotFoundException e1) {
  52.         };
  53.  
  54.         try {
  55.             Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3307/db1?user=abc&password=123");
  56.             Statement stmt=conn.createStatement();
  57.             ResultSet rs=stmt.executeQuery("SELECT * FROM tb1");
  58.             Boolean found=false;
  59.  
  60.             while (rs.next()) {
  61.                 System.out.println(rs.getString("user")+""+txt.getText()+""+rs.getString("pswd")+""+pss.getText());
  62.                 if((rs.getString("user").equals(txt.getText()))&&(rs.getString("pswd").equals(pss.getText()))) {
  63.                     found=true;
  64.                     break;
  65.                 };
  66.             };
  67.             rs.close();
  68.             conn.close();
  69.  
  70.             if(found) {
  71.                 JOptionPane.showMessageDialog(f,"Sucessful!");
  72.                 txt.setText("");
  73.                 pss.setText("");
  74.             } else {
  75.                 JOptionPane.showMessageDialog(f,"Fail!");
  76.                 txt.setText("");
  77.                 pss.setText("");
  78.             }
  79.  
  80.         } catch(SQLException e2) {
  81.         }
  82.  
  83.  
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement