⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 changepwd.java

📁 一个基于JAVA技术的销售信息管理系统
💻 JAVA
字号:
import java.awt.Toolkit;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import myEncryption.MyEncryption;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ChangePWD extends Dialog {

	private Text text_2;
	private Text text_1;
	private Text text;
	private MessageBox messageBox;
	protected Object result;

	protected Shell shell;
	
	private String ID;

	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public ChangePWD(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * @param parent
	 */
	public ChangePWD(Shell parent,String ID) {
		this(parent, SWT.NONE);

		this.ID	=	ID;
	}

	/**
	 * Open the dialog
	 * @return the result
	 */
	public Object open() {
		createContents();
		setCenter();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	public void setCenter(){
		// 得到屏幕的宽度和高度
		int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
		int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
		// 得到Shell窗口的宽度和高度
		int shellHeight = this.shell.getBounds().height;
		int shellWidth = this.shell.getBounds().width;
		// 如果窗口大小超过屏幕大小,让窗口与屏幕等大
		if (shellHeight > screenHeight)
			shellHeight = screenHeight;
		if (shellWidth > screenWidth)
			shellWidth = screenWidth;
		// 让窗口在屏幕中间显示
		this.shell.setLocation(((screenWidth - shellWidth) / 2),
				((screenHeight - shellHeight) / 2));
	}
	

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(344, 274);
		shell.setText("修改密码");

		text = new Text(shell, SWT.PASSWORD | SWT.BORDER);
		text.setEchoChar('\b');
		text.setTextLimit(20);
		text.setBounds(136, 40, 112, 22);

		text_1 = new Text(shell, SWT.PASSWORD | SWT.BORDER);
		text_1.setEchoChar('\b');
		text_1.setTextLimit(20);
		text_1.setBounds(136, 95, 112, 22);

		text_2 = new Text(shell, SWT.PASSWORD | SWT.BORDER);
		text_2.setEchoChar('\b');
		text_2.setTextLimit(20);
		text_2.setBounds(136, 136, 112, 22);

		final Label label = new Label(shell, SWT.RIGHT);
		label.setText("请输入旧密码:");
		label.setBounds(40, 47, 90, 12);

		final Label label_1 = new Label(shell, SWT.RIGHT);
		label_1.setText("请输入新密码:");
		label_1.setBounds(40, 100, 90, 12);

		final Label label_2 = new Label(shell, SWT.RIGHT);
		label_2.setText("新密码确认:");
		label_2.setBounds(40, 139, 90, 12);

		final Label label_3 = new Label(shell, SWT.NONE);
		label_3.setText("(1-20位)");
		label_3.setBounds(254, 100, 48, 12);

		final Button button = new Button(shell, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if(text_1.getText().length() > 20)
				{
					messageBox = new MessageBox(shell,SWT.ICON_ERROR);
					messageBox.setMessage("请输入最多 20 位密码");
					messageBox.setText("修改密码");
					messageBox.open();
				}
				else
					if(!text_1.getText().equals(text_2.getText()))
					{
						messageBox = new MessageBox(shell,SWT.ICON_ERROR);
						messageBox.setMessage("新密码不一致,请重新输入");
						messageBox.setText("修改密码");
						messageBox.open();
					}
					else
					{
						if(!changing(ID,text.getText(),text_1.getText()))
						{
							messageBox = new MessageBox(shell,SWT.ICON_ERROR);
							messageBox.setMessage("原密码错误,请重新输入");
							messageBox.setText("修改密码");
							messageBox.open();
						}
						else
						{
							messageBox = new MessageBox(shell,SWT.ICON_INFORMATION);
							messageBox.setMessage("密码修改成功");
							messageBox.setText("修改密码");
							messageBox.open();
							shell.dispose();
						}
					}
				
			}
		});
		button.setText("确  定");
		button.setBounds(82, 177, 68, 22);

		final Button button_1 = new Button(shell, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				shell.dispose();
			}
		});
		button_1.setText("取 消");
		button_1.setBounds(180, 177, 68, 22);
		//
	}
	
	public boolean changing(String id,String pwd,String PWD)
	{

		boolean right	=	false;
		Connection con;
		Statement stmt;
		String query;
		ResultSet rs;

		MyEncryption me = new MyEncryption();
		pwd	=	String.valueOf(me.passwordMaker(pwd));

		
		try{
			DBCon dbc = new DBCon();
			con = DriverManager.getConnection(dbc.url,"staff","123");
			stmt = con.createStatement();

			/**
			 * 实现sql和程序的字符转换
			 */
			//向转换表插入数据
			query = "update Change " +
					"set ExChange = '"+ pwd +"';";
			stmt.executeUpdate(query);

			//读出插入后的数据
			query = "select ExChange from Change ;";
			rs = stmt.executeQuery(query);
			while( rs.next()){
				pwd	=	rs.getString("ExChange");
			}
			if(rs	!=	null)
				rs.close();
				
			query = "select Password from Staff where ID = '"+id+"';";
			rs = stmt.executeQuery(query);
			while( rs.next()){
				if(pwd.equals(rs.getString("Password")))
					right = true;

			}
			
			
			if(rs	!=	null)
				rs.close();

			PWD	=	String.valueOf(me.passwordMaker(PWD));

			if(right)
			{
				query = "update Staff " +
						"set Password = '"+ PWD +
						"' where ID = '"+id+"';";
				stmt.executeUpdate(query);
			}
			else
				return false;
			
			dbc.dbClose(con,rs);
			
			return true;
		}
		catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -