Java 实现学生信息管理系统

时间:2023-03-09 04:22:45
Java 实现学生信息管理系统

编写一个简单的学生管理信息系统。

在oracle中设计一张学生表,以学号作为关键字。

其他学生信息有:姓名、手机号。

在进入系统时,显示如下菜单:

**************************************************

=====欢迎进入学生管理信息系统=====

1. 新增学生

2. 修改学生

3. 删除学生

4. 查询学生

5. 退出系统

请选择(1-5):

**************************************************

(1) 新增学生功能:

**************************************************

=====新增学生=====

学号:

姓名:

手机号:

保存成功!

是否继续添加(y/n):

*************************************************

(2) 修改学生功能:

**************************************************

=====修改学生=====

请输入要修改的学生学号:111

该学生信息如下:

学生学号:111

学生姓名:张三

学生手机号:13333333333

请输入新的学生信息:

学生姓名:李四

学生手机号:13333333333

保存成功!

**************************************************

(3) 删除学生功能:

**************************************************

=====删除学生=====

请输入要删除的学生学号:111

该学生信息如下:

学生学号:111

学生姓名:张三

学生手机号:13333333333

是否真的删除(y/n):y

删除成功!

**************************************************

(4) 查询学生功能

**************************************************

=====查询学生=====

学生信息如下:

学号   姓名   手机号

111 张三   13333333333

222 李四   14444444444

**************************************************

编程思路:

一、定义一个学生管理类,内有增、删、改、查4个方法。

二、在main函数中,实例化学生管理类,并根据菜单的选项分别调用4个方法。

三、使用PreparedStatement的参数赋值,示例如下:

PreparedStatementpstmt = con.prepareStatement("insert into book values(?, ?, ?)");

pstmt.setString(1, "333");

pstmt.setString(2, "王五");

pstmt.setString(3, "15555555555");

pstmt.executeUpdate();

【实现过程】

1.数据库建stu表:

Java 实现学生信息管理系统

2.设计一个学生实体类(Stu.java)

package Stu;
//实体类,封装学生类数据
/**
* @authorScatlett
*/
publicclassStu {
private String no; //学号
private String name; //姓名
private String phone; //手机号 //getter setter
public String getNo() {
returnno;
}
publicvoidsetNo(String no) {
this.no = no;
}
public String getName() {
returnname;
}
publicvoidsetName(String name) {
this.name = name;
}
public String getPhone() {
returnphone;
}
publicvoidsetPhone(String phone) {
this.phone = phone;
}
//无参构造函数
public Stu() {
super();
// TODO Auto-generated constructor stub
}
//有参构造函数
public Stu(String no, String name, String phone) {
super();
this.no = no;
this.name = name;
this.phone = phone;
}
}

3.创建封装一个(DBUtil.java),用于连接到Oracle数据库

package Stu;

importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement; publicclassDBUtil {
privatestaticfinal String DRIVER_NAME = "oracle.jdbc.driver.OracleDriver";
privatestaticfinal String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
privatestaticfinal String USER = "scott";
privatestaticfinal String PASS = "tiger"; publicstatic Connection getCon() throwsClassNotFoundException,
SQLException {
Connection con = null; Class.forName(DRIVER_NAME); con = DriverManager.getConnection(URL, USER, PASS); return con;
} publicstaticvoid close(Connection con, Statement stmt, ResultSetrs) {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

4.创建一个学生管理数据访问对象(StuDao.java)

packagestudao;
//学生管理数据访问对象StuDao
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.util.ArrayList;
importjava.util.List; importStu.DBUtil;
importStu.Stu; publicclassStuDao {
private Connection con;
privatePreparedStatementpstmt;
privateResultSetrs; //添加学生信息
publicboolean add(Stu stu) {
String sql="insert into stu(stu_no,stu_name,phone) values(?,?,?)";
try {
con=DBUtil.getCon();
pstmt=con.prepareStatement(sql);
pstmt.setString(1, stu.getNo());
pstmt.setString(2, stu.getName());
pstmt.setString(3, stu.getPhone());
pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
returnfalse;
} catch (SQLException e) {
e.printStackTrace();
returnfalse;
} finally{
DBUtil.close(con, pstmt, rs);
}
returntrue;
} //查看学生列表(1所有)
public List<Stu> list() {
List<Stu> list=newArrayList<Stu>();//是线性列表,ArrayList是 String sql="select * from stu"; try {
con=DBUtil.getCon();
pstmt=con.prepareStatement(sql);
//pstmt.executeUpdate();//用于增删改
rs=pstmt.executeQuery();//用于查询
while (rs.next()) { //Stustu=new Stu(rs.getString("stu_no"),rs.getString("stu_name"),rs.getString("phone"));
//上行写法亦可为:
Stu stu=new Stu();
stu.setNo(rs.getString("stu_no"));//取结果集里面学号这一列的值赋给
stu.setName(rs.getString("stu_name"));
stu.setPhone(rs.getString("phone")); list.add(stu);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBUtil.close(con, pstmt, rs);
}
return list;
}
//查看学生列表(2根据学生学号显示学生信息)
public Stu findSomeone(String no) {
Stu stu=null;
String sql="select * from stu where stu_no=?"; try {
con=DBUtil.getCon();
pstmt=con.prepareStatement(sql);
//pstmt.executeUpdate();//用于增删改
pstmt.setString(1,no);
rs=pstmt.executeQuery();//用于查询
while (rs.next()) { //Stustu=new Stu(rs.getString("stu_no"),rs.getString("stu_name"),rs.getString("phone"));
//上行写法亦可为:
stu=new Stu();
stu.setNo(rs.getString("stu_no"));//取结果集里面学号这一列的值赋给
stu.setName(rs.getString("stu_name"));
stu.setPhone(rs.getString("phone"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBUtil.close(con, pstmt, rs);
}
returnstu;
}
//修改学生信息
publicboolean update(Stu stu) {
String sql="update stu set stu_name=?,phone=? wherestu_no=?";
try {
con=DBUtil.getCon();
pstmt=con.prepareStatement(sql);
pstmt.setString(3, stu.getNo());
pstmt.setString(1, stu.getName());
pstmt.setString(2, stu.getPhone());
pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
returnfalse;
} catch (SQLException e) {
e.printStackTrace();
returnfalse;
} finally{
DBUtil.close(con, pstmt, rs);
}
returntrue;
} //删除学生信息
publicboolean del(String id) {
String sql="delete from stu where stu_no=?";
try {
con=DBUtil.getCon();
pstmt=con.prepareStatement(sql);
pstmt.setString(1,id); pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
returnfalse;
} catch (SQLException e) {
e.printStackTrace();
returnfalse;
} finally{
DBUtil.close(con, pstmt, rs);
}
returntrue;
}
}

5.学生信息管理系统的菜单选择实现

package Stu;
//学生信息管理系统的菜单选择
importjava.sql.Connection;
importjava.util.List;
importjava.util.Scanner; importjavax.print.DocFlavor.INPUT_STREAM; importorg.omg.CORBA.PUBLIC_MEMBER; importstudao.StuDao; publicclassStuManage {
publicvoid menu() {
//1.打印菜单
//2.输入菜单
//3.switch菜单选择
int choose;
do {
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("请选择(1-5):"); Scanner scanner=new Scanner(System.in);
choose=scanner.nextInt();
System.out.println("******************************");
switch (choose) {
case 1:
myAdd(); //菜单选择1,是新增学生
break;
case 2:
myUpdate(); //菜单选择2,是修改学生
break;
case 3:
myDel(); //菜单选择3,是删除学生
break;
case 4:
myList(); //菜单选择4,是查询学生
break;
case 5: //菜单选择5,是退出该系统
System.out.println("您选择了退出系统,确定要退出吗?(y/n)");
Scanner scan=new Scanner(System.in);
String scanExit=scan.next();
if(scanExit.equals("y")){
System.exit(-1);
System.out.println("您已成功退出系统,欢迎您再次使用!");
}
break;
default:
break;
}
} while (choose!=5);
} //新增学生信息
publicvoidmyAdd() { String continute;
do {
Scanner s=new Scanner(System.in);
String no,name,phone;
System.out.println("====新增学生====");
System.out.println("学号:");
no=s.next();
System.out.println("姓名:");
name=s.next();
System.out.println("手机号:");
phone=s.next(); Stu stu=new Stu(no,name,phone);
StuDaodao=newStuDao();
boolean ok=dao.add(stu);
if (ok) {
System.out.println("保存成功!");
}else {
System.out.println("保存失败!");
}
System.out.println("是否继续添加(y/n):");
Scanner scanner2=new Scanner(System.in);
continute=scanner2.next();
} while (continute.equals("y"));
} //删除学生信息
publicvoidmyDel(){
Scanner s=new Scanner(System.in);
String no;
System.out.println("====删除学生====");
System.out.println("请输入要删除的学生学号:");
no=s.next();
System.out.println("该学生的信息如下:"); StuDaostuDao=newStuDao();
System.out.println("学生学号:"+stuDao.findSomeone(no).getNo());
System.out.println("学生姓名:"+stuDao.findSomeone(no).getName());
System.out.println("学生手机号:"+stuDao.findSomeone(no).getPhone()); System.out.println("是否真的删除(y/n):");
Scanner scanner3=new Scanner(System.in);
String x=scanner3.next();
if (x.equals("y")) {
Stu stu=new Stu(no,null,null);
StuDaodao=newStuDao();
boolean ok=dao.del(no);
if (ok) {
System.out.println("删除成功!");
}else {
System.out.println("删除失败!");
}
}
}
//修改学生信息
publicvoidmyUpdate(){
Scanner s=new Scanner(System.in);
String no;
System.out.println("====修改学生====");
System.out.println("请输入要修改的学生学号:");
no=s.next();
System.out.println("该学生的信息如下:");
StuDaostuDao=newStuDao();
System.out.println("学生学号:"+stuDao.findSomeone(no).getNo());
System.out.println("学生姓名:"+stuDao.findSomeone(no).getName());
System.out.println("学生手机号:"+stuDao.findSomeone(no).getPhone()); System.out.println("请输入新的学生信息:");
Scanner stuUp=new Scanner(System.in);
String name,phone;
System.out.println("学生姓名:");
name=stuUp.next();
System.out.println("学生手机号:");
phone=stuUp.next();
Stu stu=new Stu(no,name,phone);
StuDaodao=newStuDao();
boolean ok=dao.update(stu);
if (ok) {
System.out.println("保存成功!");
}else {
System.out.println("保存失败!");
}
}
//查询学生信息
publicvoidmyList(){
System.out.println("************************");
System.out.println("====查询学生====");
System.out.println("该学生的信息如下:");
System.out.println("学号\t姓名\t手机号");
StuDaostuDao=newStuDao();
List<Stu> list=stuDao.list();
for (Stu stuList:list) { //循环打印出查询结果
System.out.println(stuList.getNo()+"\t"+stuList.getName()+"\t"+stuList.getPhone());
}
System.out.println("************************");
}
}

6.最后编写一个主函数测试类

package Stu;
//主函数测试类
publicclass Main {
/**
* @paramargs
*/
publicstaticvoid main(String[] args) {
StuManage s=newStuManage();
s.menu();
}
}

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

7.程序测试结果截图:

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

(1)  新增学生信息

Java 实现学生信息管理系统

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

(2)  修改学生信息

Java 实现学生信息管理系统

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

(3)  删除学生信息

Java 实现学生信息管理系统

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

(4)  查询学生信息

Java 实现学生信息管理系统

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}