本章为我的期末项目,参考意义不大。其中有参考别人(howard2005)的代码和错误。
创建用户实体类User
public class User {
private int id;
private String username;
private String password;
private String telephone;
private Date registerTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Date getRegisterTime() {
return registerTime;
}
public void setRegisterTime(Date registerTime) {
this.registerTime = registerTime;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", telephone='" + telephone + '\'' +
", registerTime=" + registerTime +
'}';
}
}
数据库连接管理类ConnectionManager
public class ConnectionManager {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/student?useSSL=false";
private static final String USER = "root";
private static final String PASSWORD = "123456";
private ConnectionManager() {
}
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USER, PASSWORD);
System.out.println("提示:数据库连接成功!");
} catch (ClassNotFoundException e) {
System.err.println("异常:数据库驱动程序未找到!");
} catch (SQLException e) {
System.err.println("异常:数据库连接失败!");
}
return conn;
}
public static void closeConnection(Connection conn) {
if (conn != null) {
try {
if (!conn.isClosed()) {
conn.close();
System.out.println("提示:数据库连接关闭!");
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
}
public static void main(String[] args) {
Connection conn = getConnection();
closeConnection(conn);
}
}
运行结果:
用户数据访问接口UserDao
public interface UserDao {
int insert(User user);
int delete(int id);
int update(User user);
User findById(int id);
List<User> findAll();
User login(String username, String password);
}
用户数据访问接口实现类UserDaoImpl
public class UserDaoImpl implements UserDao {
@Override
public int insert(User user) {
return 0;
}
@Override
public int delete(int id) {
return 0;
}
@Override
public int update(User user) {
return 0;
}
@Override
public User findById(int id) {
return null;
}
@Override
public List<User> findAll() {
return null;
}
@Override
public User login(String username, String password) {
User user = null;
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_user where username = ? and password = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, username);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
}
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
ConnectionManager.closeConnection(conn);
}
return user;
}
}
}
对UserDaoImpl进行单元测试TestUserDaoImpl
public class TestUserDaoImpl {
private UserDao userDao;
@Before
public void init() {
userDao = new UserDaoImpl();
}
@Test
public void testLogin() {
String username = "admin";
String password = "11111";
User user = userDao.login(username, password);
if (user != null) {
System.out.println("恭喜,[" + username + "]登录成功~");
} else {
System.out.println("遗憾,[" + username + "]登录失败~");
}
}
用户服务类UserService
public class UserService {
private UserDao userDao;
public UserService() {
userDao = new UserDaoImpl();
}
public int addUser(User user) {
return userDao.insert(user);
}
public int deleteUser(int id) {
return userDao.delete(id);
}
public int updateUser(User user) {
return userDao.update(user);
}
public User findUserById(int id) {
return userDao.findById(id);
}
public List<User> findAllUsers() {
return userDao.findAll();
}
public User login(String username, String password) {
return userDao.login(username, password);
}
}
用户登录界面LoginFrame
public class LoginFrame extends JFrame {
private String username;
private String password;
private JLabel lblUsername;
private JLabel lblPassword;
private JTextField txtUsername;
private JPasswordField txtPassword;
private JButton btnOK;
private JButton btnCancel;
private JPanel panel, panel1, panel2, panel3;
public LoginFrame(String title) {
super(title);
initGUI();
}
private void initGUI() {
panel = (JPanel) getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
lblUsername = new JLabel("用户名:");
lblPassword = new JLabel("密 码:");
txtUsername = new JTextField(15);
txtPassword = new JPasswordField(15);
btnOK = new JButton("确定[O]");
btnCancel = new JButton("取消[C]");
panel1.add(lblUsername);
panel1.add(txtUsername);
panel2.add(lblPassword);
panel2.add(txtPassword);
panel3.add(btnOK);
panel3.add(btnCancel);
panel.setLayout(new GridLayout(3, 1));
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
btnOK.setMnemonic(KeyEvent.VK_O);
btnCancel.setMnemonic(KeyEvent.VK_C);
txtPassword.setEchoChar('*');
setSize(250, 200);
setLocationRelativeTo(null);
setResizable(false);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new LoginFrame("用户登录");
}
}
用户登录功能LoginFrame
public class LoginFrame extends JFrame {
private String username;
private String password;
private JLabel lblUsername;
private JLabel lblPassword;
private JTextField txtUsername;
private JPasswordField txtPassword;
private JButton btnOK;
private JButton btnCancel;
private JPanel panel, panel1, panel2, panel3;
public LoginFrame(String title) {
super(title);
initGUI();
eventsHandling();
}
private void eventsHandling() {
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
username = txtUsername.getText().trim();
password = new String(txtPassword.getPassword());
UserService userService = new UserService();
User user = userService.login(username, password);
if (user != null) {
JOptionPane.showMessageDialog(null, "恭喜,[" + username + "]登录成功!");
dispose();
} else {
JOptionPane.showMessageDialog(null, "遗憾,用户名或密码错误!");
txtUsername.setText("");
txtPassword.setText("");
txtUsername.requestFocus();
}
}
});
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
private void initGUI() {
panel = (JPanel) getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
lblUsername = new JLabel("用户名:");
lblPassword = new JLabel("密码:");
txtUsername = new JTextField(15);
txtPassword = new JPasswordField(15);
btnOK = new JButton("确定[O]");
btnCancel = new JButton("取消[C]");
panel1.add(lblUsername);
panel1.add(txtUsername);
panel2.add(lblPassword);
panel2.add(txtPassword);
panel3.add(btnOK);
panel3.add(btnCancel);
panel.setLayout(new GridLayout(3, 1));
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
btnOK.setMnemonic(KeyEvent.VK_O);
btnCancel.setMnemonic(KeyEvent.VK_C);
txtPassword.setEchoChar('*');
setSize(250, 200);
setLocationRelativeTo(null);
setResizable(false);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new LoginFrame("用户登录");
}
}
注册账号Register
public class Register extends JFrame {
JLabel username;
JLabel password;
JLabel passwordAgain;
JTextField usernameFile;
JPasswordField passwordFile;
JPasswordField PasswordFileAgain;
JButton sign;
JPanel p;
JPanel p1;
final int WIDTH = 310;
final int HEIGHT = 510;
public Register() {
init();
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
validate();
setVisible(true);
Toolkit kit = Toolkit.getDefaultToolkit();
int width = screenSize.width;
int height = screenSize.height;
int x = (width - WIDTH) / 2;
int y = (height - HEIGHT) / 2;
setBounds(x, y, WIDTH, HEIGHT);
setTitle("账号注册");
}
void init() {
p = new JPanel();
p.setLayout(null);
p.setVisible(true);
p1 = new JPanel();
p1.setVisible(true);
p1.setLayout(null);
p1.setBounds(0, 0, 400, 600);
username = new JLabel("账号:");
password = new JLabel("密码:");
passwordAgain = new JLabel("确认密码:");
usernameFile = new JTextField(15);
passwordFile = new JPasswordField(15);
PasswordFileAgain = new JPasswordField(15);
passwordFile.setEchoChar('*');
PasswordFileAgain.setEchoChar('*');
sign = new JButton("注册");
sign.setBounds(160, 300, 100, 50);
username.setBounds(130, 150, 40, 15);
password.setBounds(130, 200, 40, 15);
passwordAgain.setBounds(130, 250, 120, 15);
usernameFile.setBounds(200, 150, 100, 15);
passwordFile.setBounds(200, 200, 100, 15);
PasswordFileAgain.setBounds(200, 250, 100, 15);
p1.add(username);
p1.add(password);
p1.add(passwordAgain);
p1.add(usernameFile);
p1.add(passwordFile);
p1.add(PasswordFileAgain);
p1.add(sign);
p.add(p1);
this.add(p);
sign.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String strusername = usernameFile.getText();
String strpassword = new String(passwordFile.getPassword());
String strpasswordAgain = new String(PasswordFileAgain.getPassword());
UserDaoImpl userDaoImpl = new UserDaoImpl();
try {
User users = sqlHelp.userpassweord(strusername);
if (users == null) {
if (strpassword.equals(strpasswordAgain)) {
System.out.println("注册成功!");
String username = strusername;
String password = strpassword;
sqlHelp.addManeger(username,password);
JOptionPane.showMessageDialog(null, "注册成功!", "点击确定后返回登录界面", JOptionPane.INFORMATION_MESSAGE);
} else {
System.out.println("注册失败!");
JOptionPane.showMessageDialog(null, "注册失败,请检查两次密码是否相同!", "登录失败提示窗口", JOptionPane.INFORMATION_MESSAGE);
}
}else{
System.out.println("已有账号无法注册!");
JOptionPane.showMessageDialog(null,"注册失败了,已拥有该账号,请检查密码是否输入错误,目前不支持找回密码","账号已有窗口",JOptionPane.INFORMATION_MESSAGE);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
});
}
public static void main(String[] args) {
new Register();
}
}
查询窗口Query
public class Query extends JFrame {
private JPanel contentPane;
private JTextField idField;
private JTextField nameField;
private JTextField genderField;
private JTextField dobField;
private JTextField batchField;
private JTextField searchField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
QueryFrame frame = new QueryFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Query() {
setResizable(false);
setTitle("查询信息");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 470);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JPanel panel = new JPanel();
contentPane.add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("请输入要查找的学生的学号:");
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel.setBounds(71, 0, 208, 29);
panel.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("学号:");
lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1.setBounds(71, 105, 50, 30);
panel.add(lblNewLabel_1);
idField = new JTextField();
idField.setEditable(false);
idField.setBounds(143, 99, 240, 45);
panel.add(idField);
idField.setColumns(10);
JLabel lblNewLabel_1_1 = new JLabel("姓名:");
lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1.setBounds(71, 160, 50, 30);
panel.add(lblNewLabel_1_1);
nameField = new JTextField();
nameField.setColumns(10);
nameField.setBounds(143, 154, 240, 45);
panel.add(nameField);
JLabel lblNewLabel_1_1_1 = new JLabel("性别:");
lblNewLabel_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1_1.setBounds(71, 215, 50, 30);
panel.add(lblNewLabel_1_1_1);
genderField = new JTextField();
genderField.setColumns(10);
genderField.setBounds(143, 209, 240, 45);
panel.add(genderField);
JLabel lblNewLabel_1_1_1_1 = new JLabel("出生日期:");
lblNewLabel_1_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30);
panel.add(lblNewLabel_1_1_1_1);
dobField = new JTextField();
dobField.setColumns(10);
dobField.setBounds(143, 264, 240, 45);
panel.add(dobField);
JLabel lblNewLabel_1_1_1_2 = new JLabel("班级:");
lblNewLabel_1_1_1_2.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30);
panel.add(lblNewLabel_1_1_1_2);
batchField = new JTextField();
batchField.setColumns(10);
batchField.setBounds(143, 319, 240, 45);
panel.add(batchField);
searchField = new JTextField();
searchField.setBounds(71, 39, 208, 45);
panel.add(searchField);
searchField.setColumns(10);
JButton searchButton = new JButton("查找");
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int id = Integer.parseInt(searchField.getText());
try {
UserDaoImpl userDaoImpl = new UserDaoImpl();
Student student = userDaoImpl.queryStudent(id);
if(student != null) {
idField.setText(String.valueOf(id));
nameField.setText(student.getName());
genderField.setText(student.getGender());
dobField.setText(student.getDob());
batchField.setText(String.valueOf(student.getBatch()));
} else {
JOptionPane.showMessageDialog(QueryFrame.this, "查无此人!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
});
searchButton.setFont(new Font("宋体", Font.PLAIN, 18));
searchButton.setBounds(289, 42, 97, 39);
panel.add(searchButton);
}
}
修改信息 Change
public class ChangeFrame extends JFrame {
private JPanel contentPane;
private JTextField idField;
private JTextField nameField;
private JTextField genderField;
private JTextField dobField;
private JTextField batchField;
private JTextField searchField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChangeFrame frame = new ChangeFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ChangeFrame() {
setResizable(false);
setTitle("修改信息");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 470);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JPanel panel = new JPanel();
contentPane.add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("请输入要修改的学生的学号:");
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel.setBounds(71, 0, 208, 29);
panel.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("学号:");
lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1.setBounds(71, 105, 50, 30);
panel.add(lblNewLabel_1);
idField = new JTextField();
idField.setEditable(false);
idField.setBounds(143, 99, 240, 45);
panel.add(idField);
idField.setColumns(10);
JLabel lblNewLabel_1_1 = new JLabel("姓名:");
lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1.setBounds(71, 160, 50, 30);
panel.add(lblNewLabel_1_1);
nameField = new JTextField();
nameField.setColumns(10);
nameField.setBounds(143, 154, 240, 45);
panel.add(nameField);
JLabel lblNewLabel_1_1_1 = new JLabel("性别:");
lblNewLabel_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1_1.setBounds(71, 215, 50, 30);
panel.add(lblNewLabel_1_1_1);
genderField = new JTextField();
genderField.setColumns(10);
genderField.setBounds(143, 209, 240, 45);
panel.add(genderField);
JLabel lblNewLabel_1_1_1_1 = new JLabel("出生日期:");
lblNewLabel_1_1_1_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30);
panel.add(lblNewLabel_1_1_1_1);
dobField = new JTextField();
dobField.setColumns(10);
dobField.setBounds(143, 264, 240, 45);
panel.add(dobField);
JLabel lblNewLabel_1_1_1_2 = new JLabel("班级:");
lblNewLabel_1_1_1_2.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30);
panel.add(lblNewLabel_1_1_1_2);
batchField = new JTextField();
batchField.setColumns(10);
batchField.setBounds(143, 319, 240, 45);
panel.add(batchField);
JButton changeButton = new JButton("修改");
changeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int id = Integer.parseInt(searchField.getText());
String nameString = nameField.getText();
String gendeString = genderField.getText();
String dobString = dobField.getText();
String batch = batchField.getText();
UserDaoImpl userDaoImpl = new UserDaoImpl();
try {
userDaoImpl.changeStudent(id, nameString, gendeString, dobString, batch);
JOptionPane.showMessageDialog(ChangeFrame.this, "修改成功!");
} catch (SQLException e) {
JOptionPane.showMessageDialog(ChangeFrame.this, "修改失败!");
e.printStackTrace();
}
}
});
changeButton.setFont(new Font("宋体", Font.PLAIN, 18));
changeButton.setBounds(182, 389, 97, 33);
panel.add(changeButton);
searchField = new JTextField();
searchField.setBounds(71, 39, 208, 45);
panel.add(searchField);
searchField.setColumns(10);
JButton searchButton = new JButton("查找");
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
int id = Integer.parseInt(searchField.getText());
try {
UserDaoImpl userDaoImpl = new UserDaoImpl();
Student student = userDaoImpl.queryStudent(id);
if(student != null) {
idField.setText(String.valueOf(id));
nameField.setText(student.getName());
genderField.setText(student.getGender());
dobField.setText(student.getDob());
batchField.setText(String.valueOf(student.getBatch()));
} else {
JOptionPane.showMessageDialog(ChangeFrame.this, "无此用户");
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
searchButton.setFont(new Font("宋体", Font.PLAIN, 18));
searchButton.setBounds(289, 42, 97, 39);
panel.add(searchButton);
}
}
添加信息Add
public class Add extends JFrame {
private JPanel contentPane;
private JTextField idField;
private JTextField nameField;
private JTextField genderField;
private JTextField dobField;
private JTextField batchField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Add frame = new Add();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Add() {
setResizable(false);
setTitle("添加学生");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 470);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JPanel panel = new JPanel();
contentPane.add(panel);
panel.setLayout(null);
JLabel TitleLabel = new JLabel("请输入新学生的信息:");
TitleLabel.setFont(new Font("宋体", Font.BOLD, 20));
TitleLabel.setBounds(71, 34, 220, 45);
panel.add(TitleLabel);
JLabel idLabel = new JLabel("学号:");
idLabel.setFont(new Font("宋体", Font.PLAIN, 16));
idLabel.setBounds(71, 105, 50, 30);
panel.add(idLabel);
idField = new JTextField();
idField.setBounds(143, 99, 240, 45);
panel.add(idField);
idField.setColumns(10);
JLabel nameLabel = new JLabel("姓名:");
nameLabel.setFont(new Font("宋体", Font.PLAIN, 16));
nameLabel.setBounds(71, 160, 50, 30);
panel.add(nameLabel);
nameField = new JTextField();
nameField.setColumns(10);
nameField.setBounds(143, 154, 240, 45);
panel.add(nameField);
JLabel genderLabel = new JLabel("性别:");
genderLabel.setFont(new Font("宋体", Font.PLAIN, 16));
genderLabel.setBounds(71, 215, 50, 30);
panel.add(genderLabel);
genderField = new JTextField();
genderField.setColumns(10);
genderField.setBounds(143, 209, 240, 45);
panel.add(genderField);
JLabel dobLabel = new JLabel("出生日期:");
dobLabel.setFont(new Font("宋体", Font.PLAIN, 16));
dobLabel.setBounds(41, 270, 80, 30);
panel.add(dobLabel);
dobField = new JTextField();
dobField.setColumns(10);
dobField.setBounds(143, 264, 240, 45);
panel.add(dobField);
JLabel batchLabel = new JLabel("班级:");
batchLabel.setFont(new Font("宋体", Font.PLAIN, 16));
batchLabel.setBounds(71, 325, 50, 30);
panel.add(batchLabel);
batchField = new JTextField();
batchField.setColumns(10);
batchField.setBounds(143, 319, 240, 45);
panel.add(batchField);
JButton addButton = new JButton("添加");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(idField.getText());
String nameString = nameField.getText();
String genderString = genderField.getText();
String dobfieldString = dobField.getText();
int batch = Integer.parseInt(batchField.getText());
System.out.println(id + "\t" + nameString + "\t" + genderString + "\t" + dobfieldString + "\t" + batch);
UserDaoImpl userDaoImpl = new UserDaoImpl();
try {
userDaoImpl.addStudent(id, nameString, genderString, dobfieldString, batch);
JOptionPane.showMessageDialog(AddFrame.this, "添加成功!");
} catch (SQLException e1) {
if(e1.getSQLState().equals("23000")) {
JOptionPane.showMessageDialog(AddFrame.this, "添加失败!该学生已存在");
}
e1.printStackTrace();
}
}
});
addButton.setFont(new Font("宋体", Font.PLAIN, 18));
addButton.setBounds(182, 389, 97, 33);
panel.add(addButton);
}
}
|