create table user
(
username VARCHAR(50) not null,
password VARCHAR(50),
gender INTEGER,
hobby VARCHAR(500),
address VARCHAR(50),
degree VARCHAR(50)
);
成员变量参考12.1
public User(String username, String password, int sex, String hobby, String address, String degree)
程序设计
package com.qst.dms.entity;
public class User {
public String username;
public String password;
public Integer gender;
public String hobby;
public String address;
public String degree;
public User() {
}
public User(String username, String password, Integer gender, String hobby, String address, String degree) {
this.username = username;
this.password = password;
this.gender = gender;
this.hobby = hobby;
this.address = address;
this.degree = degree;
}
/**
* 获取
* @return username
*/
public String getUsername() {
return username;
}
/**
* 设置
* @param username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* 获取
* @return password
*/
public String getPassword() {
return password;
}
/**
* 设置
* @param password
*/
public void setPassword(String password) {
this.password = password;
}
/**
* 获取
* @return gender
*/
public Integer getGender() {
return gender;
}
/**
* 设置
* @param gender
*/
public void setGender(Integer gender) {
this.gender = gender;
}
/**
* 获取
* @return hobby
*/
public String getHobby() {
return hobby;
}
/**
* 设置
* @param hobby
*/
public void setHobby(String hobby) {
this.hobby = hobby;
}
/**
* 获取
* @return address
*/
public String getAddress() {
return address;
}
/**
* 设置
* @param address
*/
public void setAddress(String address) {
this.address = address;
}
/**
* 获取
* @return degree
*/
public String getDegree() {
return degree;
}
/**
* 设置
* @param degree
*/
public void setDegree(String degree) {
this.degree = degree;
}
public String toString() {
return "User{username = " + username + ", password = " + password + ", gender = " + gender + ", hobby = " + hobby + ", address = " + address + ", degree = " + degree + "}";
}
}
// 根据用户名查询用户,各用户的用户名不能相同
public User findUserByName(String userName)
// 保存用户信息
public boolean saveUser(User user)
程序设计
package com.qst.dms.service;
import com.qst.dms.entity.User;
import com.qst.dms.util.DBUtil;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserService {
DBUtil db = new DBUtil();
Connection conn = null;
// 根据用户名查询用户,各用户的用户名不能相同
public User findUserByName(String userName) {
// 返回符合条件的用户对象
User user = new User();
try {
try {
conn = db.getConnection();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
String querySqluser = "SELECT * FROM user WHERE username = ?";
Object[] userParams = {userName};
ResultSet userResult = db.executeQuery(querySqluser, userParams);
db.commitAll();
if (userResult.next()) {
// 设置登出记录的属性值
user.setUsername(userResult.getString("username"));
user.setPassword(userResult.getString("password"));
user.setAddress(userResult.getString("address"));
user.setGender(userResult.getInt("gender"));
user.setHobby(userResult.getString("hobby"));
user.setDegree(userResult.getString("degree"));
}
userResult.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭数据库连接,释放资源
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return user;
}
// 保存用户信息
public boolean saveUser(User user) {
// 返回保存结果,成功返回true,失败返回false
try {
try {
conn = db.getConnection();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
String querySqluser = "INSERT INTO user VALUES (?, ?, ?, ?, ?, ?)";
Object[] queryParams = {user.getUsername(), user.getPassword(), user.getGender(), user.getHobby(), user.getAddress(), user.getDegree()};
db.executeUpdate(querySqluser, queryParams);
db.commitAll();
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭数据库连接,释放资源
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return false;
}
}
Frame
// 注册监听器,监听确定按钮
btnOk.addActionListener(new RegisterListener());
// 注册监听器,监听重置按钮
btnCancle.addActionListener(new ResetListener());
// 主面板
private JPanel p;
// 标签
private JLabel lblName, lblPwd, lblRePwd, lblSex, lblHobby, lblAdress,
lblDegree;
// 用户名,文本框
private JTextField txtName;
// 密码和确认密码,密码框
private JPasswordField txtPwd, txtRePwd;
// 性别,单选按钮
private JRadioButton rbMale, rbFemale;
// 爱好,多选框
private JCheckBox ckbRead, ckbNet, ckbSwim, ckbTour;
// 地址,文本域
private JTextArea txtAdress;
// 学历,组合框
private JComboBox<String> cmbDegree;
// 确认和取消,按钮
private JButton btnOk, btnCancle;
// 注册的用户
private static User user;
// 用户业务类
private UserService userService;
// 构造方法
public RegistFrame() {
super("用户注册");
// 实例化用户业务类对象
userService = new UserService();
// 设置窗体的icon
ImageIcon icon = new ImageIcon("images\\dms.png");
this.setIconImage(icon.getImage());
// 设置面板布局,网格布局
p = new JPanel(new GridLayout(8, 1));
// 实例化组件
lblName = new JLabel("用 户 名:");
lblPwd = new JLabel("密 码:");
lblRePwd = new JLabel("确认密码:");
lblSex = new JLabel("性 别:");
lblHobby = new JLabel("爱 好:");
lblAdress = new JLabel("地 址:");
lblDegree = new JLabel("学 历:");
txtName = new JTextField(16);
txtPwd = new JPasswordField(16);
txtRePwd = new JPasswordField(16);
rbMale = new JRadioButton("男");
rbFemale = new JRadioButton("女");
// 性别的单选逻辑
ButtonGroup bg = new ButtonGroup();
bg.add(rbMale);
bg.add(rbFemale);
ckbRead = new JCheckBox("阅读");
ckbNet = new JCheckBox("上网");
ckbSwim = new JCheckBox("游泳");
ckbTour = new JCheckBox("旅游");
txtAdress = new JTextArea(3, 20);
// 组合框显示的学历数组
String str[] = { "小学", "初中", "高中", "本科", "硕士", "博士" };
cmbDegree = new JComboBox<String>(str);
// 设置组合框可编辑
cmbDegree.setEditable(true);
btnOk = new JButton("确定");
// 注册监听器,监听确定按钮
btnOk.addActionListener(new RegisterListener());
btnCancle = new JButton("重置");
// 注册监听器,监听重置按钮
btnCancle.addActionListener(new ResetListener());
// 将组件分组放入面板,然后将小面板放入主面板
JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p1.add(lblName);
p1.add(txtName);
p.add(p1);
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p2.add(lblPwd);
p2.add(txtPwd);
p.add(p2);
JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p3.add(lblRePwd);
p3.add(txtRePwd);
p.add(p3);
JPanel p4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p4.add(lblSex);
p4.add(rbMale);
p4.add(rbFemale);
p.add(p4);
JPanel p5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p5.add(lblHobby);
p5.add(ckbRead);
p5.add(ckbNet);
p5.add(ckbSwim);
p5.add(ckbTour);
p.add(p5);
JPanel p6 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p6.add(lblAdress);
p6.add(txtAdress);
p.add(p6);
JPanel p7 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p7.add(lblDegree);
p7.add(cmbDegree);
p.add(p7);
JPanel p8 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p8.add(btnOk);
p8.add(btnCancle);
p.add(p8);
// 主面板放入窗体中
this.add(p);
// 设置窗体大小和位置居中
this.setSize(310, 350);
this.setLocationRelativeTo(null);
// 设置窗体不可改变大小
this.setResizable(false);
// 设置窗体初始可见
this.setVisible(true);
}
// 监听类,负责处理确认按钮的业务逻辑
private class RegisterListener implements ActionListener {
// 重写actionPerFormed()方法,事件处理方法
public void actionPerformed(ActionEvent e) {
// 获取用户输入的数据
}
}
// 监听类,负责处理重置按钮
public class ResetListener implements ActionListener {
// 重写actionPerFormed()方法,重置组件内容事件处理方法
public void actionPerformed(ActionEvent e) {
// 清空姓名、密码、确认密码内容
txtName.setText("");
txtPwd.setText("");
txtRePwd.setText("");
// 重置单选按钮为未选择
rbMale.setSelected(false);
rbFemale.setSelected(false);
// 重置所有的复选按钮为未选择
ckbRead.setSelected(false);
ckbNet.setSelected(false);
ckbSwim.setSelected(false);
ckbTour.setSelected(false);
// 清空地址栏
txtAdress.setText("");
// 重置组合框为未选择状态
cmbDegree.setSelectedIndex(0);
}
}
程序设计
package com.qst.dms.ui;
import com.qst.dms.entity.User;
import com.qst.dms.service.UserService;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class RegistFrame extends JFrame{
// 主面板
private JPanel p;
// 标签
private JLabel lblName, lblPwd, lblRePwd, lblSex, lblHobby, lblAdress,
lblDegree;
// 用户名,文本框
private static JTextField txtName;
// 密码和确认密码,密码框
private static JPasswordField txtPwd;
private static JPasswordField txtRePwd;
// 性别,单选按钮
private static JRadioButton rbMale;
private static JRadioButton rbFemale;
// 爱好,多选框
private static JCheckBox ckbRead;
private static JCheckBox ckbNet;
private static JCheckBox ckbSwim;
private static JCheckBox ckbTour;
// 地址,文本域
private static JTextArea txtAdress;
// 学历,组合框
private static JComboBox<String> cmbDegree;
// 确认和取消,按钮
private JButton btnOk, btnCancle;
// 注册的用户
private static User user;
// 用户业务类
private UserService userService;
private Image iconImage;
// 构造方法
public RegistFrame() {
super();
// 实例化用户业务类对象
userService = new UserService();
// 设置窗体的icon
ImageIcon icon = new ImageIcon("images\\dms.png");
this.setIconImage(icon.getImage());
// 设置面板布局,网格布局
p = new JPanel(new GridLayout(8, 1));
// 实例化组件
lblName = new JLabel("用 户 名:");
lblPwd = new JLabel("密 码:");
lblRePwd = new JLabel("确认密码:");
lblSex = new JLabel("性 别:");
lblHobby = new JLabel("爱 好:");
lblAdress = new JLabel("地 址:");
lblDegree = new JLabel("学 历:");
txtName = new JTextField(16);
txtPwd = new JPasswordField(16);
txtRePwd = new JPasswordField(16);
rbMale = new JRadioButton("男");
rbFemale = new JRadioButton("女");
// 性别的单选逻辑
ButtonGroup bg = new ButtonGroup();
bg.add(rbMale);
bg.add(rbFemale);
ckbRead = new JCheckBox("阅读");
ckbNet = new JCheckBox("上网");
ckbSwim = new JCheckBox("游泳");
ckbTour = new JCheckBox("旅游");
txtAdress = new JTextArea(3, 20);
// 组合框显示的学历数组
String str[] = { "小学", "初中", "高中", "本科", "硕士", "博士" };
cmbDegree = new JComboBox<String>(str);
// 设置组合框可编辑
cmbDegree.setEditable(true);
btnOk = new JButton("确定");
// 注册监听器,监听确定按钮
btnOk.addActionListener((ActionListener) new RegisterListener());
btnCancle = new JButton("重置");
// 注册监听器,监听重置按钮
btnCancle.addActionListener(new ResetListener());
// 将组件分组放入面板,然后将小面板放入主面板
JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p1.add(lblName);
p1.add(txtName);
p.add(p1);
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p2.add(lblPwd);
p2.add(txtPwd);
p.add(p2);
JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p3.add(lblRePwd);
p3.add(txtRePwd);
p.add(p3);
JPanel p4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p4.add(lblSex);
p4.add(rbMale);
p4.add(rbFemale);
p.add(p4);
JPanel p5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p5.add(lblHobby);
p5.add(ckbRead);
p5.add(ckbNet);
p5.add(ckbSwim);
p5.add(ckbTour);
p.add(p5);
JPanel p6 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p6.add(lblAdress);
p6.add(txtAdress);
p.add(p6);
JPanel p7 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p7.add(lblDegree);
p7.add(cmbDegree);
p.add(p7);
JPanel p8 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p8.add(btnOk);
p8.add(btnCancle);
p.add(p8);
// 主面板放入窗体中
this.add(p);
// 设置窗体大小和位置居中
this.setSize(310, 400);
this.setLocationRelativeTo(null);
// 设置窗体不可改变大小
this.setResizable(false);
// 设置窗体初始可见
this.setVisible(true);
}
public void setIconImage(Image iconImage) {
this.iconImage = iconImage;
}
// 监听类,负责处理确认按钮的业务逻辑
private class RegisterListener implements ActionListener {
// 重写actionPerFormed()方法,事件处理方法
public void actionPerformed(ActionEvent e) {
// 获取用户输入的数据
String userName = txtName.getText().trim();
String password = new String(txtPwd.getPassword());
String rePassword = new String(txtRePwd.getPassword());
int sex = Integer.parseInt(rbFemale.isSelected()?"0":"1");
String hobby = (ckbRead.isSelected()?"阅读":"")
+ (ckbNet.isSelected()?"上网":"")
+ (ckbNet.isSelected()?"游泳":"")
+ (ckbNet.isSelected()?"旅游":"");
String address = txtAdress.getText().trim();
String degree = cmbDegree.getSelectedItem().toString().trim();
//判断两次输入密码是否一致
if (password.equals(rePassword)){
//将数据封装到对象中
user = new User(userName, password, sex, hobby, address, degree);
//保存数据
if (userService.saveUser(user)){
//输出提示信息
JOptionPane.showMessageDialog(null,"注册成功!","成功提示",JOptionPane.PLAIN_MESSAGE);
}else{
//输出提示信息
JOptionPane.showMessageDialog(null,"注册失败!","错误提示",JOptionPane.ERROR_MESSAGE);
}
}else{
//输出提示信息
JOptionPane.showMessageDialog(null,"两次输入的密码不一致!","错误提示",JOptionPane.ERROR_MESSAGE);
}
}
}
// 监听类,负责处理重置按钮
public static class ResetListener implements ActionListener {
// 重写actionPerFormed()方法,重置组件内容事件处理方法
public void actionPerformed(ActionEvent e) {
// 清空姓名、密码、确认密码内容
txtName.setText("");
txtPwd.setText("");
txtRePwd.setText("");
// 重置单选按钮为未选择
rbMale.setSelected(false);
rbFemale.setSelected(false);
// 重置所有的复选按钮为未选择
ckbRead.setSelected(false);
ckbNet.setSelected(false);
ckbSwim.setSelected(false);
ckbTour.setSelected(false);
// 清空地址栏
txtAdress.setText("");
// 重置组合框为未选择状态
cmbDegree.setSelectedIndex(0);
}
}
}
// 主面板
private JPanel p;
// 标签
private JLabel lblName, lblPwd;
// 用户名,文本框
private JTextField txtName;
// 密码,密码框
private JPasswordField txtPwd;
// 确认、取消和注册,按钮
private JButton btnOk, btnCancle, btnRegist;
// 登录用户
private static User user;
// 用户业务类
private UserService userService;
// 构造方法
public LoginFrame() {
super("登录");
// 实例化用户业务类对象
userService = new UserService();
// 设置窗体的icon
ImageIcon icon = new ImageIcon("images\\dms.png");
this.setIconImage(icon.getImage());
// 实例化组件
p = new JPanel();
// 使用null布局
p.setLayout(null);
lblName = new JLabel("用户名:");
lblPwd = new JLabel("密 码:");
txtName = new JTextField(20);
txtPwd = new JPasswordField(20);
txtPwd.setEchoChar('*');
btnOk = new JButton("登录");
btnOk.addActionListener(new LoginListener());
btnCancle = new JButton("重置");
btnCancle.addActionListener(new ResetListener());
btnRegist = new JButton("注册");
btnRegist.addActionListener(new RegistListener());
lblName.setBounds(30, 30, 60, 25);
lblPwd.setBounds(30, 60, 60, 25);
txtName.setBounds(95, 30, 120, 25);
txtPwd.setBounds(95, 60, 120, 25);
btnOk.setBounds(30, 90, 60, 25);
btnCancle.setBounds(95, 90, 60, 25);
btnRegist.setBounds(160, 90, 60, 25);
p.add(lblName);
p.add(txtName);
p.add(lblPwd);
p.add(txtPwd);
p.add(btnOk);
p.add(btnCancle);
p.add(btnRegist);
// 主面板放入窗体中
this.add(p);
// 设置窗体大小和位置
this.setSize(250, 170);
// 设置窗口在屏幕中央
this.setLocationRelativeTo(null);
// 设置窗体不能改变大小
this.setResizable(false);
// 设置窗体的默认关闭按钮
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置窗体初始可见
this.setVisible(true);
}
// 监听类,负责处理登录按钮
public class LoginListener implements ActionListener {
// 重写actionPerFormed()方法,事件处理逻辑
public void actionPerformed(ActionEvent e) {
// 根据用户名查询用户
}
}
// 监听类,负责处理重置按钮
public class ResetListener implements ActionListener {
// 重写actionPerFormed()方法,事件处理方法
public void actionPerformed(ActionEvent e) {
// 清空文本框
txtName.setText("");
txtPwd.setText("");
}
}
// 监听类,负责处理注册按钮
public class RegistListener implements ActionListener {
// 重写actionPerFormed()方法,事件处理方法
public void actionPerformed(ActionEvent e) {
// 创建注册窗口
new RegistFrame();
}
}
程序设计
package com.qst.dms.ui;
import com.qst.dms.entity.User;
import com.qst.dms.service.UserService;
import sun.rmi.log.ReliableLog;
import sun.rmi.runtime.Log;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginFrame extends JFrame {
// 主面板
private JPanel p;
// 标签
private JLabel lblName, lblPwd;
// 用户名,文本框
private JTextField txtName;
// 密码,密码框
private JPasswordField txtPwd;
// 确认、取消和注册,按钮
private JButton btnOk, btnCancle, btnRegist;
// 登录用户
private static User user;
// 用户业务类
private UserService userService;
// 构造方法
public LoginFrame() {
super("登录");
// 实例化用户业务类对象
userService = new UserService();
// 设置窗体的icon
ImageIcon icon = new ImageIcon("images\\dms.png");
this.setIconImage(icon.getImage());
// 实例化组件
p = new JPanel();
// 使用null布局
p.setLayout(null);
lblName = new JLabel("用户名:");
lblPwd = new JLabel("密 码:");
txtName = new JTextField(20);
txtPwd = new JPasswordField(20);
txtPwd.setEchoChar('*');
btnOk = new JButton("登录");
btnOk.addActionListener((ActionListener) new LoginListener());
btnCancle = new JButton("重置");
btnCancle.addActionListener(new RegistFrame.ResetListener());
btnRegist = new JButton("注册");
btnRegist.addActionListener(new RegistListener());
lblName.setBounds(30, 30, 60, 25);
lblPwd.setBounds(30, 60, 60, 25);
txtName.setBounds(95, 30, 120, 25);
txtPwd.setBounds(95, 60, 120, 25);
btnOk.setBounds(30, 90, 60, 25);
btnCancle.setBounds(95, 90, 60, 25);
btnRegist.setBounds(160, 90, 60, 25);
p.add(lblName);
p.add(txtName);
p.add(lblPwd);
p.add(txtPwd);
p.add(btnOk);
p.add(btnCancle);
p.add(btnRegist);
// 主面板放入窗体中
this.add(p);
// 设置窗体大小和位置
this.setSize(250, 170);
// 设置窗口在屏幕中央
this.setLocationRelativeTo(null);
// 设置窗体不能改变大小
this.setResizable(false);
// 设置窗体的默认关闭按钮
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置窗体初始可见
this.setVisible(true);
}
// 监听类,负责处理登录按钮
public class LoginListener implements ActionListener {
// 重写actionPerFormed()方法,事件处理逻辑
public void actionPerformed(ActionEvent e) {
// 根据用户名查询用户
user = userService.findUserByName((txtName.getText().trim()));
if(user != null) {
//判断输入的密码是否正确
if (user.getPassword().equals(new String(txtPwd.getPassword()))) {
//输出提示信息
JOptionPane.showMessageDialog(null,"登录成功!","成功提示",JOptionPane.PLAIN_MESSAGE);
//登录成功
LoginFrame.this.setVisible(false);
//显示主窗口
//new MainFrame();
} else {
//输出提示信息
JOptionPane.showMessageDialog(null, "密码错误!请重新输入!", "错误提示", JOptionPane.ERROR_MESSAGE);
//清空密码框
txtPwd.setText("");
}
}else{
//输出提示信息
JOptionPane.showMessageDialog(null,"该用户不存在,请先注册!","错误提示",JOptionPane.ERROR_MESSAGE);
}
}
}
// 监听类,负责处理重置按钮
public class ResetListener implements ActionListener {
// 重写actionPerFormed()方法,事件处理方法
public void actionPerformed(ActionEvent e) {
// 清空文本框
txtName.setText("");
txtPwd.setText("");
}
}
// 监听类,负责处理注册按钮
public class RegistListener implements ActionListener {
// 重写actionPerFormed()方法,事件处理方法
public void actionPerformed(ActionEvent e) {
// 创建注册窗口
new RegistFrame();
}
}
}
JOptionPane.showMessageDialog(null,"密码错误!请重新输入!","错误提示", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(null,"该用户不存在,请先注册!","错误提示",JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(null,"注册成功!","成功提示",JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(null,"注册失败!","错误提示",JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(null,"两次输入的密码不一致!","错误提示",JOptionPane.ERROR_MESSAGE);
测试
登录界面