Topic: 又有新问题了(判断密码 达不到效果)代码补全了

  Print this page

1.又有新问题了(判断密码 达不到效果)代码补全了 Copy to clipboard
Posted by: jialichao
Posted on: 2006-05-28 18:01

package mypackage;

import javax.swing.*;

import java.sql.*;
import java.awt.*;
import java.awt.event.*;

public class FramSet extends JFrame {

  /**
   * @param args
   */
  JLabel username;
  JLabel password;
  JLabel repassword;
  JLabel age;
  JLabel name;
  JLabel lname;
  JTextField usertext;
  JPasswordField passwordtext;
  JPasswordField repasswordtext;
  JTextField agetext;
  JButton submit;
  JRadioButton man;
  JRadioButton woman;
  String sex="男";

  public FramSet()
  {
    
    Container c=this.getContentPane();
  
    GridBagLayout gbl=new GridBagLayout();
    GridBagConstraints gbc= new GridBagConstraints();
  
    c.setLayout(gbl);
    gbc.anchor=GridBagConstraints.WEST;
    username=new JLabel("用 户 名 : ");
    gbc.gridx=0;
    gbc.gridy=0;
    gbl.setConstraints(username,gbc);
    c.add(username);
    
    usertext=new JTextField(15);
    gbc.gridx=1;
    gbc.gridy=0;
    gbl.setConstraints(usertext,gbc);
    c.add(usertext);
    
    password=new JLabel("密 码 : ");
    gbc.gridx=0;
    gbc.gridy=1;
    gbl.setConstraints(password,gbc);
    c.add(password);
    
    passwordtext=new JPasswordField(15);
    passwordtext.setEchoChar('*');
    gbc.gridx=1;
    gbc.gridy=1;
    gbl.setConstraints(passwordtext,gbc);
    c.add(passwordtext);
    
    repassword=new JLabel("确认密码 : ");
    gbc.gridx=0;
    gbc.gridy=2;
    gbl.setConstraints(repassword,gbc);
    c.add(repassword);
    
    repasswordtext=new JPasswordField(15);
    passwordtext.setEchoChar('*');
    gbc.gridx=1;
    gbc.gridy=2;
    gbl.setConstraints(repasswordtext,gbc);
    c.add(repasswordtext);
    
    name=new JLabel("性 别 :");
    gbc.gridx=0;
    gbc.gridy=3;
    gbl.setConstraints(name,gbc);
    c.add(name);
    
    Panel p=new Panel();
    p.setLayout(new FlowLayout());
    final ButtonGroup bg=new ButtonGroup();
    man=new JRadioButton("男",true);
    woman=new JRadioButton("女");
    p.add(man);
    p.add(woman);
    c.add(p);
    bg.add(man);
    bg.add(woman);
    gbc.gridx=1;
    gbc.gridy=3;
    gbl.setConstraints(p,gbc);
    
    age=new JLabel("年 龄 :");
    gbc.gridx=0;
    gbc.gridy=4;
    gbl.setConstraints(age,gbc);
    c.add(age);
    
    agetext=new JTextField(15);
    gbc.gridx=1;
    gbc.gridy=4;
    gbl.setConstraints(agetext,gbc);
    c.add(agetext);
    
   lname=new JLabel(" ");
    gbc.gridx=1;
    gbc.gridy=5;
    gbl.setConstraints(lname,gbc);
    c.add(lname);
    
    submit=new JButton("注册");
    gbc.gridx=1;
    gbc.gridy=6;
    gbl.setConstraints(submit,gbc);
    c.add(submit);
    
    final ImageIcon p1=new ImageIcon("SmsMsgSomeFail.gif");
    final ImageIcon p2=new ImageIcon("SmsMsgFail.gif");
    
    class MyListener implements ActionListener
    {
      public void actionPerformed(ActionEvent e)
      {
      try
      {
        if(e.getSource()==submit)
        {
          
          Connection con;
          PreparedStatement ptm;
          Statement sm;
          ResultSet rs;
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          String url="jdbc:odbc:mydb";
          con=DriverManager.getConnection(url,"sa","");
          
                  
          String name,age,pas,repas,temp;
          name=usertext.getText();
          pas=new String(passwordtext.getPassword());
          repas=new String(repasswordtext.getPassword());
          age=agetext.getText();
          
          
          
          boolean flag=true;
          ptm=con.prepareStatement("select name from users1");
          rs=ptm.executeQuery("select name from users1");
          String str;
          while(rs.next()){
            str=rs.getString("name");
            if(name.equals(str)){
              flag=false;
              break;
            }
            
            
          }
          
          if(flag){
           if(pas.equals(repas)){
            ptm=con.prepareStatement("insert into users1 values(?,?,?,?)");
            ptm.setString(1,name);
            ptm.setString(2,pas);
            ptm.setString(3,age);
            ptm.setString(4,sex);
            ptm.executeUpdate();
            JOptionPane.showMessageDialog(null,"注册成功 !欢迎您的加入 !","信息提示",JOptionPane.PLAIN_MESSAGE,p1);    
            ptm.close();
            con.close();
           }else{
             JOptionPane.showMessageDialog(null, "两次输入密码不相同 !", "错误提示",JOptionPane.PLAIN_MESSAGE,p2);
           }
          }else{
            JOptionPane.showMessageDialog(null, "用户名已经被注册 !", "错误提示",JOptionPane.PLAIN_MESSAGE,p2);
          }
          
          
          
          
          
        }
        else
        {
          sex=((JRadioButton)(e.getSource())).getText();
        }

      }
      catch(Exception ae){}
      }

      
    }
    

    
    MyListener mylistener=new MyListener();
    submit.addActionListener(mylistener);
    man.addActionListener(mylistener);
    woman.addActionListener(mylistener);
  }
  public static void main(String[] args) {
    // TODO 自动生成方法存根
    FramSet enrolfram=new FramSet();
    enrolfram.setTitle("新用户注册");
    enrolfram.setResizable(false);
    enrolfram.setSize(300,300);
    enrolfram.setVisible(true);

  }

}

全部代码在这里了,大家看看有什么问题。我试过用==号判断字符串是否相同是可以的,equals()是判断两个对象是否相同的吧,我觉得放这里用不是太合适啊。

2.Re:又有新问题了 [Re: jialichao] Copy to clipboard
Posted by: lisliefor
Posted on: 2006-05-28 19:19

代码不完整,看不出哪里出问题。
那个if(name==str)的地方,用equals()方法比较吧!(最近很多帖子都是这个问题)

3.Re:又有新问题了(判断密码 达不到效果) [Re: jialichao] Copy to clipboard
Posted by: why
Posted on: 2006-05-28 21:40

Please try to learn to debug your code using println or debugging features provided by the IDE.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923