您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關使用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怎么實現一個學生管理系統就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。