您好,登錄后才能下訂單哦!
本篇內容介紹了“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怎么實現簡單學生管理系統”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。