无为清净楼资源网 Design By www.qnjia.com
本文实例为大家分享了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=" + " major="); 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"; 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管理系统有所帮助。
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月16日
2024年11月16日
- 《怪猎荒野》PS5Pro主机版对比:B测性能都不稳定
- 黄宝欣.1992-黄宝欣金装精选2CD【HOMERUN】【WAV+CUE】
- 群星.1996-宝丽金流行爆弹精丫宝丽金】【WAV+CUE】
- 杜德伟.2005-独领风骚新歌精选辑3CD【滚石】【WAV+CUE】
- 安与骑兵《心无疆界》[低速原抓WAV+CUE]
- 柏菲唱片-群星〈胭花四乐〉2CD[原抓WAV+CUE]
- 金典女声发烧靓曲《ClassicBeautifulSound》2CD[低速原抓WAV+CUE]
- 王杰1992《封锁我一生》粤语专辑[WAV+CUE][1G]
- 群星《一人一首成名曲 (欧美篇)》6CD[WAV/MP3][7.39G]
- 东来东往2004《回到我身边·别说我的眼泪你无所谓》先之唱片[WAV+CUE][1G]
- MF唱片-《宝马[在真HD路上]》2CD[低速原抓WAV+CUE]
- 李娜《相信我》新时代[WAV+CUE]
- 2019明达发烧碟MasterSuperiorAudiophile[WAV+CUE]
- 蔡幸娟.1993-相爱容易相处难【飞碟】【WAV+CUE】
- 陆虎.2024-是否愿意成为我的全世界【Hikoon】【FLAC分轨】