java+JDBC小項目《學(xué)生管理系統(tǒng)》源碼帶注解
《java+JDBC小項目《學(xué)生管理系統(tǒng)》源碼帶注解》由會員分享,可在線閱讀,更多相關(guān)《java+JDBC小項目《學(xué)生管理系統(tǒng)》源碼帶注解(12頁珍藏版)》請在裝配圖網(wǎng)上搜索。
1、Java+javabean+JDBC學(xué)生管理系統(tǒng) 一、 項目結(jié)構(gòu) 本項目是使用javabean和jdbc做的,這個包是實體包 這個是菜單包,源碼不會再發(fā)這些,自己寫就好了。 二、 項目運行結(jié)果 三、 源碼 廢話不多說,直接上源碼: 這兩個是關(guān)鍵源碼,是負(fù)責(zé)登錄和學(xué)生信息操作的邏輯類: public class AdminDaoImpl extends DBHelper implements AdminDao { Admin admin = null; /** * 登錄 */ @SuppressWarnings("resource"
2、) @Override public Admin login(String name) { String sql = "select * from admin where username=?"; Object[] param = {name}; Object obj = this.excute(sql, param); ResultSet rs = (ResultSet) obj; try { while (rs.next()) { admin = new Admin(); String username = rs.getSt
3、ring("username"); String password = rs.getString("password"); admin.setUsername(username); admin.setPassword(password); } } catch (SQLException e) { System.out.println("未找到此name"); } return admin; } } public class StudentDaoImpl extends DBHelper implements Stude
4、ntDao {
Student stu = null;
List
5、; stu = new Student(); try { while (rs.next()) { stu.setId(rs.getInt("id")); stu.setName(rs.getString("name")); stu.setAge(rs.getInt("age")); stu.setGender(rs.getString("gender")); stu.setGrade(rs.getString("grade")); stu.setPhone(rs.getLong("phone")); st
6、u.setEmail(rs.getString("email"));
stu.setAddress(rs.getString("address"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.closeAll();
}
return stu;
}
@SuppressWarnings("resource")
@Override
public List
7、 "select * from student";
Object obj = this.excute(sql, null);
ResultSet rs = (ResultSet) obj;
list = new ArrayList
8、)); stu.setGender(rs.getString("gender")); stu.setGrade(rs.getString("grade")); stu.setPhone(rs.getLong("phone")); stu.setEmail(rs.getString("email")); stu.setAddress(rs.getString("address")); list.add(stu); } } catch (SQLException e) { e.printStackTrace();
9、 } finally { this.closeAll(); } return list; } @SuppressWarnings("resource") @Override public String getNameById(int id) { String name = null; String sql = "select name from student where id=?"; Object[] param = { id }; Object obj = this.excute(sql, param); Result
10、Set rs = (ResultSet) obj; try { while (rs.next()) { name = rs.getString("name"); } } catch (SQLException e) { e.printStackTrace(); } finally { this.closeAll(); } return name; } @SuppressWarnings("resource") @Override public int getidByIntput(int id) {
11、 int num = 0; String sql = "select id from student where id=?"; Object[] param = { id }; Object obj = this.excute(sql, param); ResultSet rs = (ResultSet) obj; try { while (rs.next()) { num = rs.getInt("id"); } } catch (SQLException e) { e.printStackTrace(); } fi
12、nally { this.closeAll(); } return num; } @Override public boolean addStudent(Object[] param) { boolean b = false; String sql = "insert into student values(?,?,?,?,?,?,?,?)"; Object obj = this.excute(sql, param); b = (boolean) obj; return b; } @Override public
13、 boolean removeStuById(int id) { boolean b = false; String sql = "delete from student where id=?"; Object[] param = { id }; Object obj = this.excute(sql, param); b = (boolean) obj; return b; } @Override public boolean modifyAllStuById(Student stu) { boolean b = false;
14、 String sql = "update student set age = ?,grade=?,address=?,phone=?,email=? where id = ?"; Object[] param = { stu.getAge(), stu.getGrade(), stu.getAddress(), stu.getPhone(), stu.getEmail(), stu.getId() }; Object obj = this.excute(sql, param); b = (boolean) obj; return b; } @Ov
15、erride public boolean modifyPartStuById(Student stu, String attr) { boolean b = false; if (attr.equals("age")) { String sql = "update student set age =? where id=?"; Object[] param = { stu.getAge(), stu.getId() }; Object obj = this.excute(sql, param); b = (boolean) obj; }
16、else if (attr.equals("grade")) { String sql = "update student set grade =? where id=?"; Object[] param = { stu.getGrade(), stu.getId() }; Object obj = this.excute(sql, param); b = (boolean) obj; } else if (attr.equals("address")) { String sql = "update student set address =? w
17、here id=?"; Object[] param = { stu.getAddress(), stu.getId() }; Object obj = this.excute(sql, param); b = (boolean) obj; } else if (attr.equals("phone")) { String sql = "update student set phone =? where id=?"; Object[] param = { stu.getPhone(), stu.getId() }; Object obj =
18、 this.excute(sql, param); b = (boolean) obj; } else if (attr.equals("email")) { String sql = "update student set email =? where id=?"; Object[] param = { stu.getEmail(), stu.getId() }; Object obj = this.excute(sql, param); b = (boolean) obj; } return b; } } 好吧,到此
19、為止,邏輯算是完成了,接下來就是工具包,也就是JDBC通式 public class DBHelper { private static final String url = "jdbc:mysql://localhost:3306/sms?characterEncoding=utf-8"; private static final String Driver = "com.mysql.jdbc.Driver"; private static final String name = "root"; private static final String pwd = "sa
20、123456"; private Connection conn = null; private PreparedStatement pstmt = null; private ResultSet rs = null; /** * 創(chuàng)建數(shù)據(jù)庫連接 * * @return */ public Connection Getconn() { try { Class.forName(Driver); conn = DriverManager.getConnection(url, name, pwd); } catch (Cl
21、assNotFoundException e) { System.out.println("注冊驅(qū)動失敗"); } catch (SQLException e) { System.out.println("驅(qū)動包路徑錯誤"); } return conn; } public Object excute(String sql, Object[] param) { int a = 0; Object o = null; this.Getconn(); try { pstmt = conn.prepareStatement
22、(sql); if (param != null) { for (int i = 0; i < param.length; i++) { pstmt.setObject(i + 1, param[i]); } } boolean b = pstmt.execute(); if (b) { rs = pstmt.getResultSet(); o = rs; } else { a = pstmt.getUpdateCount(); if (a > 0) { o = true
23、; } else { o = false; } closeAll(); } } catch (SQLException e) { e.printStackTrace(); } return o; } /** * 關(guān)閉數(shù)據(jù)庫 */ public void closeAll() { try { if (rs != null) { rs.close(); } if (pstmt != null) { pstmt.close(); }
24、 if (conn != null) { conn.close(); } } catch (SQLException e) { System.out.println("錯誤關(guān)閉"); } } } 至于這個類,是一些控制臺輸入信息判斷,當(dāng)然可以貼出來供大家參考~ /** * 匹配信息 * * @author Administrator * */ public class Matches { Scanner input = new Scanner(System.in); static String id =
25、null; static String gender = null; static String age = null; static String grade = null; static String phone = null; static String email = null; /** * 匹配id * * @return */ public String matchesId() { id = input.next(); if (Pattern.matches("^[0-9]{1,}$", id)) {
26、} else { System.out.println("輸入錯誤,只能輸入數(shù)字:"); this.matchesId(); } return id; } /** * 匹配性別 * * @return */ public String matchesGender() { gender = input.next(); if (!(gender.equals("男") || gender.equals("女"))) { System.out.println("性別只能是男或者女:"); this.ma
27、tchesGender(); } return gender; } /** * 匹配年齡 * * @return */ public int matchesAge() { age = input.next(); if (!Pattern.matches("^[0-9]{1,}$", age)) { System.out.println("以上輸入不合法,只能輸入1-120之內(nèi)的數(shù)字:"); this.matchesAge(); } else if (Integer.valueOf(age) < 1 || I
28、nteger.valueOf(age) > 120) { System.out.println("以上輸入不合法,只能輸入1-120之內(nèi)的數(shù)字:"); this.matchesAge(); } return Integer.parseInt(age); } /** * 匹配年級 * * @return */ public String matchesGrade() { grade = input.next(); if (!(grade.equals("初級") || grade.equals("中級") || g
29、rade.equals("高級"))) { System.out.println("無此年級設(shè)置,年級只能輸入初級、中級或高級,請重新輸入:"); this.matchesGrade(); } return grade; } /** * 匹配手機號 * * @return */ public long matchesPhone() { phone = input.next(); if (!Pattern.matches("^[0-9]{11}$", phone)) { System.out.println
30、("輸入有誤,電話號碼只能是11位數(shù)字,請重新輸入:"); this.matchesPhone(); } return Long.parseLong(phone); } /** * 匹配email * * @return */ public String matchesEmail() { email = input.next(); if (!Pattern.matches("^[0-9a-zA-Z]+@[0-9a-zA-Z]+.[0-9a-zA-Z]+$", email)) { System.out.println("郵箱格式有誤,請輸入正確的電子郵箱(包含@和.com)"); this.matchesEmail(); } return email; } } 好了,別的我就不說了,怎么調(diào)用,我更就不用說了吧? 本文為原創(chuàng)作品,轉(zhuǎn)載需注明出處
- 溫馨提示:
1: 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 建筑施工重大危險源安全管理制度
- 安全培訓(xùn)資料:典型建筑火災(zāi)的防治基本原則與救援技術(shù)
- 企業(yè)雙重預(yù)防體系應(yīng)知應(yīng)會知識問答
- 8 各種煤礦安全考試試題
- 9 危險化學(xué)品經(jīng)營單位安全生產(chǎn)管理人員模擬考試題庫試卷附答案
- 加壓過濾機司機技術(shù)操作規(guī)程
- 樹脂砂混砂工藝知識總結(jié)
- XXXXX現(xiàn)場安全應(yīng)急處置預(yù)案
- 某公司消防安全檢查制度總結(jié)
- 1 煤礦安全檢查工(中級)職業(yè)技能理論知識考核試題含答案
- 4.燃?xì)獍踩a(chǎn)企業(yè)主要負(fù)責(zé)人模擬考試題庫試卷含答案
- 工段(班組)級安全檢查表
- D 氯化工藝作業(yè)模擬考試題庫試卷含答案-4
- 建筑起重司索信號工安全操作要點
- 實驗室計量常見的30個問問答題含解析