以下是学生页面

首先先给上数据库

在准备准备工作

以下为代码:

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. c++ *和& 指针,取内容,别名,取地址

    *前面有类型符时为定义指针 &前面有类型符时为定义引用变量(别名) (int ,float,long,double,char等 ) *p:定义xx类型的指针 int *p 整型指针,char ...

  2. Axios +Vue + themeleay

    1.pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...

  3. 8 STL-stack

    ​ 重新系统学习c++语言,并将学习过程中的知识在这里抄录.总结.沉淀.同时希望对刷到的朋友有所帮助,一起加油哦!  生命就像一朵花,要拼尽全力绽放!死磕自个儿,身心愉悦! 写在前面,本篇章主要介绍S ...

  4. MySQL进阶实战6,缓存表、视图、计数器表

    一.缓存表和汇总表 有时提升性能最好的方法是在同一张表中保存衍生的冗余数据,有时候还需要创建一张完全独立的汇总表或缓存表. 缓存表用来存储那些获取很简单,但速度较慢的数据: 汇总表用来保存使用grou ...

  5. 通过Shell脚本自动安装Hive&JDBC测试&提供CDH5网盘地址

    〇.参考地址 1.Linux下编写脚本自动安装hive https://blog.csdn.net/weixin_44911081/article/details/121227024?ops_requ ...

  6. 【每日一题】【找到位置返回&升序数组中第K大就是n-K小】2022年1月17日-NC88 寻找第K大

    描述有一个整数数组,请你根据快速排序的思路,找出数组中第 k 大的数. 给定一个整数数组 a ,同时给定它的大小n和要找的 k ,请返回第 k 大的数(包括重复的元素,不用去重),保证答案存在. 方法 ...

  7. 个人电脑公网IPv6配置

    一.前言 自己当时以低价买的阿里ECS云服务器马上要过期了,对于搭建个人博客.NAS这样服务器的需求购买ECS服务器成本太高了,刚好家里有台小型的桌面式笔记本,考虑用作服务器,但是公网IPv4的地址实 ...

  8. 为什么总是应该考虑给定 List 的初始大小

    在 .Net 技术中,使用 List<> 来存储数据是很常见的.List<> 是一个可以动态增长的泛型集合类型,可以存储任何类型的数据. 但是,在实际使用中,很多人并不注意给定 ...

  9. vue下载与安装

    首先安装node.js环境: node.js 安装推荐文章:https://www.cnblogs.com/zhouyu2017/p/6485265.html 基于node.js安装淘宝镜像npm i ...

  10. JavaScript:对象:对象和属性的内存结构是什么样的?

    在说变量的时候,大致画了变量的内存结构,现在来看一下对象的内存结构是什么样的,有助于我们理解传参的各种情况,只是大致的画一下内存模型,不代表实际内存情况. 我们可以用一段代码,来判断一下,是不是这样的 ...