一.项目介绍:
项目简介:
1.设计思路:采用MVC设计思路
M:model数据模型
V:view视图
C:controller业务逻辑控制,调度模块
2.实现登陆界面:选择登录身份:系统管理员登录、教师登录、学生登录。
3.实现学生信息系统主界面:包括修改密码、退出系统,对学生、班级、教师、课程、选课信息的增删改查,成绩管理等功能的实现。
4.Mysql数据库的连接。
5.实现增删改查,成绩管理功能。
需求功能分析:
二.建包:
com.artisan.dao:对数据库进行操作
com.artisan.model:定义数据库中的表
com.artisan.util:放实用操作、进行数据库连接等
com.artisan.view:可视化界面设计
三.创建名为db_student的数据库:
安装Navicat数据库管理工具
四.做一个登录界面
安装windowbulider插件:可用拖拽方式创建界面
五.创建一个枚举类
package com.artisan.model;
public enum UserType {
//让用户登录类型以中文显示
ADMIN("系统管理员",0),TEACHER("教师",1),STUDENT("学生",2);
private String name;
private int index;
private UserType(String name,int index){
this.name = name;
this.index = index;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
@Override
public String toString(){
//重写
return this.name;
}
}
package com.artisan.util;
public class StringUtil {
//判断是否为空
public static boolean isEmpty(String str){
if("".equals(str)|| str == null){
//防止出现异常
return true;
}
return false;
}
}
import com.artisan.view;
protected void loginAct(ActionEvent ae) {
// TODO Auto-generated method stub
String userName = userNameTextField.getText().toString();
String password passwordTextField.getText().toString();
UserType selectedItem = (UserType)userTypeComboBox.getSelectedItem();
if(StringUtil.isEmpty(userName)){
JOptionPane.showMessageDialog(this, "用户名不能为空!");
return;
}
if(StringUtil.isEmpty(password)){
JOptionPane.showMessageDialog(this, "密码不能为空!");
return;
}
@Override
public String toString(){
return this.name;
}
}
六.数据库连接以及管理员登录实现
package com.artisan.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DbUtil {
private String dbUrl="jdbc:mysql://localhost:3306/db_student?useUnicode=true&characterEncoding=utf8"; // 数据库连接地址
private String dbUserName="root"; // 用户名
private String dbPassword=""; // 密码
private String jdbcName="com.mysql.jdbc.Driver"; // 驱动名称
//获取数据库连接
public Connection getCon(){
try {
Class.forName(jdbcName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection con = null;
try {
con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
//关闭数据库连接
public void closeCon(Connection con)throws Exception{
if(con!=null){
con.close();
}
}
public static void main(String[] args) {
DbUtil dbUtil=new DbUtil();
try {
dbUtil.getCon();
System.out.println("数据库连接成功!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("数据库连接失败");
}
}
}
package com.artisan.dao;
import java.sql.Connection;
import java.sql.SQLException;
import com.artisan.util.DbUtil;
//创建对数据库连接对象,整个项目与数据库交互都用这个
public class BaseDao {
public Connection con = new DbUtil().getCon();
public void closeDao(){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.artisan.model;
public class Admin{
private int id;
private String name;
private String password;
private String createDate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCreateDate() {
return createDate;
}
public void setCreateDate(String createDate) {
this.createDate = createDate;
}
}
package com.artisan.dao;
import com.artisan.model.Admin;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class AdminDao extends BaseDao {
//管理员登陆
public Admin login(Admin admin){
String sql = "select * from s_admin where name=? and password=?";
Admin adminRst = null;
try {
PreparedStatement prst = con.prepareStatement(sql);//把sql语句传给数据库操作对象
prst.setString(1, admin.getName());
prst.setString(2, admin.getPassword());
ResultSet executeQuery = prst.executeQuery();
if(executeQuery.next()){
adminRst = new Admin();
adminRst.setId(executeQuery.getInt("id"));
adminRst.setName(executeQuery.getString("name"));
adminRst.setPassword(executeQuery.getString("password"));
adminRst.setCreateDate(executeQuery.getString("createDate"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return adminRst;
}
接着上面的LoginFrm
package com.artisan.view;
if("系统管理员".equals(selectedItem.getName())){
AdminDao adminDao = new AdminDao();
Admin adminTmp = new Admin();
adminTmp.setName(userName);
adminTmp.setPassword(password);
admin = adminDao.login(adminTmp);
adminDao.closeDao();
if(admin == null){
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
return;
}
JOptionPane.showMessageDialog(this, "欢迎【"+selectedItem.getName()+"】:"+admin.getName()+"登录本系统!");
this.dispose();
new MainFrm(selectedItem, admin).setVisible(true);}
七.学生信息系统主界面设计
同上,以拖拽方式完成
班级管理、教师管理、课程管理等同上
八.修改密码
package com.artisan.view;
public class EditPasswordFrm extends JInternalFrame {
contentPane.setLayout(gl_contentPane);
if("系统管理员".equals(MainFrm.userType.getName())){
Admin admin = (Admin)MainFrm.userObject;
currentUserLabel.setText("【系统管理员】" + admin.getName());
}else if("学生".equals(MainFrm.userType.getName())){
Student student = (Student)MainFrm.userObject;
currentUserLabel.setText("【学生】" + student.getName());
}else{
Teacher teacher = (Teacher)MainFrm.userObject;
currentUserLabel.setText("【学生】" + teacher.getName());
}
}
protected void submitEdit(ActionEvent e) {
// TODO Auto-generated method stub
String oldPassword = oldPasswordTextField.getText().toString();
String newPassword = newPasswordTextField.getText().toString();
String conformPassword = confirmPasswordTextField.getText().toString();
if(StringUtil.isEmpty(oldPassword)){
JOptionPane.showMessageDialog(this, "请填写旧密码!");
return;
}
if(StringUtil.isEmpty(newPassword)){
JOptionPane.showMessageDialog(this, "请填写新密码!");
return;
}
if(StringUtil.isEmpty(conformPassword)){
JOptionPane.showMessageDialog(this, "请确认新密码!");
return;
}
if(!newPassword.equals(conformPassword)){
JOptionPane.showMessageDialog(this, "两次密码输入不一致!");
return;
}
if("系统管理员".equals(MainFrm.userType.getName())){
AdminDao adminDao = new AdminDao();
Admin adminTmp = new Admin();
Admin admin = (Admin)MainFrm.userObject;
adminTmp.setName(admin.getName());
adminTmp.setId(admin.getId());
adminTmp.setPassword(oldPassword);
JOptionPane.showMessageDialog(this, adminDao.editPassword(adminTmp, newPassword));
adminDao.