欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JSP学生信息管理系统

程序员文章站 2023-11-07 13:11:34
本文实例为大家分享了jsp学生信息管理系统源码,jsp+servlet+javabean+jdbc+mysql,供大家参考,具体内容如下 1.service层,进行数据库...

本文实例为大家分享了jsp学生信息管理系统源码,jsp+servlet+javabean+jdbc+mysql,供大家参考,具体内容如下

1.service层,进行数据库操作    

package com.service;
/** 
 * 负责学生信息的所有数据库操作,增删改查
 */
import java.sql.connection;
import java.sql.preparedstatement;
import java.sql.resultset;
import java.sql.sqlexception;
import java.util.arraylist;
import java.util.list;
 
import com.model.stuinfo;
 
public class stuinfoservice {
 private connection conn;
 private preparedstatement pstmt;//执行sql语句
 
 public stuinfoservice() {
  conn = new com.conn.conn().getcon();
 }
 
 public boolean addstu(stuinfo stu) {//插入学生数据
  try {
   pstmt = conn.preparestatement("insert into studentinfo"
     + "(nickname,truename,sex,birthday,major,course,interest,remark) "
     + "values(?,?,?,?,?,?,?,?)");
   pstmt.setstring(1, stu.getnickname());
   pstmt.setstring(2, stu.gettruename());
   pstmt.setbyte(3, stu.getsex());
   pstmt.setstring(4, stu.getbirthday());
   pstmt.setstring(5, stu.getmajor());
   pstmt.setstring(6, stu.getcourses());
   pstmt.setstring(7, stu.getinterests());
   pstmt.setstring(8, stu.getremark());
 
   pstmt.executeupdate();
   return true;
  } catch (sqlexception e) {
   // todo auto-generated catch block
 
   e.printstacktrace();
   return false;
  }
 
 }
//查询所哟学生信息
 public list<stuinfo> queryallstu() {//查询学生数据
  list<stuinfo> stus = new arraylist<stuinfo>();//每一个学生的信息作为list集合的每一个元素存储在list集合中
  try {
   pstmt = conn.preparestatement("select * from studentinfo");
   resultset rs = pstmt.executequery();
   while (rs.next()) {
    stuinfo stu = new stuinfo();
    stu.setid(rs.getint(1));
    stu.setnickname(rs.getstring(2));
    stu.settruename(rs.getstring(3));
    stu.setsex(rs.getbyte(4));
    if (rs.getdate(5) != null)
     stu.setbirthday(rs.getdate(5).tostring());
    stu.setmajor(rs.getstring(6));
    if (rs.getstring(7) != null)
     stu.setcourse(rs.getstring(7).split("&"));
    if (rs.getstring(8) != null)
     stu.setinterest(rs.getstring(8).split("&"));
    stu.setremark(rs.getstring(9));
    stus.add(stu);
 
   }
   return stus;
 
  } catch (sqlexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
   return null;
  }
 
 }
//查询单个学生信息
 public stuinfo querystubyid(int id) {
  // list stus = new arraylist();
  try {
   pstmt = conn
     .preparestatement("select * from studentinfo where id=?");
   pstmt.setint(1, id);
   resultset rs = pstmt.executequery();
   if (rs.next()) {
    stuinfo stu = new stuinfo();
    stu.setid(rs.getint(1));
    stu.setnickname(rs.getstring(2));
    stu.settruename(rs.getstring(3));
    stu.setsex(rs.getbyte(4));
    if (rs.getdate(5) != null)
     stu.setbirthday(rs.getdate(5).tostring());
    stu.setmajor(rs.getstring(6));
    if (rs.getstring(7) != null)
     stu.setcourse(rs.getstring(7).split("&"));
    if (rs.getstring(8) != null)
     stu.setinterest(rs.getstring(8).split("&"));
    stu.setremark(rs.getstring(9));
    // stus.add(stu);
    return stu;
 
   }
   return null;
 
  } catch (sqlexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
   return null;
  }
 
 }
  //更新学生信息
 public boolean updatestu(stuinfo stu) {
 
  try {
   pstmt = conn
     .preparestatement("update studentinfo set nickname=? , truename=? , sex=? ,birthday=? ,"
       + " major=? ,course=? , interest=?, remark=? where id=?");
   pstmt.setstring(1, stu.getnickname());
   pstmt.setstring(2, stu.gettruename());
   pstmt.setbyte(3, stu.getsex());
   pstmt.setstring(4, stu.getbirthday());
   pstmt.setstring(5, stu.getmajor());
   pstmt.setstring(6, stu.getcourses());
   pstmt.setstring(7, stu.getinterests());
   pstmt.setstring(8, stu.getremark());
   pstmt.setint(9, stu.getid());
   pstmt.executeupdate();
   return true;
  } catch (sqlexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
   return false;
  }
 }
  //删除学生信息
 public boolean deletestu(int id) {
 
  try {
   pstmt = conn.preparestatement("delete from studentinfo where id=?");
   pstmt.setint(1, id);
   pstmt.executeupdate();
   return true;
  } catch (exception e) {
   e.getstacktrace();
   return false;
  }
 
 }
}

2.inputstuinfoservlet,添加学生信息的servlet    

package com.servlet;
 
import java.io.ioexception;
import java.io.printwriter;
import java.util.date;
 
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
 
import com.model.stuinfo;
import com.service.stuinfoservice;
 
public class inputstuinfoservlet extends httpservlet {
 
 /**
  * constructor of the object.
  */
 public inputstuinfoservlet() {
  super();
 }
 
 /**
  * destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // just puts "destroy" string in log
  // put your code here
 }
 
 /**
  * the doget method of the servlet. <br>
  *
  * this method is called when a form has its tag value method equals to get.
  * 
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws servletexception if an error occurred
  * @throws ioexception if an error occurred
  */
 public void doget(httpservletrequest request, httpservletresponse response)
   throws servletexception, ioexception {
 
  dopost(request, response);
 }
 
 /**
  * the dopost method of the servlet. <br>
  *
  * this method is called when a form has its tag value method equals to post.
  * 
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws servletexception if an error occurred
  * @throws ioexception if an error occurred
  */
 public void dopost(httpservletrequest request, httpservletresponse response)
   throws servletexception, ioexception {
  request.setcharacterencoding("utf-8");//get到表单所有的控件的值
  string nickname=request.getparameter("nickname");
  string truename=request.getparameter("truename");
  byte sex=byte.parsebyte(request.getparameter("sex"));
  string birthday=request.getparameter("birthday");
  string major=request.getparameter("major");
  system.out.println(major);
  //string course=request.getparameter("course");
  string courses[]=request.getparametervalues("course");
  string interests[]=request.getparametervalues("interest");
  string remark=request.getparameter("remark");
  //放到javabean中暂时保存
  stuinfo stu=new stuinfo();
  stu.setnickname(nickname);
  stu.settruename(truename);
   
  stu.setbirthday(birthday);
  if(birthday.equals(""))
   stu.setbirthday(null);
  if(courses!=null)
  stu.setcourse(courses);
  if(interests!=null)
  stu.setinterest(interests);
  stu.setremark(remark);
  stu.setmajor(major);
  stu.setsex(sex);
  if(new stuinfoservice().addstu(stu))//插入学生数据的方法
   response.sendredirect("../inputstuinfo_success.jsp");
  else
   response.sendredirect("../inputstuinfo.jsp");//插入数据库失败则返回初始输入页面
   
 }
 
 /**
  * initialization of the servlet. <br>
  *
  * @throws servletexception if an error occurs
  */
 public void init() throws servletexception {
  // put your code here
 }
 
}

3.stuinfo,保存学生信息的javabean    

package com.model;
//javabean相当于是一个中间件,用于类与类之间,各层之间的中转数据的一个中转站
public class stuinfo {
 private int id;
 private string nickname;
 private string truename;
 private byte sex;
 private string birthday;
 private string major;
 private string[] course = { "" };
 private string courses = "";
 private string[] interest = { "" };
 private string interests = "";
 private string remark;
 public int getid() {
  return id;
 }
 public void setid(int id) {
  this.id = id;
 }
 public string getnickname() {
  return nickname;
 }
 public void setnickname(string nickname) {
  this.nickname = nickname;
 }
 public string gettruename() {
  return truename;
 }
 public void settruename(string truename) {
  this.truename = truename;
 }
 public byte getsex() {
  return sex;
 }
 public void setsex(byte sex) {
  this.sex = sex;
 }
 public string getbirthday() {
  return birthday;
 }
 public void setbirthday(string birthday) {
  this.birthday = birthday;
 }
 public string getmajor() {
  return major;
 }
 public void setmajor(string major) {
  this.major = major;
 }
 public string[] getcourse() {
  return course;
 }
 public void setcourse(string[] course) {
  this.course = course;
 }
 public string getcourses() { 
  if(course!=null)
  {courses="";
   for(int i=0;i<course.length;i++)
   courses+=course[i]+"&";
  }
  courses=courses.substring(0,courses.length()-1);
  return courses;
 }
 public void setcourses(string courses) {
  this.courses = courses;
 }
 public string[] getinterest() {
  return interest;
 }
 public void setinterest(string[] interest) {
  this.interest = interest;
 }
 public string getinterests() {
  if(interest!=null)
  {interests="";
   for(int i=0;i<interest.length;i++)
   interests+=interest[i]+"&";
  }
  interests=interests.substring(0,interests.length()-1);
  return interests;
 }
 public void setinterests(string interests) {
  this.interests = interests;
 }
 public string getremark() {
  return remark;
 }
 public void setremark(string remark) {
  this.remark = remark;
 }
}

4.db connect 类    

package com.conn;
 
import java.sql.connection;
import java.sql.drivermanager;
 
public class conn {
 
 public connection getcon() {
  try {
   class.forname("com.mysql.jdbc.driver");
   string url = "jdbc:mysql://localhost/stu_info_system?useunicode=true&characterencoding=utf-8";
   string user = "root";
   string password = "root";
   connection conn = drivermanager.getconnection(url, user, password);
   system.out.println(conn.getmetadata().geturl());
 
   return conn;
  } catch (exception e) {
   e.printstacktrace();
   return null;
  }
 
 }
 
}

源码下载:jsp学生信息管理系统

以上就是本文的全部内容,希望对大家学习jsp管理系统有所帮助。