温馨提示×

mysql怎么实现用户注册

九三
186
2020-12-06 18:12:42
栏目: 云计算

mysql怎么实现用户注册

利用JDBC与MySQL实现一个用户注册功能

具体方法如下:

package cn.edu.hbue.qyf;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.SQLException;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JTextField;

@SuppressWarnings("serial")

public class HomeWork6 extends JFrame implements ActionListener{

private JTextField custNameTextField;

private JTextField codeNameTextField;

private JTextField recodeNameTextField;

private JLabel custNameLabel;

private JLabel codeLabel;

private JLabel recodeLabel;

private JButton confirmButton;

private JButton cancelButton;

public HomeWork6(){

setSize(250, 200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container cp = this.getContentPane();

cp.setLayout(new FlowLayout());

custNameLabel = new JLabel(" 用 户 名 ");//创建第一个标签和文本框

cp.add(custNameLabel);

custNameTextField = new JTextField(10);

cp.add(custNameTextField);

codeLabel = new JLabel(" 输 入 密 码 ");//创建第二个标签和文本框

cp.add(codeLabel);

codeNameTextField = new JTextField(10);

cp.add(codeNameTextField);

recodeLabel = new JLabel("再 次 输 入 密 码");//创建第三个标签和文本框

cp.add(recodeLabel);

recodeNameTextField= new JTextField(10);

cp.add(recodeNameTextField);

//创建按钮

confirmButton = new JButton("注册");

confirmButton.addActionListener(this);

cp.add(confirmButton);

cancelButton = new JButton("取消");

cp.add(cancelButton);

cancelButton.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

if(e.getActionCommand().equals("注册")){

// 1. 从各个控件收集用户输入的数据

String cusName = custNameTextField.getText();//字符串

String code = codeNameTextField.getText();//字符串

String recode = recodeNameTextField.getText();//字符串

//2. 检测字段是否为空

if(cusName.equals("")){

JOptionPane.showMessageDialog(getContentPane(), "请填写用户名!",

"信息提示框", JOptionPane.INFORMATION_MESSAGE);

return ;

}

else if(code.equals("")){

JOptionPane.showMessageDialog(getContentPane(), "密码不能为空!",

"信息提示框", JOptionPane.INFORMATION_MESSAGE);

return ;

}

else if(recode.equals("")){

JOptionPane.showMessageDialog(getContentPane(), "必须重复输入密码!",

"信息提示框", JOptionPane.INFORMATION_MESSAGE);

return ;

}

//3.合法性检查

int length = code.length();

if(!(length >= 6 && length <= 10)){

JOptionPane.showMessageDialog(getContentPane(), "密码格式错误!",//密码长度限定

"信息提示框", JOptionPane.INFORMATION_MESSAGE);

return ;

}

if (!recode.equals(code))

JOptionPane.showInputDialog(confirmButton, "两次密码不一致,请重新输入");

//4. 连接数据库,构造insert语句,向数据库中插入记录,关闭与数据库的连接

Connection conn = JDBCutil.getConn();

String sql = "insert into information(cusName,code,recode) values(?,?,?)";

PreparedStatement preStmt=null;

try {

preStmt= conn.prepareStatement(sql);

preStmt.setString(1, cusName);

preStmt.setString(2, code);

preStmt.setString(3, recode);

//显示信息提示框

int i = -1;

i = preStmt.executeUpdate();//如果小于1表示插入失败

if(i >= 1)//显示数据插入成功,并清除字段

JOptionPane.showMessageDialog(confirmButton, "注册成功!");

else

JOptionPane.showMessageDialog(confirmButton, "注册失败!(账号或密码已存在)

");

preStmt.executeUpdate();

System.out.println("成功插入数据"+ preStmt.getUpdateCount() + "条");

} catch (SQLException e1) {

e1.printStackTrace();

}finally{

JDBCutil.closeConn(preStmt,conn);

}

//5. 如插入成功,提示用户新增成功,并清空字段。插入不成功,提示新增失败

}else{

HomeWork6.this.dispose();

}

}

public static void main(String[] args){

HomeWork6 insert = new HomeWork6();

insert.setVisible(true);

}

}

0