📄 findpassword.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class FindPassword extends JDialog
implements ActionListener
{
private JLabel label1=new JLabel("用户名:");
private JLabel label2=new JLabel("密码:");
private JLabel label4=new JLabel("我的密码是什么?");
private JLabel o=new JLabel("");
private JLabel oo=new JLabel("");
private JLabel ooo=new JLabel("");
private JLabel oooo=new JLabel("");
private JTextField username=new JTextField();
private JTextField password=new JTextField();
private JPanel jpTop=new JPanel();
private JPanel jpMain=new JPanel();
private JPanel jpBottom=new JPanel();
private JPanel jpAll=new JPanel(new BorderLayout());
private JButton jbtOK=new JButton("查找");
private JButton jbtCancel=new JButton("取消");
private Font font1=new Font("宋体",Font.BOLD,18);
private Connection myCon;
private Statement myStatement;
private ResultSet myResultSet,myResultSetagain;
public FindPassword()
{
//ConnetDataBase();
AddTop();
AddMain();
AddBottom();
this.setTitle("找回密码");
this.getContentPane().add(jpAll);
this.setSize(250,240);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((d.width - this.getSize().width) / 2,
(d.height - this.getSize().height) / 2);
this.setVisible(true);
}
public void AddTop()
{
label4.setFont(font1);
label4.setBackground(new Color(225,245,253));
label4.setForeground(Color.RED);
label4.setHorizontalAlignment(JLabel.CENTER);
jpTop.setBackground(new Color(225,245,253));
jpTop.setLayout(new BorderLayout());
jpTop.add(label4,BorderLayout.CENTER);
jpAll.add(jpTop,BorderLayout.NORTH);
}
public void AddMain()
{
jpMain.setLayout(new GridLayout(4,2,0,10));
jpMain.setBackground(new Color(225,245,253));
jpMain.add(o);
jpMain.add(oo);
jpMain.add(label1);
jpMain.add(username);
jpMain.add(label2);
password.setEditable(false);
jpMain.add(password);
jpMain.add(ooo);
jpMain.add(oooo);
jpAll.add(jpMain,BorderLayout.CENTER);
}
public void AddBottom()
{
jpBottom.setBackground(new Color(225,245,253));
jbtOK.addActionListener(this);
jpBottom.add(jbtOK);
jbtCancel.addActionListener(this);
jpBottom.add(jbtCancel);
jpAll.add(jpBottom,BorderLayout.SOUTH);
}
//public void ConnetDataBase()
//{
//}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="查找")
{
int door=1;
username.selectAll();
String strusername=username.getSelectedText();
if(strusername==null)
{
JOptionPane.showMessageDialog(null,"用户名不能是空!",
"提示",JOptionPane.ERROR_MESSAGE);
}
else
{
String strpassword=null;
try {
//①加载驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException ex) {
//驱动程序加载不成功,打印错误信息并退出
System.out.println("Can not find driver " );
System.exit( -1);
}
Connection con;
try {
//②获得jdbc 连接
con = DriverManager.getConnection("jdbc:odbc:userlist", "user", "");
//③创建Statement对象
Statement stmt = con.createStatement();
//④得到查询结果集
String sql = "select * from userlist where username='"
+ username.getText() + "'";
ResultSet rs = stmt.executeQuery(sql);
//System.out.print(new String(rs.getString("password")));
//列印结果集
while(rs.next())
{
String ps = rs.getString("username");
String pass = rs.getString("password");
if (ps.equals(username.getText())) {
//验证通过
JOptionPane.showMessageDialog(null,"查找成功!\n"+
"用户名:"+ps+"\n密码:"+pass+"\n克格勃提示您请牢记您的"+
"用户名及密码!","恭喜您",JOptionPane.INFORMATION_MESSAGE);
password.setText(pass);
door=0;
}
}
if(door==1)
{
JOptionPane.showMessageDialog(null,"无此用户!","错误"
,JOptionPane.INFORMATION_MESSAGE);
}
//⑤关闭数据库连接
rs.close();
stmt.close();
con.close();
}
catch (SQLException sqe) {
sqe.printStackTrace();
}
}
}
else if(e.getActionCommand()=="取消")
{
this.dispose();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -