实训2

一、创建项目
创建java项目:学生管理系统(MYSQL版)V1.0
在这里插入图片描述
二、在项目里面创建我们在写项目期间需要的文件所存放文件的文件夹(help,lib,images)
在这里插入图片描述
三、在lib里添加MySQL数据库驱动程序包
在这里插入图片描述
作为库添加到项目里
在这里插入图片描述
在这里插入图片描述
四、在images里添加图片
在这里插入图片描述在这里插入图片描述
五、在help里添加帮助文档
在这里插入图片描述在这里插入图片描述
六、创建实体类
在这里插入图片描述
创建学校实体类对应学校表
在这里插入图片描述
package net.zty.student;

import java.util.Date;

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

}
创建状态实体类对应状态表
在这里插入图片描述
package net.zty.student.bean;

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

}
创建学生实体类对应学生表
在这里插入图片描述
package net.zty.student.bean;

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

}
创建用户实体类对应用户表
在这里插入图片描述
package net.zty.student.bean;

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

}
七、创建数据库连接管理类
在这里插入图片描述
package net.zty.student.dbutile;

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 = "010114";

private ConnectionManager() {
}

public static Connection getConnection() {
    // 定义数据库连接
    Connection conn = null;
    try {
        // 安装数据库驱动程序
        Class.forName(DRIVER);
        // 获取数据库连接
        conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
    } 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();
            }
        } catch (SQLException e) {
            System.err.println("温馨提示:数据库连接关闭失败!");
        }
    }
}


public static void main(String[] args) {
    Connection conn = getConnection();
    if (conn != null) {
        JOptionPane.showMessageDialog(null, "恭喜,数据库连接成功!");
    } else {
        JOptionPane.showMessageDialog(null, "遗憾,数据库连接失败!");
    }
    closeConnection(conn);
}

}
运行程序,查看结果
在这里插入图片描述创建用户实体类
在这里插入图片描述
package net.zty.student.bean;

import java.sql.Timestamp;
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 Timestamp getRegisterTime() {
    return (Timestamp) 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.zty.student.test;

import net.zty.student.dbutile.ConnectionManager;
import net.zty.student.bean.User;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DisplayAllUsers {
public static void main(String[] args) {
Connection conn = ConnectionManager.getConnection();
try {
String strSQL = “select * from t_user”;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next()) {
User 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”));
System.out.println(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
}
}

按编号查找用户记录
在这里插入图片描述
package net.zty.student.test;

import net.zty.student.dbutile.ConnectionManager;

import java.sql.*;
import java.util.Scanner;

public class FindUserByld {
public static void main(String[] args) {
int id;
Scanner sc = new Scanner(System.in);

    System.out.print("输入待查用户编号:");
    id = sc.nextInt();

    Connection conn = null;
    try{
        conn = ConnectionManager.getConnection();
        String strSQL = "select * from t_user where id = ?";
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        pstmt.setInt(1,id);
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {
            System.out.println("" + rs.getInt("id"));
            System.out.println("" + rs.getString("username"));
            System.out.println("" + rs.getString("password"));
            System.out.println("" + rs.getString("telephone"));
            System.out.println("" + rs.getTimestamp("register_time"));
            }else {
            System.out.println("查询结果:编号为【" + id + "】的用户不存在!");
        }
    }catch (SQLException throwables) {
        throwables.printStackTrace();
    }finally{
        ConnectionManager.closeConnection(conn);
    }
}

}
创建用户登录类
在这里插入图片描述
package net.zty.student.test;

import net.zty.student.dbutile.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Login {
public static void main(String[] args) {
// 声明部分
String username;
String password;
Scanner sc = new Scanner(System.in);

    // 输入部分
    System.out.print("输入用户名:");
    username = sc.next();
    System.out.print("输入密码:");
    password = sc.next();
    
    Connection conn = ConnectionManager.getConnection();
    try {
        String strSQL = "select * from t_user where username = ? and password = ?";
        PreparedStatement pstmt = conn.prepareStatement(strSQL);
        pstmt.setString(1, username);
        pstmt.setString(2, password);
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {
            System.out.println("恭喜,登录成功!");
        } else {
            System.err.println("遗憾,登录失败!");
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        ConnectionManager.closeConnection(conn);
    }
}

}
创建添加用户记录类
在这里插入图片描述
package net.zty.student.test;

import net.zty.student.dbutile.ConnectionManager;
import net.zty.student.bean.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Scanner;

public class AddUser {
public static void main(String[] args) {
String username, password, telephone;
Scanner sc = new Scanner(System.in);
System.out.print(“用户名:”);
username = sc.next();
System.out.print(“密码:”);
password = sc.next();
System.out.print(“电话:”);
telephone = sc.next();
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setTelephone(telephone);
user.setRegisterTime(new Timestamp(new Date().getTime()));
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, user.getRegisterTime());
// 7. 执行SQL,返回添加的记录数
int count = pstmt.executeUpdate();
// 8. 判断是否添加成功
if (count > 0) {
System.out.println(“恭喜,用户记录添加成功!”);
} else {
System.out.println(“遗憾,用户记录添加失败!”);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
}
}
创建更新用户记录类
在这里插入图片描述
package net.zty.student.test;

import net.zty.student.dbutile.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class UpdateUser {
public static void main(String[] args) {
int id;
String username, password;
Scanner sc = new Scanner(System.in);
System.out.print(“待编辑记录的id:”);
id = sc.nextInt();
System.out.print(“新用户名:”);
username = sc.next();
System.out.print(“新密码:”);
password = sc.next();
Connection conn = ConnectionManager.getConnection();
String strSQL = “update t_user set username = ?, password = ? where id = ?”;
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, username);
pstmt.setString(2, password);
pstmt.setInt(3, id);
int count = pstmt.executeUpdate();
if (count > 0) {
System.out.println(“恭喜,用户记录更新成功!”);
} else {
System.out.println(“遗憾,用户记录更新失败!”);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
}
}
创建删除用户记录类在这里插入图片描述
package net.zty.student.test;

import net.zty.student.dbutile.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class DeleUserByld {
public static void main(String[] args) {
int id;
Scanner sc = new Scanner(System.in);
System.out.print(“待删记录的id:”);
id = sc.nextInt();
Connection conn = ConnectionManager.getConnection();
String strSQL = “delete from t_user where id = ?”;
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setInt(1, id);
int count = pstmt.executeUpdate();
if (count > 0) {
System.out.println(“恭喜,用户记录删除成功!”);
} else {
System.out.println(“遗憾,用户记录删除失败!”);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值