功能要求:
1) 在磁盘上创建一个Mysql数据库,名称自定,在该数据库下建立一个数据表,名称为userTable,表结构如图12-4所示,表中的数据不要录入(用注册窗口添加)。
2) 用户在登录窗口点击注册进入注册界面,在注册窗口中输入账号、姓名、性别、出生日期、email和密码(要验证两次密码一致性)等信息后,单击“注册”按钮后完成用户注册功能。并且将数据添加到数据库中。
3) 注册帐号过程中,输入完账号后,回车,进行账号字符有效性验证,如果正确,将焦点自动切换到姓名文本框(不用鼠标切换);否则,弹出对话框,提示错误,并将焦点保持在账号文本框;用同样方法实现Email地址合法性验证;在确认密码输入结束后,按回车,完成注册功能(即等同于点击注册按钮)
4) 用户在登录窗口中输入用户和口令,单击“登录”按钮后,如果用户和口令与数据表文件中某一行匹配,则弹出对话框(对话框的文字提示信息是“登录成功!”),并进入“更改密码”窗口,否则弹出对话框(对话框的文字提示信息是“用户或口令不正确!”)
5) 在“更改密码”窗口中输入相应信息后,单击“确定”,完成更改密码功能。可以打开数据库验证是否成功!
6) 建议设计三个窗口类(登录、注册和更改密码)、一个用户类(至少包括以下方法:数据库连接方法、添加数据方法、登录验证方法、更改密码方法以及用于数据合法性检测的方法)。
package mytest;
public class Login {
boolean success = false;
String id;
String password;
public void setID(String string) {
this.id = string;
}
public void setPassword(String password) {
this.password = password;
}
public String getID() {
return id;
}
public String getPassword() {
return password;
}
public void setSuccess(boolean success) {
this.success = success;
}
public boolean getSuccess() {
return success;
}
}
package mytest;
public class Change {
boolean success = false;
String id;
String password;
public void setID(String string) {
this.id = string;
}
public void setPassword(String password) {
this.password = password;
}
public String getID() {
return id;
}
public String getPassword() {
return password;
}
public void setSuccess(boolean success) {
this.success = success;
}
public boolean getSuccess() {
return success;
}
}
package mytest;
public class Register {
String id;
String password;
String email;
String sex;
String name;
String birth;
public void setID(String string) {
this.id = string;
}
public void setPassword(String password) {
this.password = password;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setEmail(String email) {
this.email = email;
}
public void setBirth(String birth) {
this.birth = birth;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getID() {
return id;
}
public String getPassword() {
return password;
}
public String getSex() {
return sex;
}
public String getEmail() {
return email;
}
public String getBirth() {
return birth;
}
}
package mytest;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import mytest.*;
import mytest.Login;
import mytest.Register;
public class RegAndLog extends JFrame {
RLview rl;
RegAndLog(){
setBounds(100,100,800,260);
rl = new RLview();
add(rl);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
public static void main(String args[]) {
RegAndLog window = new RegAndLog();
window.setTitle("登录");
}
}
package mytest;
import java.awt.*;
import javax.swing.*;
import mytest.Register;
import java.awt.event.*;
public class RegisterView extends JPanel implements ActionListener, ItemListener{
Register register;
JTextField id,name,email,password1,password2;
JComboBox sex,year,month,day;
int[] DAY={31,28,31,30,31,30,31,31,30,31,30,31};
JButton reg,esc;
RegisterView(){
register = new Register();
id = new JTextField(15);
name =new JTextField(15);
email =new JTextField(15);
password1 = new JTextField(15);
password2 =new JTextField(15);
reg = new JButton("注册");
esc = new JButton("退出");
add(new JLabel("账号:"));
add(id);
add(new JLabel("姓名:"));
add(name);
add(new JLabel("性别"));
sex = new JComboBox();
sex.addItem("male");
sex.addItem("female");
add(sex);
year=new JComboBox();
month=new JComboBox();
day=new JComboBox();
add(new JLabel("出生日期"));
for(int i=1910;i<2021;i++) {
year.addItem(i);
}
add(year);
add(new JLabel("年"));
for(int i=1;i<13;i++) {
month.addItem(i);
month.addItemListener(this);
}
add(month);
add(new JLabel("月"));
add(day);
add(new JLabel("日"));
// add(new JLabel("Email"));
add(new JLabel(“email:"));
add(email);
add(new JLabel("密码:"));
add(password1);
add(new JLabel("确认密码"));
add(password2);
add(reg);
add(esc);
reg.addActionListener(this);
esc.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==esc) {
System.exit(0);
}
if(e.getSource()==reg) {
register.setID(id.getText());
register.setName(name.getText());
register.setEmail(email.getText());
register.setSex(sex.getSelectedItem().toString());
String born = year.getSelectedItem().toString()+"-"+
month.getSelectedItem().toString()
+"-"+day.getSelectedItem().toString();
register.setBirth(born);
if(password1.getText().equals(password2.getText())) {
register.setPassword(password1.getText());
}
else {
JOptionPane.showMessageDialog(null,"密码前后不一致","警告",JOptionPane.WARNING_MESSAGE);
}
HandleReg hr = new HandleReg();
hr.writeReg(register);
}
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == month) {
String year = this.year.getSelectedItem().toString();//获取选中
//已知年份
String mon = this.month.getSelectedItem().toString();//获取月份
int y = Integer.valueOf(year);
int m = Integer.valueOf(mon);//转换为int
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
DAY[1] = 29;
} else {
DAY[1] = 28;//判断闰年
}
day.removeAllItems();//清空选项列表、
for (int i = 0; i < DAY[m - 1]; i++) {
day.addItem(i + 1);
}
}
}
}
package mytest;
import java.awt.*;
import javax.swing.*;
import mytest.*;
import mytest.Login;
import java.awt.event.*;
public class LoginView extends JPanel implements ActionListener{
Login login;
JTextField id;
JPasswordField pass;
JButton log,esc;
boolean success;
LoginView(){
login = new Login();
id = new JTextField(15);
pass = new JPasswordField(15);
log = new JButton("登录");
esc = new JButton("退出");
add(new JLabel("账号"));
add(id);
add(new JLabel("密码"));
add(pass);
add(log);
add(esc);
log.addActionListener(this);
esc.addActionListener(this);
}
public boolean isSuccess() {
return success;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==esc) {
System.exit(0);
}
if(e.getSource()==log) {
login.setID(id.getText());
char [] pw = pass.getPassword();
login.setPassword(new String (pw));
HandleLog hl = new HandleLog();
login = hl.queryVerify(login);
success = login.getSuccess();
}
}
}
package mytest;
import java.sql.*;
import javax.swing.JOptionPane;
import mytest.Register;
public class HandleReg {
Connection con;
PreparedStatement preSql;
public HandleReg() {
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e){}
String uri = "jdbc:mysql://localhost:3306/test?useSSL=true&characterEncoding=utf-8";
try { // 表名↑ 密码↓
con = DriverManager.getConnection(uri,"root","z123");
}
catch (SQLException e) {System.out.println(e.toString());}
}
public void writeReg(Register register) {//更新
String sqlStr = "insert into mess values(?,?,?,?,?,?)";
int ok = 0;
try {
preSql = con.prepareStatement(sqlStr);//链接
preSql.setString(1,register.getID());//更新数据
preSql.setString(2, register.getName());
preSql.setString(3, register.getSex());
preSql.setString(4, register.getBirth());
preSql.setString(5, register.getEmail());
preSql.setString(6, register.getPassword());
ok = preSql.executeUpdate();
con.close();
}
catch (NullPointerException e){
System.out.println(e.toString());
}
catch(SQLException e) {
System.out.println(e.toString());
JOptionPane.showMessageDialog(null,"id不能重复","警告",JOptionPane.WARNING_MESSAGE);//提示
}
if(ok!=0) {
JOptionPane.showMessageDialog(null,"注册成功","恭喜",JOptionPane.WARNING_MESSAGE);
}
}
}
package mytest;
import java.sql.*;
import javax.swing.JOptionPane;
import mytest.Login;
public class HandleLog {
Connection con;//链接
PreparedStatement pre;//驱动
ResultSet rs;
public HandleLog() {
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e){}
String uri = "jdbc:mysql://localhost:3306/test?useSSL=true&characterEncoding=utf-8";
try { // ku名 密码↓
con = DriverManager.getConnection(uri,"root","zyk123");
}
catch(Exception e){System.out.println(e.toString());}
}
public Login queryVerify(Login logins) {
String id = logins.getID();
String pw = logins.getPassword();
String sqlStr = "select userID,password from mess where userID = ? and password = ?";//注意语法
try {
pre = con.prepareStatement(sqlStr);
pre.setString(1,id);
pre.setString(2, pw);
rs = pre.executeQuery();
if(rs.next()==true) {
logins.setSuccess(true);
JOptionPane.showMessageDialog(null,"登录成功","恭喜",JOptionPane.WARNING_MESSAGE);
}
else {
logins.setSuccess(false);
JOptionPane.showMessageDialog(null,"登录失败","抱歉",JOptionPane.WARNING_MESSAGE);
}
}
catch(Exception e){
System.out.println(e.toString());
}
return logins;
}
}
package mytest;
import java.sql.*;
import javax.swing.JOptionPane;
import mytest.Login;
public class HandleChange {
Connection con;
PreparedStatement sql;
ResultSet rs;
public HandleChange() {
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e){}
String uri = "jdbc:mysql://localhost:3306/test?useSSL=true&characterEncoding=utf-8";
try { // 数据库名 密码↓
con = DriverManager.getConnection(uri,"root","z123");
}
catch(Exception e){
System.out.println(e.toString());
}
if(con==null) return;
}
public Change query(Change ch) {
String id = ch.getID();
String pw = ch.getPassword();
String sqlStr = "update mess set password=? where userID =?";
try {
sql = con.prepareStatement(sqlStr);
sql.setString(1,ch.getPassword());
sql.setString(2,ch.getID());
int ok = sql.executeUpdate();//括号里面不能再写sqlStr
ch.setSuccess(true);
System.out.println(ok);
}
catch(Exception e){
System.out.println(e.toString());
}
return ch;
}
}
package mytest;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import java.awt.*;
public class RLview extends JPanel{
JTabbedPane p;
RegisterView reg;
LoginView log;
ChangePass pa;
public RLview() {
reg = new RegisterView();
log = new LoginView();
pa = new ChangePass();
setLayout(new BorderLayout());
p =new JTabbedPane();
p.add("注册",reg);
p.add("登录",log);
p.add("修改密码",pa);
p.invalidate();
add(p,BorderLayout.CENTER);
}
}
package mytest;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import mytest.*;
import mytest.Login;
import mytest.Register;
public class RegAndLog extends JFrame {
RLview rl;
RegAndLog(){
setBounds(100,100,800,260);
rl = new RLview();
add(rl);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
public static void main(String args[]) {
RegAndLog window = new RegAndLog();
window.setTitle("登录");
}
}