重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
你要想让程序记住你的修改结果是不可能的,应为你只要退出这个程序,所有的变量都就被清理了。当你再次重新运行程序时,所有的变量又被从新初始化赋值。你要想验证你的密码是不是被修改就得找那个保存文件的代码。将你的修改结果保存起来。要不然你是看不到结果的。。或者你就把你的那些写入到数据库中。然后在查询数据库。
创新互联主要从事网站设计制作、网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务尉氏,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220
你要是想修改本程序的。你这个程序中的密码是123.、那你就弄个输入程序。然后这个123替换掉不就行了嘛。if (name1.equals("xiaoming") pwd1.equals("123")) 。如果你是想让显示出来。当完成String p = scan.next();这句后,你把p的值就得到了。打印出来就可以了啊
public boolean Update(String username,ArrayList array)
{
Connection con;
Statement sql;
ResultSet rs;
boolean Flag = false;
//桥接法驱动
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){}
try
{
//test为跟数据库关联的数据源名
con = DriverManager.getConnection("jdbc:odbc:test","","");
sql = con.createStatement();
/* 假设数据库中用户表如下:
* 表名:user
* 各字段:
* username(用户名) password(密码) pflag(用户权限)
*
*/
rs = sql.executeQuery("select password from user where username='"+username+"'");
while(rs.next()){
Flag = true;
String password = rs.getString(1);
array.add(password);
}
}
catch(SQLException e1)
{
System.err.println(e1) ;
}
return Flag;
}
}
代码不完整,还有几个类
public class ATMChangePass extends JFrame {
private JPanel contentPane;
private JPasswordField txtOldPass, txtNewPass1, txtNewPass2;
private String user;
private Zhujiemian owner;
public ATMChangePass(Zhujiemian owner, String user) {
super("修改密码");
this.user=user;
this.owner=owner;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("\u8BF7\u8F93\u5165\u65E7\u5BC6\u7801");
label.setBounds(10, 43, 155, 33);
contentPane.add(label);
JLabel label_1 = new JLabel("\u8BF7\u8F93\u5165\u65B0\u5BC6\u7801");
label_1.setBounds(10, 113, 155, 33);
contentPane.add(label_1);
JLabel label_2 = new JLabel("\u8BF7\u518D\u6B21\u8F93\u5165\u65B0\u5BC6\u7801");
label_2.setBounds(10, 179, 155, 39);
contentPane.add(label_2);
txtOldPass = new JPasswordField();
txtOldPass.setColumns(10);
txtOldPass.setBounds(214, 49, 118, 21);
contentPane.add(txtOldPass);
txtNewPass1 = new JPasswordField();
txtNewPass1.setColumns(10);
txtNewPass1.setBounds(214, 119, 118, 21);
contentPane.add(txtNewPass1);
txtNewPass2 = new JPasswordField();
txtNewPass2.setColumns(10);
txtNewPass2.setBounds(214, 188, 118, 21);
contentPane.add(txtNewPass2);
JButton button = new JButton("修改");
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
doChangePass();
}
});
button.setBounds(74, 228, 93, 23);
contentPane.add(button);
JButton button_1 = new JButton("返回");
button_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
dispose();
ATMChangePass.this.owner.setVisible(true);
}
});
button_1.setBounds(239, 228, 93, 23);
contentPane.add(button_1);
setVisible(true);
}
private void doChangePass(){
if(txtOldPass.getText().length()6){
JOptionPane.showMessageDialog(this, "原密码不正确!", getTitle(), JOptionPane.WARNING_MESSAGE);
return;
}
if(txtNewPass2.getText().length()6){
JOptionPane.showMessageDialog(this, "新密码不正确!", getTitle(), JOptionPane.WARNING_MESSAGE);
return;
}
if(!txtNewPass1.getText().equals(txtNewPass2.getText())){
JOptionPane.showMessageDialog(this, "新密码不对应!", getTitle(), JOptionPane.WARNING_MESSAGE);
return;
}
JOptionPane.showMessageDialog(this, "修改成功!");
dispose();
}
public static void main(String args[])throws Exception{
new ATMChangePass(null, "user1");
}
}
首先,form表单必须的,是用来输入用户名和密码,brform表单 action="%=basePath %Admin.shtml" method="post",br主要指明的是请求的路径,请求的方式,post.bronSubmit="return checkPWD()"的目的就是在点击提交的时候执行的操作,。br成功的话就跳转到action所指定的路径去。不成功的话就是还在当前页。br/HEADbr%brString message = (String)request.getAttribute("message");brif(message == null){brmessage = "";br}brif (!message.trim().equals("")){brout.println("script language='javascript'");brout.println("alert('"+message+"');");brout.println("/script");br}brrequest.removeAttribute("message");br%br%brString username=(String)session.getAttribute("user");brif(username==null){brresponse.sendRedirect(basePath+"/error.jsp");br}brelse{brList list=(List)session.getAttribute("list");br%br从session里面获取userName的值,然后就是判断,如果是空的话就跳转到错误的页面,还有如果请求的信息如果有的话就弹出请求的信息的内容!!!
private void yesButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String name = usrNameText.getText();
String pwd = pwdText.getText().hashCode() + "";
String repwd = repeatPwdText.getText().hashCode() + "";
if (pwd.equalsIgnoreCase(repwd)) {
JOptionPane.showMessageDialog(null, "注册成功", "提示",
JOptionPane.PLAIN_MESSAGE);
User in = new User(name, pwd, GetTime.getTime());
DOM4User.insertUser(in);
this.setVisible(false);
} else {
JOptionPane.showMessageDialog(null, "两次密码不一致,重输入", "提示",
JOptionPane.PLAIN_MESSAGE);
}
}
package com.cube.limail.util;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;/**
* 加密解密类
*/
public class Eryptogram
{
private static String Algorithm ="DES";
private String key="CB7A92E3D3491964";
//定义 加密算法,可用 DES,DESede,Blowfish
static boolean debug = false ;
/**
* 构造子注解.
*/
public Eryptogram ()
{
} /**
* 生成密钥
* @return byte[] 返回生成的密钥
* @throws exception 扔出异常.
*/
public static byte [] getSecretKey () throws Exception
{
KeyGenerator keygen = KeyGenerator.getInstance (Algorithm );
SecretKey deskey = keygen.generateKey ();
System.out.println ("生成密钥:"+bytesToHexString (deskey.getEncoded ()));
if (debug ) System.out.println ("生成密钥:"+bytesToHexString (deskey.getEncoded ()));
return deskey.getEncoded ();
} /**
* 将指定的数据根据提供的密钥进行加密
* @param input 需要加密的数据
* @param key 密钥
* @return byte[] 加密后的数据
* @throws Exception
*/
public static byte [] encryptData (byte [] input ,byte [] key ) throws Exception
{
SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key ,Algorithm );
if (debug )
{
System.out.println ("加密前的二进串:"+byte2hex (input ));
System.out.println ("加密前的字符串:"+new String (input ));
} Cipher c1 = Cipher.getInstance (Algorithm );
c1.init (Cipher.ENCRYPT_MODE ,deskey );
byte [] cipherByte =c1.doFinal (input );
if (debug ) System.out.println ("加密后的二进串:"+byte2hex (cipherByte ));
return cipherByte ;
} /**
* 将给定的已加密的数据通过指定的密钥进行解密
* @param input 待解密的数据
* @param key 密钥
* @return byte[] 解密后的数据
* @throws Exception
*/
public static byte [] decryptData (byte [] input ,byte [] key ) throws Exception
{
SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key ,Algorithm );
if (debug ) System.out.println ("解密前的信息:"+byte2hex (input ));
Cipher c1 = Cipher.getInstance (Algorithm );
c1.init (Cipher.DECRYPT_MODE ,deskey );
byte [] clearByte =c1.doFinal (input );
if (debug )
{
System.out.println ("解密后的二进串:"+byte2hex (clearByte ));
System.out.println ("解密后的字符串:"+(new String (clearByte )));
} return clearByte ;
} /**
* 字节码转换成16进制字符串
* @param byte[] b 输入要转换的字节码
* @return String 返回转换后的16进制字符串
*/
public static String byte2hex (byte [] b )
{
String hs ="";
String stmp ="";
for (int n =0 ;n b.length ;n ++)
{
stmp =(java.lang.Integer.toHexString (b [n ] 0XFF ));
if (stmp.length ()==1 ) hs =hs +"0"+stmp ;
else hs =hs +stmp ;
if (n b.length -1 ) hs =hs +":";
} return hs.toUpperCase ();
}
/**
* 字符串转成字节数组.
* @param hex 要转化的字符串.
* @return byte[] 返回转化后的字符串.
*/
public static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) 4 | toByte(achar[pos + 1]));
}
return result;
}
private static byte toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}
/**
* 字节数组转成字符串.
* @param String 要转化的字符串.
* @return 返回转化后的字节数组.
*/
public static final String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i bArray.length; i++) {
sTemp = Integer.toHexString(0xFF bArray[i]);
if (sTemp.length() 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
/**
* 从数据库中获取密钥.
* @param deptid 企业id.
* @return 要返回的字节数组.
* @throws Exception 可能抛出的异常.
*/
public static byte[] getSecretKey(long deptid) throws Exception {
byte[] key=null;
String value=null;
//CommDao dao=new CommDao();
// List list=dao.getRecordList("from Key k where k.deptid="+deptid);
//if(list.size()0){
//value=((com.csc.sale.bean.Key)list.get(0)).getKey();
value = "CB7A92E3D3491964";
key=hexStringToByte(value);
//}
if (debug)
System.out.println("密钥:" + value);
return key;
}
public String encryptData2(String data) {
String en = null;
try {
byte[] key=hexStringToByte(this.key);
en = bytesToHexString(encryptData(data.getBytes(),key));
} catch (Exception e) {
e.printStackTrace();
}
return en;
}
public String decryptData2(String data) {
String de = null;
try {
byte[] key=hexStringToByte(this.key);
de = new String(decryptData(hexStringToByte(data),key));
} catch (Exception e) {
e.printStackTrace();
}
return de;
}
} 加密使用: byte[] key=Eryptogram.getSecretKey(deptid); //获得钥匙(字节数组)
byte[] tmp=Eryptogram.encryptData(password.getBytes(), key); //传入密码和钥匙,获得加密后的字节数组的密码
password=Eryptogram.bytesToHexString(tmp); //将字节数组转化为字符串,获得加密后的字符串密码解密与之差不多