评论

收藏

[Java] JDBC实现学生管理系统

编程语言 编程语言 发布于:2021-09-18 17:50 | 阅读数:514 | 评论:0

这篇文章主要为大家详细介绍了JDBC实现学生管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了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数据库文件自己配置
数据库字段根据学生类建立!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持CodeAE代码之家
原文链接:https://blog.csdn.net/feidao0/article/details/81535758

关注下面的标签,发现更多相似文章