您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“java基于jdbc怎么實(shí)現(xiàn)簡(jiǎn)單學(xué)生管理系統(tǒng)”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
工具類(lèi)
工程目錄:
運(yùn)行截圖:
這個(gè)是java連接mysql數(shù)據(jù)庫(kù)的一個(gè)簡(jiǎn)單學(xué)生系統(tǒng),通過(guò)jdbc連接數(shù)據(jù)庫(kù)。
JDBCuntils.
package Student; import java.io.IOException; import java.sql.*; import java.util.Properties; //數(shù)據(jù)庫(kù)的工具類(lèi) 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ù)庫(kù)配置文件(這個(gè)是連接你自己的數(shù)據(jù)庫(kù)的信息,在包里創(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("請(qǐng)輸入學(xué)生學(xué)號(hào)"); int id = scan.nextInt(); System.out.println("請(qǐng)輸入學(xué)生姓名"); String name = scan.next(); System.out.println("請(qǐng)輸入學(xué)生成績(jī)"); 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("請(qǐng)輸入學(xué)生id"); int id = scan.nextInt(); System.out.println("請(qǐng)輸入學(xué)生姓名"); scan.nextLine(); String name = scan.nextLine(); System.out.println("請(qǐng)輸入學(xué)生成績(jī)"); 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("請(qǐng)輸入要查詢(xún)的學(xué)號(hào)"); n = scan.nextInt(); ps.setInt(1, n); ResultSet rs = ps.executeQuery(); //將sql語(yǔ)句傳至數(shù)據(jù)庫(kù),返回的值為一個(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ù)庫(kù)中的列數(shù)是從1開(kāi)始的 } } 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("請(qǐng)輸入學(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é)生信息查詢(xún) 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("沒(méi)有該操作選項(xiàng),請(qǐng)重新來(lái)過(guò)!"); main(args); break; } } } }
“java基于jdbc怎么實(shí)現(xiàn)簡(jiǎn)單學(xué)生管理系統(tǒng)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。