当前位置 : 主页 > 编程语言 > java >

JDBC实现学生管理系统

来源:互联网 收集:自由互联 发布时间:2021-07-07
本文实例为大家分享了JDBC实现学生管理系统的具体代码,供大家参考,具体内容如下 1、学生类 package manage; import java.util.Date; /** * @author fanxf * @since 2018/4/27 17:01 */public class Student { priv

本文实例为大家分享了JDBC实现学生管理系统的具体代码,供大家参考,具体内容如下

1、学生类

package manage;
 
import java.util.Date;
 
/**
 * @author fanxf
 * @since 2018/4/27 17:01
 */
public class Student {
 
 private int id;
 
 private int age;
 
 private String sex;
 
 private String name;
 
 private Date dateCreated;
 
 public int getId() {
  return id;
 }
 
 public void setId(int id) {
  this.id = id;
 }
 
 public int getAge() {
  return age;
 }
 
 public void setAge(int age) {
  this.age = age;
 }
 
 public String getSex() {
  return sex;
 }
 
 public void setSex(String sex) {
  this.sex = sex;
 }
 
 public String getName() {
  return name;
 }
 
 public void setName(String name) {
  this.name = name;
 }
 
 public Date getDateCreated() {
  return dateCreated;
 }
 
 public void setDateCreated(Date dateCreated) {
  this.dateCreated = dateCreated;
 }
 
 public Student() {
 }
 
 public Student(int age, String sex, String name) {
  this.age = age;
  this.sex = sex;
  this.name = name;
 }
 
 public Student(int id, int age, String sex, String name) {
  this.id = id;
  this.age = age;
  this.sex = sex;
  this.name = name;
 }
 
 @Override
 public String toString() {
  return "Student{" +
    "id=" + id +
    ", age=" + age +
    ", sex='" + sex + '\'' +
    ", name='" + name + '\'' +
    ", dateCreated=" + dateCreated +
    '}';
 }
}

2、jdbc工具类

package manage;
 
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
 
/**
 * @author fanxf
 * @since 2018/4/27 11:06
 */
//数据库的工具类
public class JdbcUtils {
 
 private static String driver = "";
 private static String url = "";
 private static String user = "";
 private static String password = "";
 
 static {
  Properties p = new Properties();
  try {
   p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
  } catch (IOException e) {
   e.printStackTrace();
  }
  driver = p.getProperty("driver");
  url = p.getProperty("url");
  user = p.getProperty("user");
  password = p.getProperty("password");
  try {
   Class.forName(driver);
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }
 }
 
 public static Connection getConnection() {
  try {
   return DriverManager.getConnection(url, user, password);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return null;
 }
 //释放的时候要从小到大释放
 //Connection -> Statement --> Resultset
 
 
 public static void release(ResultSet rs, Statement stmt, Connection conn) {
  if (rs != null) {
   try {
    rs.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  if (stmt != null) {
   try {
    stmt.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 
  if (conn != null) {
   try {
    conn.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }
}

3、代码

package manage;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
 
/**
 * @author fanxf
 * @since 2018/4/27 17:06
 */
public class ManageSystem {
 
 private static Connection conn = null;
 private static PreparedStatement ps = null;
 private static ResultSet rs = null;
 
 /**
  * 添加学生数据
  *
  * @param student
  * @return
  */
 public static int addStudent(Student student) {
  conn = JdbcUtils.getConnection();
  int result = 0;
  try {
   ps = conn.prepareStatement("INSERT INTO student (age, sex, `name`, dateCreated) VALUES (?, ?, ?, now())");
   ps.setInt(1, student.getAge()); //设置第一个参数
   ps.setString(2, student.getSex()); //设置第二个参数
   ps.setString(3, student.getName()); //设置第三个参数
   result = ps.executeUpdate();
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   JdbcUtils.release(null, ps, conn); //关闭连接
  }
  return result;
 }
 
 public void add() {
  Scanner scan = new Scanner(System.in);
  System.out.println("请输入学生年龄");
  int age = scan.nextInt();
  System.out.println("请输入学生性别");
  String sex = scan.next();
  System.out.println("请输入学生姓名");
  String name = scan.next();
  Student s = new Student(age, sex, name);
  int flag = addStudent(s);
  if (flag > 0) {
   System.out.println("添加成功");
  } else {
   System.out.println("添加失败");
  }
 }
 
 /**
  * 修改
  *
  * @param student
  * @return
  */
 public static int updateStudent(Student student) {
  conn = JdbcUtils.getConnection();
  int result = 0;
  try {
   ps = conn.prepareStatement("UPDATE student SET age = ?, sex = ?, `name` = ? WHERE id = ?");
   ps.setInt(1, student.getAge()); //设置第一个参数
   ps.setString(2, student.getSex()); //设置第二个参数
   ps.setString(3, student.getName()); //设置第三个参数
   ps.setInt(4, student.getId());
   result = ps.executeUpdate();
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   JdbcUtils.release(null, ps, conn); //关闭连接
  }
  return result;
 }
 
 public void update() {
  Scanner scan = new Scanner(System.in);
  System.out.println("请输入学生id");
  int id = scan.nextInt();
  System.out.println("请输入学生年龄");
  int age = scan.nextInt();
  System.out.println("请输入学生性别");
  String sex = scan.next();
  System.out.println("请输入学生姓名");
  String name = scan.next();
  Student s = new Student(id, age, sex, name);
  int flag = updateStudent(s);
  if (flag > 0) {
   System.out.println("更新成功");
  } else {
   System.out.println("更新失败");
  }
 }
 
 /**
  * 删除
  *
  * @param id
  * @return
  */
 public static int deleteStudent(int id) {
  conn = JdbcUtils.getConnection();
  int result = 0;
  try {
   ps = conn.prepareStatement("DELETE FROM student WHERE id = ?");
   ps.setInt(1, id); //设置第一个参数
   result = ps.executeUpdate();
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   JdbcUtils.release(null, ps, conn); //关闭连接
  }
  return result;
 }
 
 public void delete() {
  Scanner scan = new Scanner(System.in);
  System.out.println("请输入学生id");
  int id = scan.nextInt();
  int flag = deleteStudent(id);
  if (flag > 0) {
   System.out.println("删除成功");
  } else {
   System.out.println("删除失败");
  }
 }
 
 public static void main(String[] args) {
  System.out.println("************ 欢迎进入学生管理系统 *************");
  ManageSystem ms = new ManageSystem();
  boolean b = true;
  while (b) {
   System.out.println("你想进行以下哪项操作");
   System.out.println("1、添加学生 2、更新学生数据 3、学生信息查询 4、删除学生 0、退出");
   Scanner scan = new Scanner(System.in);
   int i = scan.nextInt();
   switch (i) {
    case 1:
     ms.add();
     break;
    case 2:
     ms.update();
     break;
    case 3:
     System.out.println();
     break;
    case 4:
     ms.delete();
     break;
    default:
     System.out.println("没有该操作选项,请重新来过!");
     main(args);
     break;
   }
  }
 }
}

4、properties数据库文件自己配置

数据库字段根据学生类建立!

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

网友评论