使用JDBC怎么实现一个学生管理系统
这篇文章将为大家详细讲解有关使用JDBC怎么实现一个学生管理系统,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
创新互联专业为企业提供伍家岗网站建设、伍家岗做网站、伍家岗网站设计、伍家岗网站制作等企业网站建设、网页设计与制作、伍家岗企业网站模板建站服务,十载伍家岗做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
1、学生类
package manage; import java.util.Date; 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; } } } }
关于使用JDBC怎么实现一个学生管理系统就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
网页题目:使用JDBC怎么实现一个学生管理系统
分享URL:http://scyanting.com/article/ggjhhg.html