实训第一,二天:Java实训项目:GUI学生信息管理系统

1.首先,老师喊我用脑图写了一个学生信息管理系统的功能结构图如下:实训第一,二天:Java实训项目:GUI学生信息管理系统_第1张图片
【注意:
1.把脑图里的字要竖着放用:shift+enter

2.在MySQL中创建数据库及表
实训第一,二天:Java实训项目:GUI学生信息管理系统_第2张图片
导入了数据库脚本(泸职院的信息

3.创建学生管理系统
@1:在项目里创建文件夹help、images、lib
实训第一,二天:Java实训项目:GUI学生信息管理系统_第3张图片
@2:在lib里添加连接MySQL数据库的jar包(老师给我们包,直接复制进来
实训第一,二天:Java实训项目:GUI学生信息管理系统_第4张图片
然后作为库添加到项目里(Add as Library…
@3:在images里添加图片
实训第一,二天:Java实训项目:GUI学生信息管理系统_第5张图片
@4:在help里添加帮助文档(老师给我们的在群里
@5:实体类
实训第一,二天:Java实训项目:GUI学生信息管理系统_第6张图片
1)学校实体College
package net.zzm.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.zzm.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.zzm.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.zzm.student.bean;
/**

  • 功能:User实体类
  • 作者:张正敏
  • 日期:2019年6月17日
    */
    import java.util.Date;

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 +
            '}';
}

}
【注意:
1.4个实体里面的内容MySQL中也有
2.在写的过程中注意细节
3.把私有方法private写出来了过后点用alt+insert(可在code里面用鼠标点)【get 和set

4.数据库连接管理类ConnectionManager
实训第一,二天:Java实训项目:GUI学生信息管理系统_第7张图片package net.zzm.student.dbutil;

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

/**

  • 功能:数据库连接管理类
  • 作者:张正敏
  • 日期:2019年6月17日
    */

public class ConnectionManager {
/**
* 数据库驱动程序
/
private static final String DRIVER = “com.mysql.jdbc.Driver”;
/
*
* 数据库统一资源标识符
/
private static final String URL = “jdbc:mysql://localhost:3306/stu”;
/
*
* 数据库用户名
/
private static final String USERNAME = “root”;
/
*
* 数据库密码
*/
private static final String PASSWORD = “888888”;

/**
 * 私有化构造方法,拒绝实例化
 */
private ConnectionManager() {
}

/**
 * 获得数据库连接
 *
 * @return 数据库连接对象
 */
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;
}

/**
 * 关闭数据库连接
 *
 * @param conn
 */
public static void closeConnection(Connection conn) {
    // 判断数据库连接是否为空
    if (conn != null) {
        // 判断数据库连接是否关闭
        try {
            if (!conn.isClosed()) {
                // 关闭数据库连接
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

/**
 * 测试数据库连接是否成功
 *
 * @param args
 */
public static void main(String[] args) {
    // 获得数据库连接
    Connection conn = getConnection();
    // 判断是否连接成功
    if (conn != null) {
        JOptionPane.showMessageDialog(null, "恭喜,数据库连接成功!");
    } else {
        JOptionPane.showMessageDialog(null, "遗憾,数据库连接失败!");
    }

    // 关闭数据库连接
    closeConnection(conn);
}

}
运行程序,测试数据库连接是否成功:
实训第一,二天:Java实训项目:GUI学生信息管理系统_第8张图片
注意:
1、连接的是自已的数据库,自已在数据库里面建的什么名字,这里面就用什么名字
2、密码:密码是自已数据库的密码我的是(888888)
3.try/catch/finally


5.数据访问接口
实训第一,二天:Java实训项目:GUI学生信息管理系统_第9张图片1)学校数据访问接口CollegeDao
package net.zzm.student.dao;

import net.zzm.student.bean.College;

public interface CollegeDao {
College findById(int id);
int update(College college);
}
2)状态数据访问接口StatusDao
package net.zzm.student.dao;

import net.zzm.student.bean.Status;

public interface StatusDao {
Status findById(int id);
int update(Status status);

}
3)学生数据访问接口StudentDao
package net.zzm.student.dao;
import java.util.List;
import java.util.Vector;
import net.zzm.student.bean.Student;
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 findByClass(String clazz);
List findByDepartment(String department);
List findAll();
Vector findRowsBySex();
Vector findRowsByClass();
Vector findRowsByDepartment();

}
4)用户数据访问接口UserDao
package net.zzm.student.dao;

import java.util.List;

import net.zzm.student.bean.User;

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、
2、
3、
4、


6.数据访问接口实现类
实训第一,二天:Java实训项目:GUI学生信息管理系统_第10张图片1)学校数据访问接口实现类CollegeDaoImpl
package net.zzm.student.dao.impl;
/**

  • 功能:学校数据访问接口实现类
  • 作者:张正敏
  • 日期:2019年6月17日
    */

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import net.zzm.student.bean.College;
import net.zzm.student.dao.CollegeDao;
import net.zzm.student.dbutil.ConnectionManager;

public class CollegeDaolmpl implements CollegeDao {
@Override
public College findById(int id) {
// 声明学校对象
College college = null;

    // 1.获取数据库连接对象
    Connection conn = ConnectionManager.getConnection();
    //2. 定义SQL字符串
    String strSQL = "SELECT * FROM t_college WHERE id = ?";
    try {
        // 3.创建预备语句对象
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        // 4.设置占位符的值
        pstmt.setInt(1, id);
        // 5.执行SQL查询,返回结果集
        ResultSet rs = pstmt.executeQuery();
        // 6.判断结果集是否有记录
        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"));
        }
        //7.关闭预备语句对象
        pstmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        ConnectionManager.closeConnection(conn);
    }

    // 返回学校对象
    return college;
}

@Override
public int update(College college) {
    // 定义更新记录数
    int count = 0;

    // 1.获得数据库连接
    Connection conn = ConnectionManager.getConnection();
    // 2.定义SQL字符串
    String strSQL = "UPDATE t_college SET name = ?, president = ?, start_time = ?,"
            + " telephone = ?, email = ?, address = ?, profile = ? WHERE id = ?";
    try {
        // 3.创建预备语句对象
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        //4. 设置占位符的值
        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.getAddress());
        pstmt.setString(7, college.getProfile());
        pstmt.setInt(8, college.getId());
        // 5.执行更新操作,更新记录数
        count = pstmt.executeUpdate();
        // 6.关闭预备语句对象
        pstmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        ConnectionManager.closeConnection(conn);
    }

    // 返回更新记录数
    return count;
}

}
创建net.hw.student.test包,在里面创建测试类TestCollegeDaoImpl:
在这里插入图片描述编写单元测试testUpdate()方法:
package net.zzm.student.test;

import net.zzm.student.bean.College;
import net.zzm.student.dao.CollegeDao;
import net.zzm.student.dao.impl.CollegeDaolmpl;
import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.Test;

/**

  • 功能:测试学校数据接口实现类
  • 作者:张正敏
  • 日期:2019年6月17日
    */
    public class TestCollegeDaolmpl {
    @Before
    public void beforeTest(){
    System.out.println(“单元测试开始了”);
    }
    @Test
    public void testFindByID(){
    CollegeDao dao=new CollegeDaolmpl();
    College college=dao.findById(1);
    System.out.println(college);
    }
    @Test
    public void testUpdate(){
    CollegeDao dao=new CollegeDaolmpl();
    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(“单元测试结束了”);
    }
    }
    显示界面
    实训第一,二天:Java实训项目:GUI学生信息管理系统_第11张图片由此可见,CollegeDaoImpl通过了单元测试。
    【注意事项:1、
    2、
    3、
    4、
    5、

2)状态数据访问接口实现类StatusDaoImpl
package net.zzm.student.dao.impl;
/**
*功能:数据访问接口实现类

  • 作者:张正敏
  • 日期:2019年6月17日
    */
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;

import net.zzm.student.bean.Status;
import net.zzm.student.dao.StatusDao;
import net.zzm.student.dbutil.ConnectionManager;

public class StatusDaolmpl implements StatusDao {

@Override
public Status findById(int id) {
    // 声明状态对象
    Status status = null;

    // 获取数据库连接对象
    Connection conn = ConnectionManager.getConnection();
    // 定义SQL字符串
    String strSQL = "SELECT * FROM t_status WHERE id = ?";
    try {
        // 创建预备语句对象
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        // 设置占位符的值
        pstmt.setInt(1, id);
        // 执行SQL查询,返回结果集
        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();
    // 定义SQL字符串
    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;
}

}

创建net.hw.student.test包,在里面创建测试类TestStatusDaoImpl:
在这里插入图片描述

package net.zzm.student.test;

import net.zzm.student.bean.Status;
import net.zzm.student.dao.StatusDao;
import net.zzm.student.dao.impl.StatusDaolmpl;
import org.junit.jupiter.api.Test;

/**

  • 功能:测试状态数据访问接口实现类
  • 作者:张正敏
  • 日期:2019年6月18日
    */

public class TestStatusDaolmpl {
@Test
public void testFindByID(){
StatusDao dao=new StatusDaolmpl();
Status status=dao.findById(1);
System.out.println(status);
}
@Test
public void testUpdate(){
StatusDao dao=new StatusDaolmpl();
Status status=dao.findById(1);
status.setAuthor(“张正敏”);
status.setTelephone(“18780636650”);
status.setEmail("[email protected]");
status.setAddress(“泸职院信息工程系”);
dao.update(status);
status=dao.findById(1);
System.out.println(status);
}
}
运行testUpdate()方法,结果如下:
显示界面
实训第一,二天:Java实训项目:GUI学生信息管理系统_第12张图片
【注意事项:1、
2、
3、
4、

3)学生数据访问接口实现类StudentDaoImpl

代码:
package net.zzm.student.dao.impl;

/**

  • 功能:学生数据访问接口实现类
  • 作者:张正敏
  • 日期:2019年6月17日
    */

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import net.zzm.student.bean.Student;
import net.zzm.student.dao.StudentDao;
import net.zzm.student.dbutil.ConnectionManager;

public class StudentDaoImpl implements StudentDao {

@Override
public int deleteByClass(String clazz) {
    // 定义删除记录数
    int count = 0;

    // 1.获得数据库连接
    Connection conn = ConnectionManager.getConnection();
    // 2.定义SQL字符串
    String strSQL = "DELETE FROM t_student WHERE class = ?";
    try {
        // 创建预备语句对象
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        // 设置占位符的值
        pstmt.setString(1, clazz);
        // 执行更新操作,删除记录
        count = pstmt.executeUpdate();
        // 关闭预备语句对象
        pstmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        ConnectionManager.closeConnection(conn);
    }

    // 返回删除记录数
    return count;
}

@Override
public int deleteByDepartment(String department) {
    // 定义删除记录数
    int count = 0;

    // 获得数据库连接
    Connection conn = ConnectionManager.getConnection();
    // 定义SQL字符串
    String strSQL = "DELETE FROM t_student WHERE department = ?";
    try {
        // 创建预备语句对象
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        // 设置占位符的值
        pstmt.setString(1, department);
        // 执行更新操作,删除记录
        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();
    // 定义SQL字符串
    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 List findAll() {
    // 声明用户列表
    List students = new ArrayList();
    // 获取数据库连接对象
    Connection conn = ConnectionManager.getConnection();
    // 定义SQL字符串
    String strSQL = "SELECT * FROM t_student";
    try {
        // 创建语句对象
        Statement stmt = conn.createStatement();
        // 执行SQL,返回结果集
        ResultSet rs = stmt.executeQuery(strSQL);
        // 遍历结果集
        while (rs.next()) {
            // 创建学生实体
            Student student = new Student();
            // 设置实体属性
            student.setId(rs.getString("id"));
            student.setName(rs.getString("name"));
            student.setSex(rs.getString("sex"));
            student.setAge(rs.getInt("age"));
            student.setDepartment(rs.getString("department"));
            student.setClazz(rs.getString("class"));
            student.setTelephone(rs.getString("telephone"));
            // 将实体添加到学生列表
            students.add(student);
        }
        // 关闭结果集
        rs.close();
        // 关闭语句对象
        stmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        // 关闭数据库连接
        ConnectionManager.closeConnection(conn);
    }
    // 返回学生列表
    return students;
}

/**
 * 按班级查询学生
 */
@Override
public List findByClass(String clazz) {
    // 声明用户列表
    List students = new ArrayList();
    // 获取数据库连接对象
    Connection conn = ConnectionManager.getConnection();
    // 定义SQL字符串
    String strSQL = "SELECT * FROM t_student WHERE class LIKE ?";
    try {
        // 创建预备语句对象
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        // 设置占位符的值
        pstmt.setString(1, clazz + "%");
        // 执行SQL,返回结果集
        ResultSet rs = pstmt.executeQuery();
        // 遍历结果集
        while (rs.next()) {
            // 创建学生实体
            Student student = new Student();
            // 设置实体属性
            student.setId(rs.getString("id"));
            student.setName(rs.getString("name"));
            student.setSex(rs.getString("sex"));
            student.setAge(rs.getInt("age"));
            student.setDepartment(rs.getString("department"));
            student.setClazz(rs.getString("class"));
            student.setTelephone(rs.getString("telephone"));
            // 将实体添加到学生列表
            students.add(student);
        }
        // 关闭结果集
        rs.close();
        // 关闭语句对象
        pstmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        // 关闭数据库连接
        ConnectionManager.closeConnection(conn);
    }
    // 返回学生列表
    return students;
}

/**
 * 按系部查询学生
 */
@Override
public List findByDepartment(String department) {
    // 声明用户列表
    List students = new ArrayList();
    // 获取数据库连接对象
    Connection conn = ConnectionManager.getConnection();
    // 定义SQL字符串
    String strSQL = "SELECT * FROM t_student WHERE department LIKE ?";
    try {
        // 创建预备语句对象
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        // 设置占位符的值
        pstmt.setString(1, department + "%");
        // 执行SQL,返回结果集
        ResultSet rs = pstmt.executeQuery();
        // 遍历结果集
        while (rs.next()) {
            // 创建学生实体
            Student student = new Student();
            // 设置实体属性
            student.setId(rs.getString("id"));
            student.setName(rs.getString("name"));
            student.setSex(rs.getString("sex"));
            student.setAge(rs.getInt("age"));
            student.setDepartment(rs.getString("department"));
            student.setClazz(rs.getString("class"));
            student.setTelephone(rs.getString("telephone"));
            // 将实体添加到学生列表
            students.add(student);
        }
        // 关闭结果集
        rs.close();
        // 关闭语句对象
        pstmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        // 关闭数据库连接
        ConnectionManager.closeConnection(conn);
    }
    // 返回学生列表
    return students;
}

@Override
public Student findById(String id) {
    // 声明学生
    Student student = null;

    // 获取数据库连接对象
    Connection conn = ConnectionManager.getConnection();
    // 定义SQL字符串
    String strSQL = "SELECT * FROM t_student WHERE id = ?";
    try {
        // 创建预备语句对象
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        // 设置占位符的值
        pstmt.setString(1, id);
        // 执行SQL查询,返回结果集
        ResultSet rs = pstmt.executeQuery();
        // 判断结果集是否有记录
        if (rs.next()) {
            // 创建学生实体
            student = new Student();
            // 设置实体属性
            student.setId(rs.getString("id"));
            student.setName(rs.getString("name"));
            student.setSex(rs.getString("sex"));
            student.setAge(rs.getInt("age"));
            student.setDepartment(rs.getString("department"));
            student.setClazz(rs.getString("class"));
            student.setTelephone(rs.getString("telephone"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        ConnectionManager.closeConnection(conn);
    }

    // 返回学生
    return student;
}

@Override
public List findByName(String name) {
    // 声明用户列表
    List students = new ArrayList();
    // 获取数据库连接对象
    Connection conn = ConnectionManager.getConnection();
    // 定义SQL字符串
    String strSQL = "SELECT * FROM t_student WHERE name LIKE ?";
    try {
        // 创建预备语句对象
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        // 设置占位符的值
        pstmt.setString(1, name + "%");
        // 执行SQL,返回结果集
        ResultSet rs = pstmt.executeQuery();
        // 遍历结果集
        while (rs.next()) {
            // 创建学生实体
            Student student = new Student();
            // 设置实体属性
            student.setId(rs.getString("id"));
            student.setName(rs.getString("name"));
            student.setSex(rs.getString("sex"));
            student.setAge(rs.getInt("age"));
            student.setDepartment(rs.getString("department"));
            student.setClazz(rs.getString("class"));
            student.setTelephone(rs.getString("telephone"));
            // 将实体添加到学生列表
            students.add(student);
        }
        // 关闭结果集
        rs.close();
        // 关闭语句对象
        pstmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        // 关闭数据库连接
        ConnectionManager.closeConnection(conn);
    }
    // 返回学生列表
    return students;
}

@Override
public Vector findRowsByClass() {
    // 定义向量
    Vector rows = new Vector();
    // 获取数据库连接对象
    Connection conn = ConnectionManager.getConnection();
    // 定义SQL字符串
    String strSQL = "SELECT class AS '班级', count(*) AS '人数'" + " FROM t_student GROUP BY class ORDER BY class DESC";
    try {
        // 创建语句对象
        Statement stmt = conn.createStatement();
        // 执行SQL,返回结果集
        ResultSet rs = stmt.executeQuery(strSQL);
        // 遍历结果集
        while (rs.next()) {
            Vector currentRow = new Vector();
            currentRow.addElement(rs.getString("班级"));
            currentRow.addElement(rs.getInt("人数") + "");
            rows.addElement(currentRow);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return rows;
}

@Override
public Vector findRowsByDepartment() {
    // 定义向量
    Vector rows = new Vector();
    // 获取数据库连接对象
    Connection conn = ConnectionManager.getConnection();
    // 定义SQL字符串
    String strSQL = "SELECT department AS '系部', count(*) AS '人数'"
            + " FROM t_student GROUP BY department ORDER BY department DESC";
    try {
        // 创建语句对象
        Statement stmt = conn.createStatement();
        // 执行SQL,返回结果集
        ResultSet rs = stmt.executeQuery(strSQL);
        // 遍历结果集
        while (rs.next()) {
            Vector currentRow = new Vector();
            currentRow.addElement(rs.getString("系部"));
            currentRow.addElement(rs.getInt("人数") + "");
            rows.addElement(currentRow);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return rows;
}

@Override
public Vector findRowsBySex() {
    // 定义向量
    Vector rows = new Vector();
    // 获取数据库连接对象
    Connection conn = ConnectionManager.getConnection();
    // 定义SQL字符串
    String strSQL = "SELECT sex AS '性别', count(*) AS '人数'" + " FROM t_student GROUP BY sex ORDER BY sex DESC";
    try {
        // 创建语句对象
        Statement stmt = conn.createStatement();
        // 执行SQL,返回结果集
        ResultSet rs = stmt.executeQuery(strSQL);
        // 遍历结果集
        while (rs.next()) {
            Vector currentRow = new Vector();
            currentRow.addElement(rs.getString("性别"));
            currentRow.addElement(rs.getInt("人数") + "");
            rows.addElement(currentRow);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return rows;
}

@Override
public int insert(Student student) {
    // 定义插入记录数
    int count = 0;

    // 获得数据库连接
    Connection conn = ConnectionManager.getConnection();
    // 定义SQL字符串
    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 update(Student student) {
    // 定义更新记录数
    int count = 0;

    // 获得数据库连接
    Connection conn = ConnectionManager.getConnection();
    // 定义SQL字符串
    String strSQL = "UPDATE t_student SET name = ?, sex = ?, age = ?,"
            + " department = ?, class = ?, telephone = ? WHERE id = ?";
    try {
        // 创建预备语句对象
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        // 设置占位符的值
        pstmt.setString(1, student.getName());
        pstmt.setString(2, student.getSex());
        pstmt.setInt(3, student.getAge());
        pstmt.setString(4, student.getDepartment());
        pstmt.setString(5, student.getClazz());
        pstmt.setString(6, student.getTelephone());
        pstmt.setString(7, student.getId());
        // 执行更新操作,更新记录
        count = pstmt.executeUpdate();
        // 关闭预备语句对象
        pstmt.close();
    } catch (SQLException e) {
        System.err.println(e.getMessage());
    } finally {
        ConnectionManager.closeConnection(conn);
    }

    // 返回更新记录数
    return count;
}

}
分步骤写:
1.获得数据库连接
2.定义SQL语句
3.创建预备语句对象
4.设置占位符的值
5.执行更新语句,删除记录
6.关闭预备语句对象

创建TestStudentDaolmpl
编写测试TestStudentDaolmpl
package net.zzm.student.test;
/**

  • 功能:创建测试TestStudentDaolmpl
  • 作者:张正敏
  • 日期:2019年6月18日
    */

import net.zzm.student.bean.Student;
import net.zzm.student.dao.StudentDao;
import net.zzm.student.dao.impl.StudentDaoImpl;
import org.junit.jupiter.api.Test;

public class TestStudentDaolmpl {
@Test
public void testInsert(){
Student student=new Student();
student.setId(“18242026”);
student.setName(“张正敏”);
student.setSex(“女”);
student.setAge(19);
student.setDepartment(“信息工程学院”);
student.setClazz(“18大数据1班”);
student.setTelephone(“18780636650”);

    StudentDao dao=new StudentDaoImpl();
    int count=dao.insert(student);
    if (count>0){
        System.out.println("恭喜,学生记录插入成功");
    }else {
        System.out.println("遗憾,学生记录插入失败");
    }
}

}
*1.*测试teststudentdaolmpl结果为
实训第一,二天:Java实训项目:GUI学生信息管理系统_第13张图片
然后再MySQL里面去看看有没有插入成功(检验)
2.测试用班级删除该学生的信息
@Test
public void testDeleteByClass(){
StudentDao dao=new StudentDaoImpl();
String clazz = “18大数据1班”;
int count=dao.deleteByClass(clazz);
if (count >0){
System.out.println(“恭喜,班级删除成功”);
}else {
System.out.println(“遗憾,班级删除失败”);
}
}
测试结果实训第一,二天:Java实训项目:GUI学生信息管理系统_第14张图片
3.测试用系部删除学生信息
@Test
public void testDeleteByDepartment(){
StudentDao dao=new StudentDaoImpl();
String department=“信息工程系”;
int count=dao.deleteByDepartment(department);
if (count>0){
System.out.println(“恭喜,系部删除成功”);
}else {
System.out.println(“遗憾,系部删除失败”);
}
}
测试结果
实训第一,二天:Java实训项目:GUI学生信息管理系统_第15张图片
*4.*测试用ID号删除学生信息
@Test
public void testDeleteById(){
StudentDao dao=new StudentDaoImpl();
String id=“11080308”;
int count=dao.deleteById(id);
if(count>0){
System.out.println(“恭喜,学生删除成功”);
}else {
System.out.println(“遗憾,学生删除失败”);
}
}
测试结果
实训第一,二天:Java实训项目:GUI学生信息管理系统_第16张图片

【】

4)用户数据访问接口实现类UserDaoImpl
package net.zzm.student.dao.impl;
/**
*功能:用户数据访问接口实现类

  • 作者:张正敏
  • 日期:2019年6月17日
    */
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Timestamp;
    import java.util.List;

import net.zzm.student.bean.User;
import net.zzm.student.dao.UserDao;
import net.zzm.student.dbutil.ConnectionManager;

public class UserDaolmpl implements UserDao {

@Override
public int deleteById(int id) {
    return 0;
}

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

@Override
public User findById(int id) {
    // 声明用户
    User user = null;

    // 获取数据库连接对象
    Connection conn = ConnectionManager.getConnection();
    // 定义SQL字符串
    String strSQL = "SELECT * FROM t_user WHERE id = ?";
    try {
        // 创建预备语句对象
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        // 设置占位符的值
        pstmt.setInt(1, id);
        // 执行SQL查询,返回结果集
        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) {
    // 1.定义插入记录数
    int count = 0;

    // 2.获得数据库连接
    Connection conn = ConnectionManager.getConnection();
    //3. 定义SQL字符串
    String strSQL = "INSERT INTO t_user (username, password, telephone, register_time)"
            + " VALUES (?, ?, ?, ?)";
    try {
        //4. 创建预备语句对象
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        //5.设置占位符的值
        pstmt.setString(1, user.getUsername());
        pstmt.setString(2, user.getPassword());
        pstmt.setString(3,user.getTelephone());
        pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
        //6,。 执行更新操作,插入新记录
        count = pstmt.executeUpdate();
        // 7.关闭预备语句对象
        pstmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        ConnectionManager.closeConnection(conn);
    }

    // 返回插入记录数
    return count;
}

@Override
public User login(String username, String password) {
    // 声明用户
    User user = null;
    // 1.获取数据库连接
    Connection conn = ConnectionManager.getConnection();
    //2. 定义SQL字符串
    String strSQL = "SELECT * FROM t_user WHERE username = ? AND password = ?";
    try {
        // 3.创建预备语句对象
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        //4. 设置占位符的值
        pstmt.setString(1, username);
        pstmt.setString(2, password);
        // 5.执行查询,返回结果集
        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 update(User user) {
    // 定义更新记录数
    int count = 0;

    // 1.获得数据库连接
    Connection conn = ConnectionManager.getConnection();
    //2. 定义SQL字符串
    String strSQL = "UPDATE t_user SET username = ?, password = ?, telephone = ?,"
            + " register_time = ? WHERE id = ?";
    try {
        //3. 创建预备语句对象
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        //4. 设置占位符的值
        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());
        // 5.执行更新操作,更新记录
        count = pstmt.executeUpdate();
        //6. 关闭预备语句对象
        pstmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        ConnectionManager.closeConnection(conn);
    }

    // 返回更新记录数
    return count;
}

}

你可能感兴趣的:(学习)