天天看点

mysql练习之简易学生管理系统

student类

package com.xfb.student;

import java.sql.*;
import java.util.Scanner;


public class Student {
    private String name;
    private int age;
    private String sex;
    private int classno;
    private String addr;
    private int row;
    private String forName="com.mysql.jdbc.Driver";
    private String url="jdbc:mysql://localhost:3306/kaoshi?characterEncoding=utf8";
    private String user="root";
    private String password="admin";
    Scanner scanner=new Scanner(System.in);
    Connection conn=null;
    Statement stm=null;
    ResultSet rs=null;
    public int insertStudent() throws SQLException{
        try {
            Class.forName(forName);
            conn=DriverManager.getConnection(url, user, password);
            stm=conn.createStatement();
            System.out.println("请输入学生姓名");
            name=scanner.next();
            System.out.println("请输入学生年龄");
            age=scanner.nextInt();
            System.out.println("请输入学生性别");
            sex=scanner.next();
            System.out.println("请输入学生班级");
            classno=scanner.nextInt();
            System.out.println("请输入学生住址");
            addr=scanner.next();
            String sql="insert into tab_student(stu_name,stu_age,stu_sex,tab_classes_id,stu_addr)"
                    +" values('"+name+"','"+age+"','"+sex+"','"+classno+"','"+addr+"')";
            row=stm.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
            rs=stm.getGeneratedKeys();
            System.out.println("成功插入:"+row+"条");
            if (rs.next()) {
                return rs.getInt();//如果有返回值 在main方法里判断是否新增成功        如果没有返回值就在函数里判断
            }                       //此函数有返回值,下面的函数没有返回值
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            conn.close();
            stm.close();
            rs.close();
        }
        return -;
    }
    public void delStudent(){
        try {
            Class.forName(forName);
            conn=DriverManager.getConnection(url, user, password);
            stm=conn.createStatement();
            System.out.println("请输入要删除的学生姓名");
            name=scanner.next();
            String sql="delete from tab_student where stu_name='"+name+"'";
            int row=stm.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
            System.out.println("已删除"+row+"条");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public void updateStudent(){
        try {
            Class.forName(forName);
            conn=DriverManager.getConnection(url, user, password);
            stm=conn.createStatement();
            System.out.println("请输入修改后的学生姓名");
            name=scanner.next();
            System.out.println("请输入修改后的学生年龄");
            age=scanner.nextInt();
            System.out.println("请输入修改后的学生性别");
            sex=scanner.next();
            System.out.println("请输入修改后的学生班级");
            classno=scanner.nextInt();
            System.out.println("请输入修改后的学生住址");
            String addr=scanner.next();
            String sql="update tab_student set stu_name='"+name+"',stu_age='"+age+"',stu_sex='"+sex+"',tab_classes_id='"+classno+"',stu_addr='"+addr+"' where stu_name='"+name+"'";
            int row = stm.executeUpdate(sql,Statement.RETURN_GENERATED_KEYS);
            System.out.println("已更新"+row+"条");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public void selectStudent(){
        try {
            Class.forName(forName);
            conn=DriverManager.getConnection(url, user, password);
            System.out.println("请输入要查找的学生姓名");
            name=scanner.next();
            stm=conn.createStatement();
            String sql="select * from tab_student where stu_name='"+name+"'";
            rs=stm.executeQuery(sql);
            if(rs.next()){
                System.out.println("姓名:"+rs.getString("stu_name")+" 年龄:"+rs.getInt("stu_age")+" 性别:"+rs.getString("stu_sex")+" 班级:"+rs.getInt("tab_classes_id")+" 住址:"+rs.getString("stu_addr"));
            }else{
                System.out.println("查无此人");
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    public void selectAllStudent(){
        try {
            Class.forName(forName);
            conn=DriverManager.getConnection(url, user, password);
            stm=conn.createStatement();
            String sql="select * from tab_student";
            rs=stm.executeQuery(sql);
            while(rs.next()){
                System.out.println("姓名:"+rs.getString("stu_name")+" 年龄:"+rs.getInt("stu_age")+" 性别:"+rs.getString("stu_sex")+" 班级:"+rs.getInt("tab_classes_id")+" 住址:"+rs.getString("stu_addr"));
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
           

main函数

package com.xfb.student;

import java.sql.SQLException;
import java.util.Scanner;

public class TestStudent {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Student stu=new Student();
        Scanner scanner=new Scanner(System.in);
        System.out.println("欢迎使用学生管理系统");
        int num=;
        while(true){
            System.out.println("请输入要执行的操作:1、添加学生 2、删除学生 3、修改学生 4、查找学生 5、查看全部学生");
            num=scanner.nextInt();
            if (num==) {
                int key=stu.insertStudent();
                if (key>) {
                    System.out.println("新增成功");
                }else if( key < ){
                    System.out.println("新增失败");
                }//接收insertStudent  return过来的数字
            }else if (num==) {
                stu.delStudent();
            }else if (num==) {
                stu.updateStudent();
            }else if (num==){
                stu.selectStudent();
            }else if (num==){
                stu.selectAllStudent();
            }
            System.out.println("要继续操作吗? 是/否");
            String str=scanner.next();
            if (str.equals("否")) {
                System.out.println("谢谢使用!拜拜~");
                break;
            }
        }

    }
}
           

继续阅读