jdbc簡單學生管理系統
阿新 • • 發佈:2021-10-24
這個是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(); }*/ } publicstatic 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; } } } }
工程目錄:
執行截圖: