JSP学生信息管理系统
本文实例为大家分享了JSP学生信息管理系统源码,JSP+Servlet+Javabean+JDBC+MySQL,供大家参考,具体内容如下
1.service层,进行数据库操作
packagecom.service;
/**
*负责学生信息的所有数据库操作,增删改查
*/
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.util.ArrayList;
importjava.util.List;
importcom.model.stuInfo;
publicclassstuInfoService{
privateConnectionconn;
privatePreparedStatementpstmt;//执行sql语句
publicstuInfoService(){
conn=newcom.conn.conn().getCon();
}
publicbooleanaddStu(stuInfostu){//插入学生数据
try{
pstmt=conn.prepareStatement("insertintostudentinfo"
+"(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();
returntrue;
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
returnfalse;
}
}
//查询所哟学生信息
publicList<stuInfo>queryAllStu(){//查询学生数据
List<stuInfo>stus=newArrayList<stuInfo>();//每一个学生的信息作为list集合的每一个元素存储在list集合中
try{
pstmt=conn.prepareStatement("select*fromstudentinfo");
ResultSetrs=pstmt.executeQuery();
while(rs.next()){
stuInfostu=newstuInfo();
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);
}
returnstus;
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
returnnull;
}
}
//查询单个学生信息
publicstuInfoqueryStubyID(intid){
//Liststus=newArrayList();
try{
pstmt=conn
.prepareStatement("select*fromstudentinfowhereid=?");
pstmt.setInt(1,id);
ResultSetrs=pstmt.executeQuery();
if(rs.next()){
stuInfostu=newstuInfo();
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);
returnstu;
}
returnnull;
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
returnnull;
}
}
//更新学生信息
publicbooleanupdateStu(stuInfostu){
try{
pstmt=conn
.prepareStatement("updatestudentinfosetNickname=?,truename=?,sex=?,birthday=?,"
+"major=?,course=?,interest=?,remark=?whereid=?");
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();
returntrue;
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
returnfalse;
}
}
//删除学生信息
publicBooleandeleteStu(intid){
try{
pstmt=conn.prepareStatement("deletefromstudentinfowhereid=?");
pstmt.setInt(1,id);
pstmt.executeUpdate();
returntrue;
}catch(Exceptione){
e.getStackTrace();
returnfalse;
}
}
}
2.InputStuInfoServlet,添加学生信息的Servlet
packagecom.servlet;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.util.Date;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcom.model.stuInfo;
importcom.service.stuInfoService;
publicclassinputStuInfoServletextendsHttpServlet{
/**
*Constructoroftheobject.
*/
publicinputStuInfoServlet(){
super();
}
/**
*Destructionoftheservlet.<br>
*/
publicvoiddestroy(){
super.destroy();//Justputs"destroy"stringinlog
//Putyourcodehere
}
/**
*ThedoGetmethodoftheservlet.<br>
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.
*
*@paramrequesttherequestsendbytheclienttotheserver
*@paramresponsetheresponsesendbytheservertotheclient
*@throwsServletExceptionifanerroroccurred
*@throwsIOExceptionifanerroroccurred
*/
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doPost(request,response);
}
/**
*ThedoPostmethodoftheservlet.<br>
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.
*
*@paramrequesttherequestsendbytheclienttotheserver
*@paramresponsetheresponsesendbytheservertotheclient
*@throwsServletExceptionifanerroroccurred
*@throwsIOExceptionifanerroroccurred
*/
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
request.setCharacterEncoding("utf-8");//get到表单所有的控件的值
Stringnickname=request.getParameter("nickname");
Stringtruename=request.getParameter("truename");
bytesex=Byte.parseByte(request.getParameter("sex"));
Stringbirthday=request.getParameter("birthday");
Stringmajor=request.getParameter("major");
System.out.println(major);
//Stringcourse=request.getParameter("course");
Stringcourses[]=request.getParameterValues("course");
Stringinterests[]=request.getParameterValues("interest");
Stringremark=request.getParameter("remark");
//放到Javabean中暂时保存
stuInfostu=newstuInfo();
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(newstuInfoService().addStu(stu))//插入学生数据的方法
response.sendRedirect("../inputStuInfo_success.jsp");
else
response.sendRedirect("../inputStuInfo.jsp");//插入数据库失败则返回初始输入页面
}
/**
*Initializationoftheservlet.<br>
*
*@throwsServletExceptionifanerroroccurs
*/
publicvoidinit()throwsServletException{
//Putyourcodehere
}
}
3.stuInfo,保存学生信息的Javabean
packagecom.model;
//Javabean相当于是一个中间件,用于类与类之间,各层之间的中转数据的一个中转站
publicclassstuInfo{
privateintid;
privateStringnickname;
privateStringtruename;
privatebytesex;
privateStringbirthday;
privateStringmajor;
privateString[]course={""};
privateStringcourses="";
privateString[]interest={""};
privateStringinterests="";
privateStringremark;
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicStringgetNickname(){
returnnickname;
}
publicvoidsetNickname(Stringnickname){
this.nickname=nickname;
}
publicStringgetTruename(){
returntruename;
}
publicvoidsetTruename(Stringtruename){
this.truename=truename;
}
publicbytegetSex(){
returnsex;
}
publicvoidsetSex(bytesex){
this.sex=sex;
}
publicStringgetbirthday(){
returnbirthday;
}
publicvoidsetbirthday(Stringbirthday){
this.birthday=birthday;
}
publicStringgetmajor(){
returnmajor;
}
publicvoidsetmajor(Stringmajor){
this.major=major;
}
publicString[]getcourse(){
returncourse;
}
publicvoidsetcourse(String[]course){
this.course=course;
}
publicStringgetcourses(){
if(course!=null)
{courses="";
for(inti=0;i<course.length;i++)
courses+=course[i]+"&";
}
courses=courses.substring(0,courses.length()-1);
returncourses;
}
publicvoidsetcourses(Stringcourses){
this.courses=courses;
}
publicString[]getinterest(){
returninterest;
}
publicvoidsetinterest(String[]interest){
this.interest=interest;
}
publicStringgetinterests(){
if(interest!=null)
{interests="";
for(inti=0;i<interest.length;i++)
interests+=interest[i]+"&";
}
interests=interests.substring(0,interests.length()-1);
returninterests;
}
publicvoidsetinterests(Stringinterests){
this.interests=interests;
}
publicStringgetremark(){
returnremark;
}
publicvoidsetremark(Stringremark){
this.remark=remark;
}
}
4.DBconnect类
packagecom.conn;
importjava.sql.Connection;
importjava.sql.DriverManager;
publicclassconn{
publicConnectiongetCon(){
try{
Class.forName("com.mysql.jdbc.Driver");
Stringurl="jdbc:mysql://localhost/Stu_info_System?useUnicode=true&characterEncoding=utf-8";
Stringuser="root";
Stringpassword="root";
Connectionconn=DriverManager.getConnection(url,user,password);
System.out.println(conn.getMetaData().getURL());
returnconn;
}catch(Exceptione){
e.printStackTrace();
returnnull;
}
}
}
源码下载:JSP学生信息管理系统
以上就是本文的全部内容,希望对大家学习JSP管理系统有所帮助。