JSP Student Information Management System _JSP programming

Source: Internet
Author: User
Tags db connect getdate

This example for you to share the JSP Student Information Management system source code, Jsp+servlet+javabean+jdbc+mysql, for your reference, the specific content as follows

1.service layer, perform database operations

Package com.service;
/** * Responsible for all student information database operations, additions and deletions to check * * 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;//Execute SQL statement public Stuinfoservice () {conn = new Com.conn.conn (). Getcon ();
     public boolean addstu (Stuinfo stu) {//Insert student data 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; }//Enquiry Yo Student Information public list<stuinfo> Queryallstu () {//query student data list<stuinfo> stus = new Arraylist<stuinf
   O> ()//each student's information is stored in the list collection as each element of the list collection 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;
  }//Query individual student Information 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;
 
} //Update Student Information public boolean updatestu (Stuinfo stu) {try {pstmt = conn. preparestatement ("Update Studentinf o 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; }///delete student information public Boolean deletestu (int id) {try {pstmt = conn.preparestatement ("Delete from Studentinfo wh
   ere id=? ");
   Pstmt.setint (1, id);
   Pstmt.executeupdate ();
  return true;
  catch (Exception e) {e.getstacktrace (); return false;
 }
 
 }
}

2.InputStuInfoServlet, add servlet     for student information

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;
  The 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 is called when a form has it 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 ifError occurred */public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexceptio
 N, IOException {doPost (request, response); /** * The DoPost method of the servlet.
  <br> * * This are called when a form has it 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 a error occurred * * Public voi D DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {request.set
  Characterencoding ("Utf-8");//get to the value of all controls in the form 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");
  Temporarily save Stuinfo stu=new Stuinfo () in JavaBean;
  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))//The method of inserting student data response.sendredirect ("...
  /inputstuinfo_success.jsp "); Else Response.sendredirect (".. /inputstuinfo.jsp ");//Insert database Failure returns initial input page}/** * Initialization of the servlet.  <br> * * @throws servletexception If an error occurs/public void init () throws Servletexception {//Put
 Your code here}}

3.stuInfo, save javabean     for student information

Package Com.model;
 JavaBean is equivalent to a middleware, for classes and classes, between each layer of the relay data of a transit point 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;
 The 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;
 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;
 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 class

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;}}}
 

SOURCE Download: JSP Student Information Management system

The above is the entire content of this article, I hope to learn from JSP management system to help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.