京墨六和曲 2021-06-30 16:54 采纳率: 50%
浏览 54
已采纳

java 如果密码和确认密码不一致,怎么阻止进库并提示

package zesen;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Register extends JFrame implements ActionListener{
    JPanel panel;
    JLabel a1,a2,a3,a4;
    JTextField b1,b2,b3,b4;
    JButton c1,c2;
    
    public Register() {
        panel = new JPanel();
        panel.setLayout(null);
        add(panel);
        
        a1 = new JLabel("Name");
        a1.setBounds(50,50,100,30);
        a1.setFont(new Font("Verdana",Font.PLAIN,15));
        panel.add(a1);
        
        b1 = new JTextField("");
        b1.setBounds(200,50,200,30);
        b1.setFont(new Font("Verdana",Font.PLAIN,15));
        panel.add(b1);
        
        a2 = new JLabel("User ID");
        a2.setBounds(50,90,100,30);
        a2.setFont(new Font("Verdana",Font.PLAIN,15));
        panel.add(a2);
        
        b2 = new JTextField("");
        b2.setBounds(200,90,200,30);
        b2.setFont(new Font("Verdana",Font.PLAIN,15));
        panel.add(b2);
        
        a3 = new JLabel("password");
        a3.setBounds(50,130,100,30);
        a3.setFont(new Font("Verdana",Font.PLAIN,15));
        panel.add(a3);
        
        b3 = new JTextField("");
        b3.setBounds(200,130,200,30);
        b3.setFont(new Font("Verdana",Font.PLAIN,15));
        panel.add(b3);
        
        a4 = new JLabel("confirm password");
        a4.setBounds(50,170,200,30);
        a4.setFont(new Font("Verdana",Font.PLAIN,15));
        panel.add(a4);
        
        b4 = new JTextField("");
        b4.setBounds(200,170,200,30);
        b4.setFont(new Font("Verdana",Font.PLAIN,15));
        panel.add(b4);
        
        c1 = new JButton("OK");
        c1.setBounds(130,230,80,30);
        panel.add(c1);
        
        c2 = new JButton("Cancel");
        c2.setBounds(250,230,80,30);
        panel.add(c2);
        
        setTitle("Register Screen");
        setBounds(300,300,500,350);
        setVisible(true);
        
        c1.addActionListener(this);
        c2.addActionListener(this);
    }
    public static void main(String[] args){
       new Register();
    }
    @Override
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==c1) {
            InsertData();
        }
        if(e.getSource()==c2){
             this.dispose();
                }
    }
    public void InsertData() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//MySQL版本
            Connection con = DriverManager.getConnection("jdbc:mysql:"
                    + "//localhost:3306/yonghu?serverTimezone=UTC","root","123456");
            PreparedStatement pstmt = con.prepareStatement("insert into user values(?,?,?,?)");
            
            //在文本框里获取值然后传入MySQL里面
            pstmt.setString(1,b1.getText());
            pstmt.setString(2,b2.getText());
            pstmt.setString(3,b3.getText());
            pstmt.setString(4,b4.getText());

            //检查是否运行
            int i = pstmt.executeUpdate();
            if(b3.getText()==b4.getText()) {
                if(i != 0){
                JOptionPane.showMessageDialog(this, "恭喜你,注册成功!");
            }
            else{
                JOptionPane.showMessageDialog(this, "注册失败");
            }
        }
        else{
            JOptionPane.showMessageDialog(this, "密码不一致");
            }
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(this, e);//try 中出现any错误,都会到catch,e就会反映对应的错误
        }
    
    }    
}
  • 写回答

5条回答 默认 最新

  • L指挥官 2021-06-30 17:05
    关注

    字符串比较不能用==,要用equals

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 已采纳回答 7月18日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵