(課程作業)java學生管理系統,單文件最簡版(附源碼)學生信息管理系統

java學生管理系統,單文件最簡版(附源碼文件)

升級終極版已經寫完了點擊查看https://blog.csdn.net/weixin_43419816/article/details/104257185
最後附有網盤鏈接(程序打包+數據庫)

只在一個主方法文件裏面完成,比較簡陋

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
 
import Util.DbUtil;
 
public class Main {
	public static void main(String[] args) {
		showMannue();
		Scanner scanner = new Scanner(System.in);
		int i = scanner.nextInt();
		
		switch (i) {
		case 1:
			System.out.println("請輸入學生學號");
			String stu_no = scanner.next();
			
			System.out.println("請輸入學生姓名");
			String stu_name = scanner.next();
			
			intPut(stu_no, stu_name);
			
			break;
		
		case 2:
			System.out.println("請輸入學生學號");
			stu_no = scanner.next();
			delete(stu_no);
			break;
			
		case 3:
			System.out.println("請輸入學生學號");
			stu_no = scanner.next();
			System.out.println("請輸入新的姓名");
			stu_name = scanner.next();
			changeName(stu_no, stu_name);
			break;
			
		case 4:
			query();
			
		}
	}
	
	public static void intPut(String stu_no, String stu_name) {
		Connection conn = DbUtil.getDBConn();
		Statement stat = null;
		String sql = "insert into student values('"+stu_no+"','"+stu_name+"' )";
		
		try {
			stat = conn.createStatement();
			int affectedRow = stat.executeUpdate(sql);
			if(affectedRow > 0) {
				System.out.println("添加成功!!!");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DbUtil.close(conn);
			
			DbUtil.close(stat);
		}
		
	}
	
	public static void delete(String stu_no) {
		Connection conn = DbUtil.getDBConn();
		Statement stat = null;
		String sql = "delete from student where stu_no = '"+stu_no+"'";
		
		try {
			stat = conn.createStatement();
			int affectedRow = stat.executeUpdate(sql);
			if(affectedRow > 0) {
				System.out.println("刪除成功!!!");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DbUtil.close(conn);
			
			DbUtil.close(stat);
		}
	}
	
	public static void changeName(String stu_no, String newName) {
		Connection conn = DbUtil.getDBConn();
		Statement stat = null;
		String sql = "update  student set stu_name = '"+newName+"' where stu_no = '"+stu_no+"'";
		
		try {
			stat = conn.createStatement();
			int affectedRow = stat.executeUpdate(sql);
			if(affectedRow > 0) {
				System.out.println("修改成功!!!");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DbUtil.close(conn);
			
			DbUtil.close(stat);
		}
	}
	
	public static void query() {
		Connection conn = DbUtil.getDBConn();
		Statement stat = null;
		ResultSet rs = null;
		String sql = "select * from student";
		
		try {
			stat = conn.createStatement();
			rs = stat.executeQuery(sql);
			
			while(rs.next()) {
				System.out.println(rs.getString(1));
				System.out.println(rs.getString(2));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DbUtil.close(conn);
			
			DbUtil.close(stat);
			
			DbUtil.close(rs);
		}
	}
	
	public static void showMannue() {
		System.out.println("請選擇要使用的功能:");
		System.out.println("1、添加學生信息");
		System.out.println("2、刪除學生信息");
		System.out.println("3、修改學生信息");
		System.out.println("4、查看學生信息");
	}
}

鏈接

鏈接:https://pan.baidu.com/s/1_8Hs6V06GdX7ia5fYV4MlQ
提取碼:7s8u

發佈了21 篇原創文章 · 獲贊 29 · 訪問量 6299
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章