javaJDBC小項(xiàng)目《學(xué)生管理系統(tǒng)》源碼帶注解
《javaJDBC小項(xiàng)目《學(xué)生管理系統(tǒng)》源碼帶注解》由會(huì)員分享,可在線閱讀,更多相關(guān)《javaJDBC小項(xiàng)目《學(xué)生管理系統(tǒng)》源碼帶注解(12頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
1、Java+javabean+JDBC學(xué)生管理系統(tǒng) 一、 項(xiàng)目結(jié)構(gòu) 本項(xiàng)目是使用javabean和jdbc做的,這個(gè)包是實(shí)體包 這個(gè)是菜單包,源碼不會(huì)再發(fā)這些,自己寫(xiě)就好了。 二、 項(xiàng)目運(yùn)行結(jié)果 三、 源碼 廢話不多說(shuō),直接上源碼: 這兩個(gè)是關(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、為止,邏輯算是完成了,接下來(lái)就是工具包,也就是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ù)庫(kù)連接 * * @return */ public Connection Getconn() { try { Class.forName(Driver); conn = DriverManager.getConnection(url, name, pwd); } catch (Cl
21、assNotFoundException e) { System.out.println("注冊(cè)驅(qū)動(dòng)失敗"); } catch (SQLException e) { System.out.println("驅(qū)動(dòng)包路徑錯(cuò)誤"); } 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ù)庫(kù) */ 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("錯(cuò)誤關(guān)閉"); } } } 至于這個(gè)類,是一些控制臺(tái)輸入信息判斷,當(dāng)然可以貼出來(lái)供大家參考~ /** * 匹配信息 * * @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("輸入錯(cuò)誤,只能輸入數(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); } /** * 匹配年級(jí) * * @return */ public String matchesGrade() { grade = input.next(); if (!(grade.equals("初級(jí)") || grade.equals("中級(jí)") || g
29、rade.equals("高級(jí)"))) { System.out.println("無(wú)此年級(jí)設(shè)置,年級(jí)只能輸入初級(jí)、中級(jí)或高級(jí),請(qǐng)重新輸入:"); this.matchesGrade(); } return grade; } /** * 匹配手機(jī)號(hào) * * @return */ public long matchesPhone() { phone = input.next(); if (!Pattern.matches("^[0-9]{11}$", phone)) { System.out.println
30、("輸入有誤,電話號(hào)碼只能是11位數(shù)字,請(qǐng)重新輸入:"); 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("郵箱格式有誤,請(qǐng)輸入正確的電子郵箱(包含@和.com)"); this.matchesEmail(); } return email; } } 好了,別的我就不說(shuō)了,怎么調(diào)用,我更就不用說(shuō)了吧? 本文為原創(chuàng)作品,轉(zhuǎn)載需注明出處
- 溫馨提示:
1: 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 人教版小學(xué)數(shù)學(xué)五年級(jí)下冊(cè)圖形的運(yùn)動(dòng)(三)-1課件
- 供應(yīng)鏈管理方法
- 2019最新青島版數(shù)學(xué)一年級(jí)上冊(cè)第三單元《走進(jìn)花果山-10以內(nèi)的加減法》(信息窗6)課件
- 第四章勞動(dòng)中的心理、生理狀態(tài)對(duì)安全的影響
- 201x九年級(jí)物理上冊(cè)-雙休作業(yè)八(新版)教科版課件
- 課件:項(xiàng)目一-電子商務(wù)物流系統(tǒng)規(guī)劃
- 【人教版】2012-2013學(xué)年九年級(jí)(全一冊(cè))數(shù)學(xué)小復(fù)習(xí):第27章相似復(fù)習(xí)課件
- 消化腺(全英文)課件
- 小學(xué)語(yǔ)文資源五年級(jí)上冊(cè)課件《落花生》課件第一課時(shí)
- 納米材料的基本效應(yīng)課件
- 分析化學(xué)概論-課件
- 沉浸式光影解決方案課件
- 第六講加固Windows操作系統(tǒng)安全ppt課件
- 民主選舉投出理性一票ppt課件
- 高二數(shù)學(xué)選修正態(tài)分布推薦實(shí)用全套PPT