目录
目录
一,使用工具
1,搭建数据库
二,创建Eclipse项目
1.创建一个web项目
三,项目搭建
1.项目介绍
四,代码展示 -jsp部分
(1)制作登录页面denglu.jsp
(2)制作注册页面reg.jsp
3.制作查询页面select.jsp
4.制作表格 index.jsp
5.制作编辑页面edit.jsp
五,代码展示---src部分
1.设置dao文件夹
1.创建Common.java
2.设置filter文件夹
1.编辑EncodingFilter.java文件夹
3.设置pojo文件夹
1.设置Student.java
4.设置servlet文件夹
1.编辑ByIDServlet.java
2.编辑DelServlet.java
3.编辑EditServlet.java
4.编辑LoginServlet.java
5.编辑QueryServlet.java
6.编辑RegServlet.java
7.设置web.xml
1.eclipse
2.SQL Server 2008
3.apache-tomact-8.0.9
链接:https://pan.baidu.com/s/1H23n_urB1iF8rlqQKE5JdA
提取码:oiv9
4.sqljdbc4-2.0.
链接:https://pan.baidu.com/s/1ynSxjo_tJmSUoPjrFJmp9A
提取码:27k0
二,前期准备---数据库
(1)新建一个数据库起名为School。创建一个新的表起名为Student,数据库结构基本为下图。
(2)将列名与数据类型修改如下图
注意:!!!
stuid需要设置自增(设置方法:将--表示规范--是标识 修改为是)
(3)修改数据库,用户名密码,修改登录属性
右键点击 sa(自己的用户名)选择属性,弹出来了登录属性,在次修改密码admin(都可以自己记住即可)
在登录属性页面,将登录修改为启用。确定。
(4)更改服务器身份验证
右击数据库---选择属性---在服务器属性中--点击安全性--选择SQL Server和Windows身份验证模式
(5)重启关闭sql或者重启sql服务
重启SQL Server(MSSQLSERVER)这个服务
(5)保存表格,测试数据库连通性
断开连接
重新连接数据库
选择SQL Server 身份验证
输入你自己的用户名与密码
登录成功,即可!!!
Ctrl+N快捷方式 ///或者选择左侧空白处右击选择New 再Other..
搜索web 选择Dynamic Web Project
在Project name中填项目名称,在Target runtime(目标运行时)选择Apache Tomcat v8.0服务
在New Runtime...中选择Apache Tomcat v8.0(要与自己下载的Apache tomcat版本相适应)
Apache Tomcat v8.0下载链接(要解压到C盘的根目录)
链接:https://pan.baidu.com/s/1H23n_urB1iF8rlqQKE5JdA
提取码:oiv9
在此处导入apache-tomcat-8.0.9
前期环境搭建完成
在lib目录下放着sqljdbc4-2.0.jar
链接:https://pan.baidu.com/s/1ynSxjo_tJmSUoPjrFJmp9A
提取码:27k0
项目结构
项目名
src
dao 数据访问对象,对数据库进行增删改查操作
Common.java 对数据库执行增删改查的公共代码
filter 过滤器 ,设置请求和响应的字符编码格式
EncodingFilter.java 过滤字符编码格式的,[!!!@WebFilter("/")]
pojo 简单的Java对象,封装向jsp页面传递的数据
Teacher.java [提醒:
1,对应数据库表中的所有列,都要变成私有的属性,并且每个属性的第一一个单词首字母都是小写。
2,通过右键创建2个构造方法 (方法是 右键---source----generate construction...)
3,所有的属性,生成对应的get和set方法 (方法是 右键---source----generate getter and setter...)
servlet 服务连接器 接受并处理客户端浏览器提交的请求
SelectServlet.java 负责查询学生信息
lnsertServlet.java 负责新增学生信息
UpdateServlet.java 负责修改学生信息
LoginServlet.java 负责登录
ByidServlet.java 负责查询要编辑的学生信息
DeleteServlet.java 负责删除学生信息
WebContent
WEB-INF
lib
sqlijdbc4-2.0.jar 必须把jar文件放入lib目录
web.xml 创建web项目时通过选择generate web.xml
select.jsp 查询页面
edit.jsp 编辑
index.jsp
reg.jsp 注册
denglu.jsp 登录页面
项目结构如图所示
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
效果图
<%@page import="pojo.Student"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
查询条件
查询结果
编号
姓名
性别
爱好
年级
简介
操作
<%
List list =(List)session.getAttribute("stuList");
if(list!=null){
for(Student stu : list){
String love=stu.getLove().replace("[","").replace("]","").replace("1","吃饭").replace("2","睡觉").replace("3","看电影");
String grade=(stu.getGrade()+"").replace("1","大一").replace("2","大二").replace("3","大三").replace("4","大四");
%>
<%=stu.getStuid() %>
<%=stu.getStuname() %>
<%=stu.getSex() %>
<%=love %>
<%=grade %>
<%=stu.getInfo() %>
编辑
删除
<%
}
}
%>
效果图
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<%
for(int i=0;i<5;i++){
%>
<%
}
%>
编号 姓名 年龄
<%
for(int i=0;i<5;i++){
%>
<%="0"+i %> 李四 12
<%
}
%>
<% String name="星期"; %>
效果图
<%@page import="pojo.Student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<%
Student stu=(Student)session.getAttribute("editStu");
%>
dao数据访问对象,对数据库进行增删改查操作
在次文件夹下,创建Java项目,用于数据库执行增删改查的公共代码
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Common {
//
private Connection conn =null;
private Statement stmt =null;
private ResultSet rs=null;
private Connection getConnection() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=school", "sa","admin");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public void close() {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
public ResultSet executeQuery(String sql){
try {
conn=this.getConnection();
stmt = conn.createStatement();//
rs=stmt.executeQuery(sql);
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
public int executeUpdate(String sql) {
int row = 0;
try {
conn=this.getConnection();
stmt = conn.createStatement();//
row = stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
this.close();
}
return row;
}
}
package filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
@WebFilter("/*")
public class EncodingFilter implements Filter {
public EncodingFilter() {
// TODO Auto-generated constructor stub
}
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
chain.doFilter(request, response);
}
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
package pojo;
public class Student {
private int stuid;
private String stuname;
private String stupwd;
private String sex;
private String love;
private int grade;
private String info;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int stuid, String stuname, String stupwd, String sex, String love, int grade, String info) {
super();
this.stuid = stuid;
this.stuname = stuname;
this.stupwd = stupwd;
this.sex = sex;
this.love = love;
this.grade = grade;
this.info = info;
}
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStupwd() {
return stupwd;
}
public void setStupwd(String stupwd) {
this.stupwd = stupwd;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getLove() {
return love;
}
public void setLove(String love) {
this.love = love;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import dao.Common;
import pojo.Student;
@WebServlet("/ByIDServlet")
public class ByIDServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public ByIDServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、获取要修改的学生编号
String stuid=request.getParameter("stuid");
//2、拼接根据编号查询学生信息的sql语句
String sql="select * from student where stuid="+stuid;
//3、调用查询方法
Common com = new Common();
ResultSet rs=com.executeQuery(sql);
Student stu=null;
try {
while (rs.next()) {
stu = new Student(rs.getInt("stuid"), rs.getString("stuname"), rs.getString("stupwd"), rs.getString("sex"),
rs.getString("love"), rs.getInt("grade"), rs.getString("info"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
com.close();
}
//4、把根据编号查询到的学生信息保存到session中
request.getSession().setAttribute("editStu", stu);
//5、跳转到编辑页面
response.sendRedirect("edit.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.Common;
@WebServlet("/DelServlet")
public class DelServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public DelServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取要删除的学生编号
String stuid=request.getParameter("stuid");
//2.组合新的删除sql语句
String sql="delete from student where stuid="+stuid;
//3.调用删除方法
Common com = new Common();
int row=com.executeUpdate(sql);
//4.给出删除提示
if(row>0){
//删除成功给出提示,之后回到查询页面,最好刷新数据
response.getWriter().print("");
}else{
//删除失败给出提示,之后回到查询页面
response.getWriter().print("");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package servlet;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.Common;
@WebServlet("/EditServlet")
public class EditServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public EditServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.
String stuid=request.getParameter("stuid");
String uname=request.getParameter("uname");
String upwd=request.getParameter("upwd");
String sex=request.getParameter("sex");
String[] loves=request.getParameterValues("love");
String grade=request.getParameter("grade");
String info=request.getParameter("info");
//2.
String str="update student set stuname='%s',stupwd='%s',sex='%s',love='%s',grade='%s',info='%s' where stuid=%s";
String sql=String.format(str,uname,upwd,sex,Arrays.toString(loves),grade,info,stuid);
//3.
Common com = new Common();
int row=com.executeUpdate(sql);
//4.
if(row>0){
//删除成功给出提示,之后回到查询页面,最好刷新数据
response.getWriter().print("");
}else{
//删除失败给出提示,之后回到查询页面
response.getWriter().print("");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package servlet;
import dao.Common;
import java.io.IOException;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.Common;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name=request.getParameter("userName");
String pwd=request.getParameter("userPwd");
System.out.println(name+"......."+pwd);
String str="select * from student where stuname='%s' and stupwd='%s'";
String sql=String.format(str,name,pwd);
Common com = new Common();
ResultSet rs=com.executeQuery(sql);
try {
if (rs.next()) {
response.sendRedirect("select.jsp");
} else {
response.getWriter().println("");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
com.close();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package servlet;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.Common;
import pojo.Student;
@WebServlet("/QueryServlet")
public class QueryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public QueryServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String uname = request.getParameter("uname");
String grade = request.getParameter("grade");
String str = "select * from student where stuname like '%s'";
if (!grade.equals("0")) {
str += " and grade=%s";
}
String sql = String.format(str, "%" + uname + "%", grade);
System.out.println(sql);
Common com = new Common();
ResultSet rs = com.executeQuery(sql);
List list = new ArrayList();
try {
while (rs.next()) {
Student stu = new Student(rs.getInt("stuid"), rs.getString("stuname"), rs.getString("stupwd"), rs.getString("sex"),
rs.getString("love"), rs.getInt("grade"), rs.getString("info"));
list.add(stu);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
com.close();
}
request.getSession().setAttribute("stuList", list);
response.sendRedirect("select.jsp");//閲嶅畾鍚�
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package servlet;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.Common;
@WebServlet("/RegServlet")
public class RegServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public RegServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String uname=request.getParameter("uname");
String upwd=request.getParameter("upwd");
String sex=request.getParameter("sex");
String[] loves=request.getParameterValues("love");
String grade=request.getParameter("grade");
String info=request.getParameter("info");
System.out.println(uname+"——"+upwd+"——"+"——"+Arrays.toString(loves)+sex+"——"+grade+"——"+info);
Common com = new Common();
String str="insert into student(stuname,stupwd,sex,love,grade,info)values('%s','%s','%s','%s',%s,'%s')";
String sql=String.format(str, uname,upwd,sex,Arrays.toString(loves),grade,info);
System.out.println(sql);
int row = com.executeUpdate(sql);
if(row > 0){
response.getWriter().print("");
}else{
response.getWriter().print(" ");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
zbvc
login.html
index.htm
index.jsp
default.html
default.htm
default.jsp