學(xué)生信息管理系統(tǒng) java 連接數(shù)據(jù)庫_第1頁
學(xué)生信息管理系統(tǒng) java 連接數(shù)據(jù)庫_第2頁
學(xué)生信息管理系統(tǒng) java 連接數(shù)據(jù)庫_第3頁
學(xué)生信息管理系統(tǒng) java 連接數(shù)據(jù)庫_第4頁
學(xué)生信息管理系統(tǒng) java 連接數(shù)據(jù)庫_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論