java基于jdbc实现简单学生管理系统
目录
- 工具类
- 工程目录:
- 运行截图:
这个是java连接mysql数据库的一个简单学生系统,通过jdbc连接数据库。
工具类
JDBCuntils.
package Student; import java.io.IOException; import java.sql.*; import java.util.Properties; //数据库的工具类 public class JDBCuntils { 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(); } } } }
数据库配置文件(这个是连接你自己的数据库的信息,在包里创建就好)
db.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/db3 user=root password=1767737316. #<!-- \u914D\u7F6E\u521D\u59CB\u5316\u5927\u5C0F --> initialSize=6 #<!-- \u914D\u7F6E\u521D\u59CB\u5316\u6700\u5927\u8FDE\u63A5\u6570 --> maxActive=20 #<!-- \u914D\u7F6E\u521D\u59CB\u5316\u6700\u5C0F\u8FDE\u63A5\u6570 --> minIdle=3 #<!-- \u914D\u7F6E\u83B7\u53D6\u8FDE\u63A5\u7B49\u5F85\u8D85\u65F6\u7684\u65F6\u95F4,1\u5206\u949F\u5355\u4F4D\u6BEB\u79D2 --> maxWait=60000
Student.java
package Student; import java.util.Date; public class Student { private int id; private String name; private int score; public Student(int id, String name,int score) { this.id = id; this.name = name; this.score = score; } public Student() { } public String toString() { return "Student{" + "name='" + id + '\'' + ", age=" + name + ", sex='" + score + '\'' + '}'; } public int getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(Integer score) { this.score = score; } }
Studentmannger.java
package Student; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; /** * @author fanxf * @since 2018/4/27 17:06 */ public class Studentmannger { private static Connection conn = null; private static PreparedStatement ps = null; private static ResultSet rs = null; /** * 添加学生数据 * * @param student * @return * @throws SQLException */ public static int addStudent(Student student) { conn = JDBCuntils.getConnection(); int result = 0; try { ps = conn.prepareStatement("INSERT INTO student (id,`name`,score) VALUES (?, ?, ?)"); ps.setInt(1, student.getId()); //设置第一个参数 ps.setString(2, student.getName()); //设置第二个参数 ps.setInt(3, student.getScore()); //设置第三个参数 result = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCuntils.release(null, ps, conn); //关闭连接 } return result; } public void add() { Scanner scan = new Scanner(System.in); System.out.println("请输入学生学号"); int id = scan.nextInt(); System.out.println("请输入学生姓名"); String name = scan.next(); System.out.println("请输入学生成绩"); int score = scan.nextInt(); Student s = new Student(id, name, score); int flag = addStudent(s); if (flag > 0) { System.out.println("添加成功"); } else { System.out.println("添加失败"); } } /**1 * * 修改 * * @param student * @return */ public static int updateStudent(Student student) { conn = JDBCuntils.getConnection(); int result = 0; try { ps = conn.prepareStatement("UPDATE student SET id = ?, `name` = ?, score = ? WHERE id = ?"); ps.setInt(1, student.getId()); //设置第一个参数 ps.setString(2, student.getName()); //设置第二个参数 ps.setInt(3, student.getScore()); //设置第三个参数 ps.setInt(4, student.getId()); result = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCuntils.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("请输入学生姓名"); scan.nextLine(); String name = scan.nextLine(); System.out.println("请输入学生成绩"); int score = scan.nextInt(); Student s = new Student(id, name, score ); int flag = updateStudent(s); if (flag > 0) { System.out.println("更新成功"); } else { System.out.println("更新失败"); } } /** * 删除 * * @param id * @return * @throws SQLException */ public static void select() throws SQLException { Scanner scan = new Scanner(System.in); conn = JDBCuntils.getConnection(); int n; String sql = "select * from student where id=?"; PreparedStatement ps = conn.prepareStatement(sql); System.out.println("请输入要查询的学号"); n = scan.nextInt(); ps.setInt(1, n); ResultSet rs = ps.executeQuery(); //将sql语句传至数据库,返回的值为一个字符集用一个变量接收 while(rs.next()){ //next()获取里面的内容 System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getInt(3)); //getString(n)获取第n列的内容 //数据库中的列数是从1开始的 } } public static int deleteStudent(int id) { conn = JDBCuntils.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 { JDBCuntils.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) throws SQLException { System.out.println("************ 欢迎进入学生管理系统 *************"); Studentmannger ms = new Studentmannger(); 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: ms.select(); break; case 4: ms.delete(); break; default: System.out.println("没有该操作选项,请重新来过!"); main(args); break; } } } }
工程目录:
运行截图:
到此这篇关于java基于jdbc实现简单学生管理系统的文章就介绍到这了,更多相关jdbc 学生管理系统内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!
【本文由:香港云服务器http://www.558cloud.com提供,感谢】