Java学生信息管理系统实训第二天总结

实训开始第一天我就请假了,才上了一节课,做了下面这个结构图。后来不仅彩排浪费了很多时间,就连学习都耽误了,真的是心累啊。
在这里插入图片描述
现在是实训的第二天,刚刚上课我就知道跟不上老师了,也不知道什么是能赶上老师的进度。所以,老师在讲课,我却在做前一天的内容。但是跟着老师的课件感觉还是不是很难,很多东西都是之前学过的。

一、建数据库的表,这些老师都给了代码的,运行一下加可以的。在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
二、建好数据库之后就新建了一个Java项目,名为学生信息管理系统V1.0。然后把jar包,图片和帮助文档放进去在这里插入图片描述
三、然后新建了college,status,student和user四个包。分别写入相关的实体类。
在这里插入图片描述
1、college实体类
package net.lsl.student.bean;

import java.util.Date;

/**

  • 功能:college实体类

  • 作者:李世兰

  • 日期:2019年6月17日
    */
    public class College {
    private int id;
    private String name;
    private String president;
    private Date startTime;
    private String telephone;
    private String email;
    private String address;
    private String profile;

    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 getPresident() {
    return president;
    }

    public void setPresident(String president) {
    this.president = president;
    }

    public Date getStartTime() {
    return startTime;
    }

    public void setStartTime(Date startTime) {
    this.startTime = startTime;
    }

    public String getTelephone() {
    return telephone;
    }

    public void setTelephone(String telephone) {
    this.telephone = telephone;
    }

    public String getEmail() {
    return email;
    }

    public void setEmail(String email) {
    this.email = email;
    }

    public String getAddress() {
    return address;
    }

    public void setAddress(String address) {
    this.address = address;
    }

    public String getProfile() {
    return profile;
    }

    public void setProfile(String profile) {
    this.profile = profile;
    }

    @Override
    public String toString() {
    return “College{” +
    “id=” + id +
    “, name=’” + name + ‘’’ +
    “, president=’” + president + ‘’’ +
    “, startTime=” + startTime +
    “, telephone=’” + telephone + ‘’’ +
    “, email=’” + email + ‘’’ +
    “, address=’” + address + ‘’’ +
    “, profile=’” + profile + ‘’’ +
    ‘}’;
    }
    }
    2.status实体类
    package net.lsl.student.bean;

    /**

    • 功能:Status实体类

    • 作者:李世兰

    • 日期:2019年6月17日
      /
      public class Status {
      /
      *

      • 标识符
        /
        private int id;
        /
        *
      • 校名
        /
        private String college;
        /
        *
      • 版本
        /
        private String version;
        /
        *
      • 作者
        /
        private String author;
        /
        *
      • 联系电话
        /
        private String telephone;
        /
        *
      • 通信地址
        /
        private String address;
        /
        *
      • 电子邮箱
        */
        private String email;
        public int getId() {
        return id;

      }

      public void setId(int id) {
      this.id = id;
      }

      public String getCollege() {
      return college;
      }

      public void setCollege(String college) {
      this.college = college;
      }

      public String getVersion() {
      return version;
      }

      public void setVersion(String version) {
      this.version = version;
      }

      public String getAuthor() {
      return author;
      }

      public void setAuthor(String author) {
      this.author = author;
      }

      public String getTelephone() {
      return telephone;
      }

      public void setTelephone(String telephone) {
      this.telephone = telephone;
      }

      public String getAddress() {
      return address;
      }

      public void setAddress(String address) {
      this.address = address;
      }

      public String getEmail() {
      return email;
      }

      public void setEmail(String email) {
      this.email = email;
      }

      @Override

      public String toString() {
      return “Status{” +
      “id=” + id +
      “, college=’” + college + ‘’’ +
      “, version=’” + version + ‘’’ +
      “, author=’” + author + ‘’’ +
      “, telephone=’” + telephone + ‘’’ +
      “, address=’” + address + ‘’’ +
      “, email=’” + email + ‘’’ +
      ‘}’;
      }
      }
      3.student实体类
      package net.lsl.student.bean;
      /**

  • 功能:Student实体类

  • 作者:李世兰

  • 日期:2019年6月17日
    /
    public class Student {
    /
    *

    • 学号
      /
      private String id;
      /
      *
    • 姓名
      /
      private String name;
      /
      *
    • 性别
      /
      private String sex;
      /
      *
    • 年龄
      /
      private int age;
      /
      *
    • 系部
      /
      private String department;
      /
      *
    • 班级
      /
      private String clazz;
      /
      *
    • 联系电话
      */
      private String telephone;

    public String getId() {
    return id;
    }

    public void setId(String id) {
    this.id = id;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getSex() {
    return sex;
    }

    public void setSex(String sex) {
    this.sex = sex;
    }

    public int getAge() {
    return age;
    }

    public void setAge(int age) {
    this.age = age;
    }

    public String getDepartment() {
    return department;
    }

    public void setDepartment(String department) {
    this.department = department;
    }

    public String getClazz() {
    return clazz;
    }

    public void setClazz(String clazz) {
    this.clazz = clazz;
    }

    public String getTelephone() {
    return telephone;
    }

    public void setTelephone(String telephone) {
    this.telephone = telephone;
    }

    @Override
    public String toString() {
    return “Student{” +
    “id=’” + id + ‘’’ +
    “, name=’” + name + ‘’’ +
    “, sex=’” + sex + ‘’’ +
    “, age=” + age +
    “, department=’” + department + ‘’’ +
    “, clazz=’” + clazz + ‘’’ +
    “, telephone=’” + telephone + ‘’’ +
    ‘}’;
    }
    }
    4.user实体类
    package net.lsl.student.bean;

import java.util.Date;

/**

  • 功能:用户实体User

  • 作者:李世兰

  • 日期:2019年6月17日
    /
    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 +
    ‘}’;
    }
    }

四、新建数据库连接管理类
在这里插入图片描述
package net.lsl.student.dbutil;

/**

  • 功能:数据库连接管理类
  • 作者:李世兰
  • 日期:2019年6月17日
    */

import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionManager {
private static final String DRIVER = “com.mysql.jdbc.Driver”;
private static final String URL = “jdbc:mysql://localhost:3306/student”;
private static final String USERNAME = “root”;
private static final String PASSWORD = “888888”;
private ConnectionManager() {
}
public static Connection getConnection() {
// 定义数据库连接
Connection conn = null;
try {
// 安装数据库驱动程序
Class.forName(DRIVER);
// 获得数据库连接
conn = DriverManager.getConnection(URL
+ “?useUnicode=true&characterEncoding=UTF8”, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
// 返回数据库连接
return conn;
}
public static void closeConnection(Connection conn) {
// 判断数据库连接是否为空
if (conn != null) {
// 判断数据库连接是否关闭
try {
if (!conn.isClosed()) {
// 关闭数据库连接
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// 获得数据库连接
Connection conn = getConnection();
// 判断是否连接成功
if (conn != null) {
JOptionPane.showMessageDialog(null, “恭喜,数据库连接成功!”);
} else {
JOptionPane.showMessageDialog(null, “遗憾,数据库连接失败!”);
}
// 关闭数据库连接
closeConnection(conn);
}
}

五、建数据访问接口
在这里插入图片描述
1、CollegeDao访问接口
package net.lsl.student.dao;

import net.lsl.student.bean.College;

/**

  • 功能:学校数据访问接口
  • 作者:李世兰
  • 日期:2019年6月17日
    */
    public interface CollegeDao {
    College findById(int id);
    int update(College college);
    }

2、StatusDao访问接口
package net.lsl.student.dao;

import net.lsl.student.bean.Status;

/**

  • 功能:状态数据访问接口
  • 作者:李世兰
  • 日期:2019年6月18日
    */
    public interface StatusDao {
    Status findById(int id);
    int update(Status status);
    }

3、StudentDao访问接口
package net.lsl.student.dao;

import net.lsl.student.bean.Student;

import java.util.List;
import java.util.Vector;

/**

  • 功能:学生数据访问接口
  • 作者:李世兰
  • 日期:2019年6月17日
    */
    public interface StudentDao {
    int insert(Student student);
    int deleteById(String id);
    int deleteByClass(String clazz);
    int deleteByDepartment(String department);
    int update(Student student);
    Student findById(String id);
    List findByName(String name);
    List finfByClass(String clazz);
    List findByDepartment(String department);
    List findAll();
    Vector findRowsBySex();
    Vector findRowsByClass();
    Vector findRowsByDepartment();

}

4、UserDao访问接口
package net.lsl.student.dao;

import net.lsl.student.bean.User;

import java.util.List;

/**

  • 功能:用户数据访问接口
  • 作者:李世兰
  • 日期:2019年6月18日
    */
    public interface UserDao {
    int insert(User user);
    int deleteById(int id);
    int update(User user);
    User findById(int id);
    List findAll();
    User login(String username,String password);
    }

六、建数据访问接口实现类
在这里插入图片描述
1、CollegeDaoImpl访问接口实现类
public class CollegeDaoImpl implements CollegeDao {
@Override
public College findById(int id) {

    College college = null;
    Connection conn = ConnectionManager.getConnection();
    String strSQL = "select * from t_college where id = ?";
    try {
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        pstmt.setInt(1,id);
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()){
            college = new College();
            college.setId(rs.getInt("id"));
            college.setName(rs.getString("name"));
            college.setPresident(rs.getString("president"));
            college.setStartTime(rs.getDate("start_time"));
            college.setTelephone(rs.getString("telephone"));
            college.setEmail(rs.getString("email"));
            college.setAddress(rs.getString("address"));
            college.setProfile(rs.getString("profile"));
        }
        pstmt.close();
        rs.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        ConnectionManager.closeConnection(conn);
    }
    return college;
}

@Override
public int update(College college){
    int count = 0;
    Connection conn = ConnectionManager.getConnection();
    String strSQL = "update t_college set name = ?,president = ?,start_time = ?, "
            + "telephone = ?, email = ?, profile = ? where id = ?";
    try {
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        pstmt.setString(1,college.getName());
        pstmt.setString(2,college.getPresident());
        pstmt.setTimestamp(3,new Timestamp(college.getStartTime().getTime()));
        pstmt.setString(4,college.getTelephone());
        pstmt.setString(5,college.getEmail());
        pstmt.setString(6,college.getProfile());
        pstmt.setInt(7,college.getId());
        count = pstmt.executeUpdate();
        pstmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        ConnectionManager.closeConnection(conn);
    }
    return count;
}

}

2、StatusDaoImpl访问接口实现类
public class StatusDaoImpl implements StatusDao {
@Override
public Status findById(int id) {
Status status = null;
Connection conn = ConnectionManager.getConnection();
String strSQL = “select * from t_status where id = ?”;
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setInt(1,id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()){
status = new Status();
status.setId(rs.getInt(“id”));
status.setCollege(rs.getString(“college”));
status.setVersion(rs.getString(“version”));
status.setAuthor(rs.getString(“author”));
status.setTelephone(rs.getString(“telephone”));
status.setAddress(rs.getString(“address”));
status.setEmail(rs.getString(“email”));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return status;
}

@Override
public int update(Status status) {
    int count = 0;
    Connection conn = ConnectionManager.getConnection();
    String strSQL = "update t_status set college = ?,version = ?,author = ?,telephone = ?,address = ?,email = ? where  id = ?";
    try {
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        pstmt.setString(1,status.getCollege());
        pstmt.setString(2,status.getVersion());
        pstmt.setString(3,status.getAuthor());
        pstmt.setString(4,status.getTelephone());
        pstmt.setString(5,status.getAddress());
        pstmt.setString(6,status.getEmail());
        pstmt.setInt(7,status.getId());
        count = pstmt.executeUpdate();
        pstmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        ConnectionManager.closeConnection(conn);
    }
    return count;
}

}

3、StudentDaoImpl访问接口实现类
public class StudentDaoImpl implements StudentDao {
@Override
public int insert(Student student) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = “insert into t_student (id ,name, sex, age, department, class, telephone)”
+ “values (?,?, ?, ?, ?, ?, ?)”;
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1,student.getId());
pstmt.setString(2, student.getName());
pstmt.setString(3, student.getSex());
pstmt.setInt(4, student.getAge());
pstmt.setString(5, student.getDepartment());
pstmt.setString(6, student.getClazz());
pstmt.setString(7, student.getTelephone());
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}

@Override
public int deleteById(String id) {
    int count = 0;
    Connection conn = ConnectionManager.getConnection();
    String strSQL = "delete from t_student where id = ?";
    try {
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        pstmt.setString(1,id);
        count = pstmt.executeUpdate();
        pstmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        ConnectionManager.closeConnection(conn);
    }
    return count;
}

@Override
public int deleteByClass(String clazz) {
    return 0;
}

@Override
public int deleteByDepartment(String department) {
    return 0;
}

@Override
public int update(Student student) {
    return 0;
}

@Override
public Student findById(String id) {
    return null;
}

@Override
public List<Student> findByName(String name) {
    return null;
}

@Override
public List<Student> finfByClass(String clazz) {
    return null;
}

@Override
public List<Student> findByDepartment(String department) {
    return null;
}

@Override
public List<Student> findAll() {
    return null;
}

@Override
public Vector findRowsBySex() {
    return null;
}

@Override
public Vector findRowsByClass() {
    return null;
}

@Override
public Vector findRowsByDepartment() {
    return null;
}

}

4、UserDaoImpl访问接口实现类
public class UserDaoImpl implements UserDao {
@Override
public int deleteById(int id) {
return 0;
}

@Override
public List<User> findAll() {
    return null;
}

@Override
public User findById(int id) {
    User user = null;
    Connection conn = ConnectionManager.getConnection();
    String strSQL = "select * from t_user where id = ?";
    try {
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        pstmt.setInt(1,id);
        ResultSet rs = pstmt.executeQuery();
        if (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) {
        e.printStackTrace();
    } finally {
        ConnectionManager.closeConnection(conn);
    }
    return user;
}

@Override
public int insert(User user) {
    int count = 0;
    Connection conn = ConnectionManager.getConnection();
    String strSQL = "insert into t_user (username,password,telephone,register_time)"
            + "values (?,?,?,?)";
    try {
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        pstmt.setString(1,user.getUsername());
        pstmt.setString(2,user.getPassword());
        pstmt.setString(3,user.getTelephone());
        pstmt.setTimestamp(4,new Timestamp(user.getRegisterTime().getTime()));
        count = pstmt.executeUpdate();
        pstmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        ConnectionManager.closeConnection(conn);
    }
    return count;
}

@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();
        if (rs.next()) {
            user = new User();
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("password"));
            user.setTelephone(rs.getString("telephone"));
            user.setRegisterTime(rs.getTimestamp("register_time"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        ConnectionManager.closeConnection(conn);
    }
    return user;
}

@Override
public int update(User user) {
    int count = 0;
    Connection conn = ConnectionManager.getConnection();
    String strSQL = "update t_user set username = ? ,password = ?,telephone = ?"
                + "register_time = ?  where id = ?";
    try {
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        pstmt.setString(1,user.getUsername());
        pstmt.setString(2,user.getPassword());
        pstmt.setString(3,user.getTelephone());
        pstmt.setTimestamp(4,new Timestamp(user.getRegisterTime().getTime()));
        pstmt.setInt(5,user.getId());
        count = pstmt.executeUpdate();
        pstmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        ConnectionManager.closeConnection(conn);
    }
    return count;
}

}

七、测试数据访问接口实现类测试
在这里插入图片描述
1、CollegeDaoImpl数据访问接口实现类测试
package net.lsl.student.test;

import net.lsl.student.bean.College;
import net.lsl.student.dao.CollegeDao;
import net.lsl.student.dao.impl.CollegeDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestCollegeDaoImpl {
@Before
public void beforeTest(){
System.out.println(“单元测试开始了”);
}
@Test
public void testFindById(){
CollegeDao dao = new CollegeDaoImpl();
College college = dao.findById(1);
System.out.println(college);
}
@Test
public void testUpdate(){
CollegeDao dao = new CollegeDaoImpl();
College college = dao.findById(1);
college.setPresident(“王洪礼”);
dao.update(college);
college = dao.findById(1);
System.out.println(college);
}
@After
public void afterTest(){
System.out.println(“单元测试结束了”);
}
}

2、StatusDaoImpl数据访问接口实现类测试
package net.lsl.student.test;
import net.lsl.student.bean.Status;
import net.lsl.student.dao.StatusDao;
import net.lsl.student.dao.impl.StatusDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestStatusDaoImpl {
@Before
public void beforeTest(){
System.out.println(“单元测试开始了”);
}
@Test
public void testFindByID(){
StatusDao dao=new StatusDaoImpl();
Status status=dao.findById(1);
System.out.println(status );

}
@Test
public void testUpdate(){
    StatusDao dao =new StatusDaoImpl();
    Status status =dao.findById(1);
    status .setAuthor("小芳") ;
    dao.update(status);
    status =dao.findById(1);
    System.out.println(status);
}
@After
public void afterTest(){

    System.out.println("单元测试结束了");
}

}

4、StudentDaoImpl数据访问接口实现类测试
package net.lsl.student.test;

import net.lsl.student.bean.Student;
import net.lsl.student.dao.StudentDao;
import net.lsl.student.dao.impl.StudentDaoImpl;
import org.junit.Test;
public class TestStudentDaoImpl {
@Test
public void testInsert(){
Student student=new Student();
student.setId(“181010024”);
student.setName(“张三”);
student.setSex(“男”);
student.setAge(20);
student.setDepartment(“信息工程系”);
student.setClazz(“18大数据班”);
student.setTelephone(“16786662556”);

    StudentDao dao=new StudentDaoImpl();
    int count=dao.insert(student);
    if(count>0){
        System.out.println("恭喜,学生记录添加成功!");
    }else{
        System.out.println("遗憾,学生记录添加失败!");
    }
}
@Test
public void testDeleteById(){
    StudentDao dao=new StudentDaoImpl();
    String id="181010024";
    int count =dao.deleteById(id);
    if(count>0){
        System.out.println("恭喜,学生记录删除成功!");
    }else{
        System.out.println("遗憾,学生记录删除失败!");
    }
}

}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值