java基于jdbc實(shí)現(xiàn)簡單學(xué)生管理系統(tǒng)
這個(gè)是java連接mysql數(shù)據(jù)庫的一個(gè)簡單學(xué)生系統(tǒng),通過jdbc連接數(shù)據(jù)庫。
工具類
JDBCuntils.
package Student; import java.io.IOException; import java.sql.*; import java.util.Properties; //數(shù)據(jù)庫的工具類 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; } //釋放的時(shí)候要從小到大釋放 //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(); } } } }
數(shù)據(jù)庫配置文件(這個(gè)是連接你自己的數(shù)據(jù)庫的信息,在包里創(chuàng)建就好)
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; /** * 添加學(xué)生數(shù)據(jù) * * @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()); //設(shè)置第一個(gè)參數(shù) ps.setString(2, student.getName()); //設(shè)置第二個(gè)參數(shù) ps.setInt(3, student.getScore()); //設(shè)置第三個(gè)參數(shù) result = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCuntils.release(null, ps, conn); //關(guān)閉連接 } return result; } public void add() { Scanner scan = new Scanner(System.in); System.out.println("請輸入學(xué)生學(xué)號"); int id = scan.nextInt(); System.out.println("請輸入學(xué)生姓名"); String name = scan.next(); System.out.println("請輸入學(xué)生成績"); 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()); //設(shè)置第一個(gè)參數(shù) ps.setString(2, student.getName()); //設(shè)置第二個(gè)參數(shù) ps.setInt(3, student.getScore()); //設(shè)置第三個(gè)參數(shù) ps.setInt(4, student.getId()); result = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCuntils.release(null, ps, conn); //關(guān)閉連接 } return result; } public void update() { Scanner scan = new Scanner(System.in); System.out.println("請輸入學(xué)生id"); int id = scan.nextInt(); System.out.println("請輸入學(xué)生姓名"); scan.nextLine(); String name = scan.nextLine(); System.out.println("請輸入學(xué)生成績"); 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("請輸入要查詢的學(xué)號"); n = scan.nextInt(); ps.setInt(1, n); ResultSet rs = ps.executeQuery(); //將sql語句傳至數(shù)據(jù)庫,返回的值為一個(gè)字符集用一個(gè)變量接收 while(rs.next()){ //next()獲取里面的內(nèi)容 System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getInt(3)); //getString(n)獲取第n列的內(nèi)容 //數(shù)據(jù)庫中的列數(shù)是從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); //設(shè)置第一個(gè)參數(shù) result = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCuntils.release(null, ps, conn); //關(guān)閉連接 } return result; } public void delete() { Scanner scan = new Scanner(System.in); System.out.println("請輸入學(xué)生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("************ 歡迎進(jìn)入學(xué)生管理系統(tǒng) *************"); Studentmannger ms = new Studentmannger(); boolean b = true; while (b) { System.out.println("你想進(jìn)行以下哪項(xiàng)操作"); System.out.println("1、添加學(xué)生 2、更新學(xué)生數(shù)據(jù) 3、學(xué)生信息查詢 4、刪除學(xué)生 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("沒有該操作選項(xiàng),請重新來過!"); main(args); break; } } } }
工程目錄:
運(yùn)行截圖:
到此這篇關(guān)于java基于jdbc實(shí)現(xiàn)簡單學(xué)生管理系統(tǒng)的文章就介紹到這了,更多相關(guān)jdbc 學(xué)生管理系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java+Mysql學(xué)生管理系統(tǒng)源碼
- java學(xué)生管理系統(tǒng)界面簡單實(shí)現(xiàn)(全)
- Java基于MySQL實(shí)現(xiàn)學(xué)生管理系統(tǒng)
- 簡單實(shí)現(xiàn)Java版學(xué)生管理系統(tǒng)
- Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)
- java實(shí)現(xiàn)學(xué)生管理系統(tǒng)(面向?qū)ο?
- java設(shè)計(jì)簡單學(xué)生管理系統(tǒng)
- Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)詳解
- java實(shí)現(xiàn)簡單學(xué)生管理系統(tǒng)項(xiàng)目
- springboot實(shí)現(xiàn)學(xué)生管理系統(tǒng)
相關(guān)文章
Java多線程編程中synchronized關(guān)鍵字的基礎(chǔ)用法講解
Java的synchronized關(guān)鍵字用于修飾線程同步,用以線程資源共享的目的等,下面就帶來簡單的Java多線程編程中synchronized關(guān)鍵字的基礎(chǔ)用法講解2016-06-06解決微服務(wù)feign調(diào)用添加token的問題
這篇文章主要介紹了解決微服務(wù)feign調(diào)用添加token的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Spring Boot日志技術(shù)logback原理及配置解析
這篇文章主要介紹了Spring Boot日志技術(shù)logback原理及用法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07SpringBoot使用PropertiesLauncher加載外部jar包
這篇文章主要介紹了SpringBoot使用PropertiesLauncher加載外部jar包,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07