JSP student information management system and jsp Student Achievement Management System
The examples in this article share with you the source code of the JSP student information management system, JSP + Servlet + Javabean + JDBC + MySQL for your reference. The details are as follows:
1. perform database operations at the service layer.
Package com. service;/*** responsible for all database operations on student information, adding, deleting, modifying, and querying */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 the 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.exe cuteUpdate (); return true;} catch (SQLException e) {// TODO Auto-generated catch block E. printStackTrace (); return false ;}// query the public List of all students <stuInfo> queryAllStu () {// query the student data List <stuInfo> stus = new ArrayList <stuInfo> (); // The information of each student is stored as every element of the list set in the list set try {pstmt = conn. prepareStatement ("select * from studentinfo"); ResultSet rs = pstmt.exe cuteQuery (); while (rs. next () {stuInfo stu = new stuInfo (); stu. setId (rs. getInt (1); stu. setNickname (rs. getString (2); stu. setTruena Me (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 the information of a single student. 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.exe cuteQuery (); 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 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.exe cuteUpdate (); return true;} catch (SQLException e) {// T ODO Auto-generated catch block e. printStackTrace (); return false ;}// Delete student information public Boolean deleteStu (int id) {try {pstmt = conn. prepareStatement ("delete from studentinfo where id =? "); Pstmt. setInt (1, id); pstmt.exe cuteUpdate (); return true;} catch (Exception e) {e. getStackTrace (); return false ;}}}
2. InputStuInfoServlet: Servlet for adding 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; public class inputStuInfoServlet extends HttpServlet {/*** Constructor 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 res Ponse 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 eq Uals 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 to All Forms 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"); // put it in Javabean to temporarily save 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) // method of inserting student data response. sendRedirect (".. /inputStuInfo_success.jsp "); else response. sendRedirect (".. /inputStuInfo. jsp "); // if the database fails to be inserted, the initial input page is returned.}/*** Initialization of the servlet. <br> ** @ throws ServletException if an error occurs */public void init () throws ServletException {// Put your code here }}
3. stuInfo: Javabean for saving Student Information
Package com. model; // Javabean is equivalent to a middleware used for transferring data between classes and classes. 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 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 code download: JSP student information management system
The above is all the content in this article, and I hope it will help you learn about JSP management systems.