這個是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. #<!-- u914Du7F6Eu521Du59CBu5316u5927u5C0F --> initialSize=6 #<!-- u914Du7F6Eu521Du59CBu5316u6700u5927u8FDEu63A5u6570 --> maxActive=20 #<!-- u914Du7F6Eu521Du59CBu5316u6700u5C0Fu8FDEu63A5u6570 --> minIdle=3 #<!-- u914Du7F6Eu83B7u53D6u8FDEu63A5u7B49u5F85u8D85u65F6u7684u65F6u95F4,1u5206u949Fu5355u4F4Du6BEBu79D2 --> 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 學生管理系統內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/wjingbo/p/15455941.html