JDBC:找回密码

来源:互联网 发布:微信斗图软件 编辑:程序博客网 时间:2024/06/05 21:55

dao

package com.Test.dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;import java.util.ArrayList;import java.util.List;import com.Test.Util.DBUtil;import com.Test.dto.UsernameDTO;public class UsernameDAO {    //显示密码    public String findPass(String empPassword){        Connection conn = null;        PreparedStatement statement = null;        ResultSet rs = null;        try{            conn = DBUtil.getConnection();            String sql = "select * from Employee where empPassword=? ";            statement = conn.prepareStatement(sql);            statement.setString(2, empPassword);            rs = statement.executeQuery();            if(rs.next()) {                return empPassword;//显示员工密码            }            else {                return null;            }        }catch(Exception ee){            ee.printStackTrace();        }        return null;    }    //输入账号 密保和答案 正确public boolean findUser(String empAccount, String empQuestion,String empAnswer) {    Connection conn = null;    PreparedStatement statement = null;    ResultSet rs = null;    try {        conn = DBUtil.getConnection();        String sql = "select * from Employee where empAccount=? and empQuestion=? and empAnswer=?";        statement = conn.prepareStatement(sql);        statement.setString(1, empAccount);        statement.setString(8, empQuestion);        statement.setString(9, empAnswer);        rs = statement.executeQuery();        if(rs.next()) {            return true;        }        else {            return false;        }    }    catch(Exception e) {        e.printStackTrace();        return false;    }    finally {        try{            rs.close();            statement.close();            conn.close();        }        catch(Exception e1){            e1.printStackTrace();        }    }  }public boolean findUser(UsernameDTO userdto) {    // TODO Auto-generated method stub    return false;}}

dto

package com.Test.dto;public class UsernameDTO {    private String empAccount,empQuestion,empAnswer,empPassword;    public String getEmpPassword() {        return empPassword;    }    public void setEmpPassword(String empPassword) {        this.empPassword = empPassword;    }    public String getEmpAccount() {        return empAccount;    }    public void setEmpAccount(String empAccount) {        this.empAccount = empAccount;    }    public String getEmpQuestion() {        return empQuestion;    }    public void setEmpQuestion(String empQuestion) {        this.empQuestion = empQuestion;    }    public String getEmpAnswer() {        return empAnswer;    }    public void setEmpAnswer(String empAnswer) {        this.empAnswer = empAnswer;    }}

UI

/** * 设计一个找回员工密码的界面(FindPasswordFrame), * 在三个文本框中分别输入员工帐号、密码保护问题和密码保护答案 * 如果输入全部正确,单击“找回密码”按钮,则在一个JLabel中显示员工密码。 */package com.Test.view;import java.awt.Component;import java.awt.Container;import java.awt.FlowLayout;import java.util.List;import java.awt.event.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.ArrayList;import java.util.Vector;import javax.swing.ComboBoxModel;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextField;import javax.swing.plaf.basic.BasicButtonListener;import javax.swing.plaf.basic.BasicOptionPaneUI.ButtonActionListener;import com.Test.dao.UsernameDAO;import com.Test.dto.UsernameDTO;public class UsernameDisplay extends JFrame{    private JLabel accountJlb,questionJlb,answerJlb,displayJlb;    private JTextField tf1,tf2,tf3;    private JButton findBtn;    //构造方法    public UsernameDisplay() {        this.setLayout(null);        Container c = this.getContentPane();        accountJlb = new JLabel("员工帐号:");        accountJlb.setBounds(80,30,70,30);        c.add(accountJlb);        tf1 = new JTextField();        tf1.setBounds(190,30,190,30);        c.add(tf1);        questionJlb = new JLabel("密码保护问题:");        questionJlb.setBounds(80,50,100,90);        c.add(questionJlb);        tf2 = new JTextField();        tf2.setBounds(190,80,190,30);        c.add(tf2);        answerJlb = new JLabel("密码保护答案:");        answerJlb.setBounds(80,100,100,90);        c.add(answerJlb);        tf3 = new JTextField();        tf3.setBounds(190,130,190,30);        c.add(tf3);        findBtn = new JButton("找回密码");        findBtn.setBounds(140,190,90,30);        c.add(findBtn);//      //?//      displayJlb.setBounds(200,190,90,30);//      c.add(displayJlb);        //FindPassword        findBtn.addActionListener(new ActionListener()          {           public void actionPerformed(ActionEvent e)           {              String userID = tf1.getText().trim();              String pw =  tf2.getText().trim();  //获取密码保护的输入              String pw1 =  tf3.getText().trim();               JButton tempButton=(JButton)e.getSource();            if(tempButton==findBtn)            {             if(userID.equals(""))             {               JOptionPane.showMessageDialog(null,"账号不能为空!","错误提示",JOptionPane.ERROR_MESSAGE);                 }                else if(pw.equals(""))                {               JOptionPane.showMessageDialog(null,"密码保护问题不能为空!","错误提示",JOptionPane.ERROR_MESSAGE);                          }                else if(pw1.equals(""))                {               JOptionPane.showMessageDialog(null,"密码保护答案不能为空!","错误提示",JOptionPane.ERROR_MESSAGE);                          }               else              {              UsernameDAO userdao = new UsernameDAO();              UsernameDTO userdto = new UsernameDTO();              userdto.setEmpAccount(userID);              userdto.setEmpQuestion(pw);              userdto.setEmpAnswer(pw1);              boolean result = userdao.findUser(userdto);              if(result) {               String empPassword = null;            JOptionPane.showMessageDialog(null, userdao.findPass(empPassword));               //单击“找回密码”按钮,则在一个JLabel中显示员工密码。              }              else {               JOptionPane.showMessageDialog(null, "失败!");              }           }}          }});         this.setTitle("FindPassword");        this.setSize(500,300);        this.setLocationRelativeTo(null);        int w = Toolkit.getDefaultToolkit().getScreenSize().width;        int h = Toolkit.getDefaultToolkit().getScreenSize().height;        this.setLocation(w/2-250, h/2-180);        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        this.setVisible(true);    }    public static void main(String[] args) {            new UsernameDisplay();    }}

DBUtil

package com.Test.Util;import java.sql.Connection;import java.sql.DriverManager;public class DBUtil {    public static Connection getConnection(){        try{            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");            Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=HappyDB;user=sa;password=123456");            return conn;        }        catch (Exception e) {        }        return null;    }}
原创粉丝点击