以下是学生页面

首先先给上数据库

在准备准备工作

以下为代码:

package Bean;

public class Student {
private String StuID;
private String StuName;
private String sex;
private String college;
private String professionals;
private String Phone;
private String Position;
private String Mima;
private String Situation;
public String getSituation() {
return Situation;
}
public void setSituation(String situation) {
Situation = situation;
}
public Student(String stuID2, String stuName2, String sex2, String college2, String professionals2, String phone2,
String position2, String mima2) {
StuID = stuID2;
StuName = stuName2;
this.sex = sex2;
this.college = college2;
this.professionals = professionals2;
Phone = phone2;
Position = position2;
Position = position2;
Mima = mima2;
}
public Student(String stuID2, String stuName2, String sex2, String college2, String professionals2, String phone2,
String position2, String mima2, String situation2) {
StuID = stuID2;
StuName = stuName2;
this.sex = sex2;
this.college = college2;
this.professionals = professionals2;
Phone = phone2;
Position = position2;
Position = position2;
Mima = mima2;
Situation = situation2;
}
public String getStuID() {
return StuID;
}
public void setStuID(String stuID) {
StuID = stuID;
}
public String getStuName() {
return StuName;
}
public void setStuName(String stuName) {
StuName = stuName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public String getProfessionals() {
return professionals;
}
public void setProfessionals(String professionals) {
this.professionals = professionals;
}
public String getPhone() {
return Phone;
}
public void setPhone(String phone) {
Phone = phone;
}
public String getPosition() {
return Position;
}
public void setPosition(String position) {
Position = position;
}
public String getMima() {
return Mima;
}
public void setMima(String mima) {
Mima = mima;
}

}

package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import Bean.Student;
import util.Util;

public class DaoStudent {

public static int LoginStudent(String stuID, String mima) {
String sql = "select * from student where stuID ='" + stuID + "'and Mima='" + mima + "'";
System.out.println(sql);
Connection conn = Util.getConn();
Statement state = null;
ResultSet rs = null;
int flag=0;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
flag=1;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Util.close(rs, state, conn);
}
return flag;
}

public static Student ViewinformationStudent(String stuID1) {
Student stu=null;
String sql = "select * from student where stuID ='" + stuID1 + "'";//+and mima='" + mima + "'
System.out.println(sql);
Connection conn = Util.getConn();
Statement state = null;
ResultSet rs = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
System.out.println(rs);
while (rs.next()) {
String stuID = rs.getString("stuID");
String StuName = rs.getString("StuName");
String sex = rs.getString("sex");
String college = rs.getString("college");
String professionals = rs.getString("professionals");
String Phone = rs.getString("Phone");
String Position = rs.getString("Position");
String Mima = rs.getString("Mima");
stu = new Student(stuID,StuName,sex,college,professionals,Phone,Position,Mima);
//System.out.println(stu);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Util.close(rs, state, conn);
}
System.out.println(stu);
return stu;
}

public static int updataStudent(String stuID, String mima) {
String sql = "update student set Mima='" + mima + "' where stuID='" +stuID + "'";
System.out.println(sql);
Connection conn = Util.getConn();
Statement state = null;
int count=0;
try {
state = conn.createStatement();
count=state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭连接
Util.close(state, conn);
}
return count;
}

public static int Register(Student stu) {
String sql = "insert into student(StuID,StuName,sex,college,Professionals,Phone,Position,Mima,Situation) values('" + stu.getStuID() + "','" +stu.getStuName() + "','" +stu.getSex() + "','" + stu.getCollege()+ "','" + stu.getProfessionals()+ "','" +stu.getPhone()+ "','"+ stu.getPosition()+ "','" + stu.getMima() +"','"+ stu.getSituation()+"')";
System.out.println(sql);
Connection conn = Util.getConn();
Statement state = null;
int count=0;
try {
state = conn.createStatement();
count = state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭连接
Util.close(state, conn);
}
return count;
}

public static String Judge(String hao) {
String sql = "select * from student where stuID ='" + hao+ "'";
System.out.println(sql);
Connection conn = Util.getConn();
Statement state = null;
ResultSet rs = null;
String Situation="";
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
Situation = rs.getString("Situation");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Util.close(rs, state, conn);
}
return Situation;
}

}

package service;

import Bean.Student;
import dao.DaoStudent;

public class Service {

public int LoginStudent(String stuID, String Mima) {
return DaoStudent.LoginStudent(stuID,Mima);
}

public Student ViewinformationStudent(String stuID) {
return DaoStudent.ViewinformationStudent(stuID);
}

public int updataStudent(String stuID, String mima) {
if(DaoStudent.updataStudent(stuID,mima)>0)
return 1;
return 0;
}

public int Register(Student stu) {
if(DaoStudent.Register(stu)>0)
return 1;
return 0;
}

public String Judge(String hao) {
return DaoStudent.Judge(hao);
}

}

package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Bean.Student;
import service.Service;

/**
* Servlet implementation class Servlet
*/
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
Service service=new Service();
/**
* @see HttpServlet#HttpServlet()
*/
public Servlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String method = req.getParameter("method");
if ("Login".equals(method)) {
Login(req, resp);
}
if ("ViewinformationStudent".equals(method)) {
ViewinformationStudent(req, resp);
}
if ("updataStudent".equals(method)) {
updataStudent(req, resp);
}
if ("Register".equals(method)) {
Register(req, resp);
}
}

private void Register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String StuID = req.getParameter("StuID");
System.out.println(StuID);
String StuName = req.getParameter("StuName");
String sex = req.getParameter("sex");
String College = req.getParameter("College");
String Professionals = req.getParameter("Professionals");
String Phone = req.getParameter("Phone");
String Position = req.getParameter("Position");
String Mima = req.getParameter("Mima");
String Situation="未通过";
Student stu = new Student(StuID,StuName,sex,College,Professionals,Phone,Position,Mima,Situation);
if(service.Register(stu)==1)
{ req.setAttribute("message", "注册成功");
req.getRequestDispatcher("Student.jsp").forward(req,resp);

}
else
{
req.setAttribute("message", "注册失败,请重试");
req.getRequestDispatcher("Student.jsp").forward(req,resp);
}

}

private void updataStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String mima1 = req.getParameter("mima1");
String mima2 = req.getParameter("mima2");
String mima3 = req.getParameter("mima3");
HttpSession session=req.getSession();
String StuID=(String)session.getAttribute("StuID");
String Mima=(String)session.getAttribute("Mima");
if(mima1.equals(Mima))
{
if(mima2.equals(mima3))
{
if(service.updataStudent(StuID,mima2)==1)
{
req.setAttribute("message", "修改成功");
session.setAttribute("Mima",mima2);
req.getRequestDispatcher("Student.jsp").forward(req,resp);
}
}
req.setAttribute("message", "新密码不相同");
req.getRequestDispatcher("updataStudent.jsp").forward(req,resp);

}
req.setAttribute("message", "原密码错误,请重试");
req.getRequestDispatcher("updataStudent.jsp").forward(req,resp);
}

private void ViewinformationStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
HttpSession session=req.getSession();
String StuID=(String)session.getAttribute("StuID");
System.out.println(StuID);
String Mima=(String)session.getAttribute("Mima");
if(service.LoginStudent(StuID,Mima)==1)
{
Student stu=null;
//List<Beankecheng> bean = new ArrayList<Beankecheng>();
stu=service.ViewinformationStudent(StuID);
req.setAttribute("bean", stu);
req.getRequestDispatcher("ViewinformationStudent.jsp").forward(req,resp);

}
else
{
req.setAttribute("message", "查看失败,请重试");
req.getRequestDispatcher("Student.jsp").forward(req,resp);
}

}

private void Login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String zhiye = req.getParameter("zhiye");
String hao = req.getParameter("hao");
String Mima = req.getParameter("Mima");
HttpSession session=req.getSession();
if(zhiye.equals("学生"))
{ session.setAttribute("StuID",hao);
session.setAttribute("Mima",Mima);
String Situation=service.Judge(hao);
if((service.LoginStudent(hao,Mima)==1)&&(Situation.equals("通过")))
{
req.setAttribute("message", "登录成功");
req.getRequestDispatcher("Student.jsp").forward(req,resp);
}
else
{
req.setAttribute("message", "登录失败,请重新登录");
req.getRequestDispatcher("Screen.jsp").forward(req,resp);
}

}
if(zhiye.equals("专业教师"))
{
session.setAttribute("TeacherID",hao);
session.setAttribute("Mima",Mima);
}
if(zhiye.equals("专业负责人"))
{
session.setAttribute("TeacherID",hao);
session.setAttribute("Mima",Mima);
}
if(zhiye.equals("教学副院长"))
{session.setAttribute("TeacherID",hao);
session.setAttribute("Mima",Mima);

}
if(zhiye.equals("管理员"))
{

}


}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}

package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Util {

static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/qimokaoshi?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
static final String USER = "root";
static final String PASS = "123456";
public static Connection getConn () {
Connection conn = null;
try {
Class.forName(JDBC_DRIVER);
System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
//statement
public static void close (Statement state, Connection conn) {

if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close (ResultSet rs, Statement state, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}

Ok,有一篇水出来了,不对,读书人的事情怎么能叫水呢,然后Bean中我自己加了状态和密码,都是我的程序需要的,每个人思路不一样,不一定Bean一样

2021级《JAVA语言程序设计》上机考试试题2的更多相关文章

  1. Java语言程序设计-助教篇

    1. 给第一次上课(软件工程)的老师与助教 现代软件工程讲义 0 课程概述 给学生:看里面的第0个作业要求 2. 助教心得 美国视界(1):第一流的本科课堂该是什么样?(看里面的助教部分) 助教工作看 ...

  2. 全国计算机等级考试二级笔试样卷Java语言程序设计

    一.选择题((1)-(35)每小题2分,共70分) 下列各题A).B).C).D)四个选项中,只有一个选项是正确的,请将正确选项涂写在答题卡相应位置上,答在试卷上不得分. (1)下列选项中不符合良好程 ...

  3. 《JAVA语言程序设计》上课笔记

    教学目标:1.使学生了解JAVA课程的性质.定位.作用:为什么要学习JAVA?让学生知道如何学好JAVA: 教学内容: 一.        问几个问题 1.             你们到这里来干什么 ...

  4. Java语言程序设计复习提纲

     这是我在准备Java考试时整理的提纲,如果是通过搜索引擎搜索到这篇博客的师弟师妹,建议还是先参照PPT和课本,这个大纲也不是很准确,自己总结会更有收获,多去理解含义,不要死记硬背,否则遇到概念辨析题 ...

  5. 0031 Java学习笔记-梁勇著《Java语言程序设计-基础篇 第十版》英语单词

    第01章 计算机.程序和Java概述 CPU(Central Processing Unit) * 中央处理器 Control Unit * 控制单元 arithmetic/logic unit /ə ...

  6. Java语言程序设计(基础篇)第一章

    第一章 计算机.程序和Java概述 1.1 引言 什么是程序设计呢? 程序设计就是创建(或者开发)软件,软件也称为程序. 1.2 什么是计算机 计算机是存储和处理数据的电子设备,计算机包括硬件(har ...

  7. JAVA语言程序设计课程评价

    紧张的又短暂的一个学期结束了,这个学期也许将成为我人生中一个重要的转折点,作为一名半路出家的选手,在初次了解Java语言时我感到非常的迷茫与不知所措.因为之前很多同学都是通过假期时间在家自学,刚转入新 ...

  8. java语言程序设计(一)-1

    java 语言的特点是: 强类型,制定了比较多的语言规范,尽可能在编译阶段检测出更多的错误及警告. 编译和解释,首先将源代码编译成codebyte,运行时,java的运行系统装载和链接需要执行的类,并 ...

  9. IT兄弟连 Java语法教程 Java语言入门 典面试题

    1.请说明JVM.JRE和JDK是什么?它们有什么关系? JVM是Java虚拟机,Java Virtual Machine的缩写,是一个虚构出来的计算机,通过在实际的计算机上仿真模拟各种计算机功能来实 ...

  10. 《Java语言程序设计》上机实验

    实验一   Java环境演练   [目的] ①安装并配置Java运行开发环境: ②掌握开发Java应用程序的3个步骤:编写源文件.编译源文件和运行应用程序: ③学习同时编译多个Java源文件. [内容 ...

随机推荐

  1. 已经有 MESI 协议,为什么还需要 volatile 关键字?

    本文已收录到  GitHub · AndroidFamily,有 Android 进阶知识体系,欢迎 Star.技术和职场问题,请关注公众号 [彭旭锐] 进 Android 面试交流群. 前言 大家好 ...

  2. vs2019中使用Git,新建项目时总提示部分项目位于解决方案文件夹外

    最终还是用Git工具传上去的. 小伙子,用Git Bush或者Git CMD 和Git GUI传吧 我是用Git GUI. Git GUI汉化.感谢大佬 https://blog.csdn.net/u ...

  3. MySQL数据库和Python的交互

    一.缘由 这是之前学习的时候写下的基础代码,包含着MySQL数据库和Python交互的基本操作. 二.代码展示 import pymysql ''' 1.数据库的链接和创建视图 ''' # db=py ...

  4. ArcObjects SDK开发 021 开发框架搭建-FrameWork包设计

    1.框架引擎部分 引擎模块其实就是之前我们说的App-Command-Tool模块,通过这个模块,把系统的主干框架搭建起来. 其中大部分出现在菜单以及工具条上的按钮都会继承这个框架定义ICommand ...

  5. 4、Idea设置显示多行文件

    使用IDEA时,可能会没有注意到,一旦打开过多的Java文件时,默认会堆积在一行显示,就像浏览器打开了多个标签一样,此时需要通过右侧箭头筛选的方式来选择其他文件.为了解决这一问题,需要打开多行显示的方 ...

  6. IdentityServer4 - v4.x .Net中的实践应用

    认证授权服务的创建 以下内容以密码授权方式为例. 创建模拟访问DB各数据源类 为模拟测试准备的数据源. /// 假设的用户模型 public class TestUser { public strin ...

  7. [深度学习] 神经网络的理解(MLP RBF RBM DBN DBM CNN 整理学习)

    转载于 http://lanbing510.info/2014/11/07/Neural-Network.html 开篇语 文章整理自向世明老师的PPT,围绕神经网络发展历史,前馈网络(单层感知器,多 ...

  8. [深度学习] tf.keras入门5-模型保存和载入

    目录 设置 基于checkpoints的模型保存 通过ModelCheckpoint模块来自动保存数据 手动保存权重 整个模型保存 总体代码 模型可以在训练中或者训练完成后保存.具体文档参考:http ...

  9. 时钟同步服务器ntp安装文档

    应用场景 同步时钟很有必要,如果服务器的时间差过大会出现不必要的问题 大数据产生与处理系统是各种计算设备集群的,计算设备将统一.同步的标准时间用于记录各种事件发生时序, 如E-MAIL信息.文件创建和 ...

  10. 1.5万字总结 Redis 常见面试题&知识点

    以下内容来源于于我开源的 JavaGuide (Java学习&&面试指南,Github 130k star,370人共同参与爱完善), 万字总结,质量有保障! 这篇文章最早写于2019 ...