




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、 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.Date;import java.util.Scanner;public class Student public static final String DRIVER=sun.jdbc.odbc.JdbcOdbcDri
2、ver;public static final String URL=jdbc:odbc:student;public static final String USERNAME=root;/用戶名public static final String PASSWORD=root;/密碼private static String name;/當前登錄管理員的名字private static int userid;/當前登錄管理員的主鍵/獲得 jdbc 鏈接public Connection connection()try Class.forName(DRIVER);Connectioncon=Dr
3、iverManager.getConnection(URL,USERNAME,PASSWORD);return con; catch (Exception e) e.printStackTrace();return null;/管理員登陸public boolean login() throws ExceptionScanner sc = new Scanner(System.in);System.out.print(輸入用戶名:);String username = sc.next();System.out.print(輸入密碼:);String password = sc.next();/
4、查找該用戶String sql = select username,id from muser where username= + username+ and password= + password + ; Connection connection = connection();Statement statement = connection.createStatement();ResultSet rs = statement.executeQuery(sql);if (rs.next() name = rs.getString(1);userid = rs.getInt(2);retur
5、n true;return false;/添加學(xué)生/添加學(xué)生public void addstudent()System.out.print(請依次輸入學(xué)號,姓名,年齡,地址(用逗號隔開):);Scanner scanner = new Scanner(System.in);String str = scanner.next().split(,);/*begin 驗證學(xué)號是否重復(fù)*/String sql = select * from student where 1=1 and stuno=+str0;try Connection connection = connection();State
6、ment statement = connection.createStatement();ResultSet rs = statement.executeQuery(sql);if(rs.next()System.out.println(該學(xué)號已經(jīng)存在,請更換);return; catch (SQLException ex) /*end 驗證學(xué)號是否重復(fù)*/String sql1 = insert into student(stuno,stuname,stuage,stuaddress) values(?,?,?,?);Connection connection1 = connection(
7、);PreparedStatement statement1;try statement1 = connection1.prepareStatement(sql1);statement1.setString(1, str0);statement1.setString(2, str1);statement1.setString(3, str2);statement1.setString(4, str3); statement1.execute();System.out.println(添加學(xué)生成功!); catch (SQLException ex) / 查詢學(xué)生public void chec
8、kstudent(String sno) throws SQLException System.out.println(n 學(xué)生列表:);String sql = select * from student where 1=1 ;if (sno!=null & !.equals(sno) sql = sql + and stuno=+sno;System.out.println(學(xué)號+t+姓名+t+年齡+t+地址);try Connection connection = connection();Statement statement = connection.createStatement(
9、);ResultSet rs = statement.executeQuery(sql);while(rs.next()/ int theid = rs.getInt(1);String sno1 = rs.getString(2);String name1 = rs.getString(3);String age1 = rs.getString(4);String address1 = rs.getString(5);/打印出來System.out.println(sno1+t+name1+t+age1+t+address1); catch (SQLException ex) System.
10、out.println(nn);/刪除學(xué)生public void deletestudent() System.out.print(請輸入要刪除的學(xué)號:);Scanner scanner = new Scanner(System.in);String stuno = scanner.next();String sql = delete from student where stuno=+stuno;Connection connection = connection(); PreparedStatement statement;try statement = connection.prepar
11、eStatement(sql);statement.execute();System.out.println(刪除學(xué)生成功!); catch (SQLException ex) / 主函數(shù)入口public static void main(String args) throws Exception Student theLibrary = new Student();/ 登陸boolean loginflag = true;while (loginflag) if(theLibrary.login()/loginflag = false;else System.out.println(用戶名或
12、者密碼錯誤,請檢查.);/登陸成功提示System.out.println(n-你好,+ name +! 歡迎來到學(xué)生管理系統(tǒng)-);boolean flag = true;while (flag) System.out.print(n 操作說明: 1 添加學(xué)生 ,2 刪除學(xué)生 ,3 查找學(xué)生, 4 顯示所有學(xué)生 ,-1 退出系統(tǒng):);Scanner scanner = new Scanner(System.in);int input = 0;try input = scanner.nextInt(); catch (Exception e) System.out.println(-請選擇一個操作-);continue;if (input=-1) System.out.println(謝謝使用,再見!);flag = false;else /添加學(xué)生if (input=1) theLibrary.addstudent();/刪除學(xué)生else if (input=2) theLibrary.deletestudent();/查找學(xué)生else if (
溫馨提示
- 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- DeepSeek大模型賦能數(shù)字醫(yī)療規(guī)劃方案
- 人的能力與個性分析
- 憲法考研試題及答案
- 物理振動試題及答案
- 湖北省云學(xué)聯(lián)盟2024-2025學(xué)年高一下學(xué)期5月月考英語試題(含答案)
- 密封膠施工飽滿度與連續(xù)性技術(shù)專題
- 2025短期用工勞動合同模板
- 提高工程設(shè)計企業(yè)的成本控制與預(yù)算管理
- 2025標準版擔保借款合同樣式
- P-gp-inhibitor-28-生命科學(xué)試劑-MCE
- 2025年軟件設(shè)計師考試模擬題大全試題及答案
- 2025-2030年中國別墅電梯行業(yè)市場現(xiàn)狀供需分析及投資評估規(guī)劃分析研究報告
- 商會授權(quán)運營協(xié)議書
- 石膏砂漿抹灰施工工藝流程及操作要點
- 2025-2030年中國電子材料行業(yè)市場現(xiàn)狀供需分析及投資評估規(guī)劃分析研究報告
- 無人機維護與保養(yǎng)知識試題及答案
- 《珍愛生命預(yù)防溺水》主題班會 課件
- 信息科學(xué)導(dǎo)論試題及答案
- 2025年普通話水平測試考試試卷及答案
- 運維自動化流程設(shè)計-全面剖析
- 《色彩理論應(yīng)用》課件
評論
0/150
提交評論