以下是学生页面

首先先给上数据库

在准备准备工作

以下为代码:

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. Linux系统安装 tftp服务 NFS服务

    安装tftp服务 安装 sudo apt-get install tftp-hpa tftpd-hpa 配置文件 # /etc/default/tftpd-hpa TFTP_USERNAME=&quo ...

  2. 在链表上实现 Partition 以及荷兰国旗问题

    在链表上实现 Partition 以及荷兰国旗问题 作者:Grey 原文地址: 博客园:在链表上实现 Partition 以及荷兰国旗问题 CSDN:在链表上实现 Partition 以及荷兰国旗问题 ...

  3. MIUI12解决安装charles抓包安装证书后还是提示证书不安全

    前言 我抓包这么长时间,这个问题我还是第一次遇到,导致我反复试验,明明安装证书还是提示不安全.我用新买的手机MIUI 12系统弄了半天 解决方案 首先下载证书这部分是一个坑,小米 MIUI 12系统下 ...

  4. linux系统编码修改

    1. 查看当前系统默认采用的字符集locale 2. 查看系统当前编码echo $LANG如果输出为:en_US.UTF-8     英文zh_CN.UTF-8     中文 3. 查看系统是否安装中 ...

  5. 【数据库】Oracle建表、创建序列、添加触发器生成自增主键

    CREATE TABLE "TEST"."T_ORDER" (    "AUUID_0" VARCHAR2 ( 255 ) NOT NULL ...

  6. 【软考-中级-其他】03、NoSQL和云计算

    其他 NoSQL概述 分类 文档存储数据库:MongoDB 采用BSON格式完成存储数据和网络数据交换 BSON格式:JSON的二进制编码格式 逻辑结构包括:数据库.集合(相当于关系数据库的表).文档 ...

  7. 【每日一题】【暴力&双指针&动态规划】42. 接雨水-211130/220214

    给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 方法1:暴力求解(超时) import java.util.*; public class Solu ...

  8. 三道MISC的writeup

    (1)背时 描述:腐烂了,变异了,太背时了...... 附件为一个压缩包 解题思路: 1.打开压缩包,发现有一个描述:v(51wA:I7uABi#Bx(T 2.将v(51wA:I7uABi#Bx(T进 ...

  9. 出现报错:The field admin.LogEntry.user was declared with a lazy reference to 'api.user', but app 'api' isn't installed.解决方法

  10. Go语言Golang DevOps运维开发实战

    Go语言Golang DevOps运维开发实战 提高运维意识.从下到上,从上到下的工作都要做好,对上运维工作的价值和含金量可以得到认可,对下我们的工作能够提高效率解放运维.运维意识是很重要,并不是你技 ...