學生信息管理系統 java 連接數據庫_第1頁
學生信息管理系統 java 連接數據庫_第2頁
學生信息管理系統 java 連接數據庫_第3頁
學生信息管理系統 java 連接數據庫_第4頁
學生信息管理系統 java 連接數據庫_第5頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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;/添加學生/添加學生public void addstudent()System.out.print(請依次輸入學號,姓名,年齡,地址(用逗號隔開):);Scanner scanner = new Scanner(System.in);String str = scanner.next().split(,);/*begin 驗證學號是否重復*/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(該學號已經存在,請更換);return; catch (SQLException ex) /*end 驗證學號是否重復*/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(添加學生成功!); catch (SQLException ex) / 查詢學生public void chec

8、kstudent(String sno) throws SQLException System.out.println(n 學生列表:);String sql = select * from student where 1=1 ;if (sno!=null & !.equals(sno) sql = sql + and stuno=+sno;System.out.println(學號+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);/刪除學生public void deletestudent() System.out.print(請輸入要刪除的學號:);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(刪除學生成功!); catch (SQLException ex) / 主函數入口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 +! 歡迎來到學生管理系統-);boolean flag = true;while (flag) System.out.print(n 操作說明: 1 添加學生 ,2 刪除學生 ,3 查找學生, 4 顯示所有學生 ,-1 退出系統:);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 /添加學生if (input=1) theLibrary.addstudent();/刪除學生else if (input=2) theLibrary.deletestudent();/查找學生else if (

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論