首页 > 编程语言 >2021级《JAVA语言程序设计》上机考试试题2

2021级《JAVA语言程序设计》上机考试试题2

时间:2022-12-23 22:44:40浏览次数:39  
标签:JAVA String 上机 rs req Mima state 2021 public

以下是学生页面

首先先给上数据库

 

 在准备准备工作

 

 以下为代码:

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一样

标签:JAVA,String,上机,rs,req,Mima,state,2021,public
From: https://www.cnblogs.com/JIANGzihao0222/p/17001770.html

相关文章