實驗內容
學生信息管理系統
學生成績表Student(Sno 字符串,長度9, Sname 字符串,長度10, Class 字符串,長度10, Age 整型, Sex 字符串,長度2)
實現如下功能:
A.輸入若干個學生的信息到Student表;
B.修改指定學號學生的基本信息;
C.刪除指定學號的學生基本信息;
D.按照性別找出所有相應的學生基本信息;
E.按照年齡段找出所有相應的學生基本信息,例如[19,21]的學生;
F.按照姓氏查詢所有學生的基本信息;
G.以上功能用菜單形式實現功能選擇。
實驗環境
eclipse+MySQL8.0版本+MySQL的JDBC驅動
實驗結果
數據庫中的內容
Sno、Sname、Class、Sex是varchar類型,Age是int類型
源代碼
Student.java
package lab3;
import java.util.Scanner;
import java.sql.*;
public class Student {public static void main(String[]args)throws SQLException{String No;Connection conn = null;Scanner cin = new Scanner(System.in);try{Class.forName("com.mysql.cj.jdbc.Driver");String url = "jdbc:mysql://localhost:3306/?user=root";//換成自己的mysql用戶名String username = "root"; //換成自己的mysql用戶名,應該也是rootString password = "123456";//換成自己的mysql密碼conn = DriverManager.getConnection(url, username, password);while(true){show();No = cin.nextLine();while(!Check(No)){System.out.println("輸入數據錯誤");System.out.println("請輸入:");No = cin.nextLine();}if(No.charAt(0) == '1'){PreparedStatement prestmt = null;System.out.println("------錄入學生信息功能------");String Sno,Sname,Class,Sex;int Age;System.out.println("請輸入 學生學號");Sno = cin.nextLine();System.out.println("請輸入 學生姓名");Sname = cin.nextLine();System.out.println("請輸入 學生班級");Class = cin.nextLine();System.out.println("請輸入 學生性別");Sex = cin.nextLine();System.out.println("請輸入 學生年齡");Age = cin.nextInt(); //sql語句中stusys.student要換成自己的數據庫名.表名String sql = "INSERT INTO stusys.student VALUES(?,?,?,?,?)";prestmt = conn.prepareStatement(sql);prestmt.setString(1, Sno);prestmt.setString(2, Sname);prestmt.setString(3, Class);prestmt.setInt(4, Age);prestmt.setString(5, Sex);prestmt.executeUpdate(); prestmt.close();}else if(No.charAt(0) == '2'){PreparedStatement prestmt = null;System.out.println("------修改學生信息功能------");String Sno,Sname,Class,Sex;int Age;System.out.println("請輸入 學生學號");Sno = cin.nextLine();System.out.println("請輸入 學生姓名");Sname = cin.nextLine();System.out.println("請輸入 學生班級");Class = cin.nextLine();System.out.println("請輸入 學生性別");Sex = cin.nextLine(); System.out.println("請輸入 學生年齡"); Age = cin.nextInt();String sql = "UPDATE stusys.student "+ "SET Sname = ?,Class = ?,Age = ?,Sex = ? "+ "WHERE Sno = ?";prestmt = conn.prepareStatement(sql);prestmt.setString(1, Sname);prestmt.setString(2, Class);prestmt.setInt(3, Age);prestmt.setString(4, Sex);prestmt.setString(5, Sno);prestmt.executeUpdate(); prestmt.close();System.out.println("修改成功");}else if(No.charAt(0) == '3'){PreparedStatement prestmt = null;System.out.println("------刪除學生信息功能------");String Sno;System.out.println("請輸入 學生學號");Sno = cin.nextLine();String sql = "DELETE "+ "FROM stusys.student "+ " WHERE Sno = ?";prestmt = conn.prepareStatement(sql);prestmt.setString(1, Sno);prestmt.executeUpdate(); prestmt.close();System.out.println("刪除成功");}else if(No.charAt(0) == '4'){System.out.println("------按照性別查找學生信息功能------");String Sex;ResultSet rs = null;PreparedStatement prestmt = null;System.out.println("請輸入 性別");Sex = cin.nextLine();String sql = "select * from stusys.student where Sex = ?";prestmt = conn.prepareStatement(sql);prestmt.setString(1, Sex);rs = prestmt.executeQuery(); while(rs.next()){String Sno = rs.getString("Sno");String Sname = rs.getString("Sname");String Class = rs.getString("Class");int Age = rs.getInt("Age");System.out.println(Sno + " " + Sname + " " + Class + " " + Age + " " + Sex);}prestmt.close();rs.close();}else if(No.charAt(0) == '5'){System.out.println("------按照年齡查找學生基本信息-------");int Age1,Age2;ResultSet rs = null;PreparedStatement prestmt = null;System.out.println("輸入年齡下限");Age1 = cin.nextInt();System.out.println("輸入年齡上限");Age2 = cin.nextInt();String sql = "select * from stusys.student where Age BETWEEN ? AND ?";prestmt = conn.prepareStatement(sql);prestmt.setInt(1, Age1);prestmt.setInt(2, Age2);rs = prestmt.executeQuery();while(rs.next()){String Sno = rs.getString("Sno");String Sname = rs.getString("Sname");String Class = rs.getString("Class");int Age = rs.getInt("Age");String Sex = rs.getString("Sex");System.out.println(Sno + " " + Sname + " " + Class + " " + Age + " " + Sex);}prestmt.close();rs.close();}else if(No.charAt(0) == '6'){System.out.println("-------按照姓氏查找學生信息--------");String Xing;ResultSet rs = null;PreparedStatement prestmt = null;System.out.println("輸入需要查詢的姓氏");Xing = cin.nextLine();Xing += '%';String sql = "select * from stusys.student where Sname like ?";prestmt = conn.prepareStatement(sql);prestmt.setString(1, Xing);rs = prestmt.executeQuery();while(rs.next()){String Sno = rs.getString("Sno");String Sname = rs.getString("Sname");int Age = rs.getInt("Age");String Sex = rs.getString("Sex");String Class = rs.getString("Class");System.out.println(Sno + " " + Sname + " " + Class + " " + Age + " " + Sex);}prestmt.close();rs.close();}else if(No.charAt(0) == '7'){System.out.println("歡迎再次使用,再見");break;}}}catch(Exception e){e.printStackTrace();System.out.println("操縱數據庫時發生未知錯誤");}finally{if(conn!=null)conn.close();if(cin!=null)cin.close();}}public static void show(){System.out.println(" 歡迎來到學生管理系統");System.out.println("-----------------------------");System.out.println(" 功能列表");System.out.println(" 1.錄入學生信息");System.out.println(" 2.修改學生信");System.out.println(" 3.刪除學生信息");System.out.println(" 4.按照性別查找學生信息");System.out.println(" 5.按照年齡段查找學生信息");System.out.println(" 6.按照姓氏查找學生信息");System.out.println(" 7.退出程序");System.out.println("請輸入功能編號");}public static boolean Check(String No){ if(No.length() != 1)return false;if(No.charAt(0)<'1' || No.charAt(0) > '8')return false;return true;}
}
MySQL數據庫大家應該都會用吧,MySQL的內容這里就不寫了哈