1.首先在数据库中建表

create database student
create table stu(
sno char(10),
sna char(10),
)
insert stu values('001','张三')
insert stu values('002','李四')

2.简单显示student表内容

<body>
<%
Connection con = null;
Statement stmt = null;
ResultSet rs = null; String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=student;user=sa;password=1";//sa身份连接 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(url);
stmt = con.createStatement(); request.setCharacterEncoding("utf-8");
String strsno = "";
strsno = request.getParameter("sno");
String SQL = "SELECT * FROM stu";
rs= stmt.executeQuery(SQL);
%>
学生信息<br>
<table border=1>
<tr>
<td>学号</td>
<td>姓名</td>
<td>操作</td>
</tr> <%while (rs.next()) { %>
<tr>
<td><%=rs.getString("sno") %></td>
<td><%=rs.getString("sna") %></td>
<td>
<a href="servlet/s.do?sno=<%=rs.getString("sno") %>" >删除</a>
<a href="StudentEdit.jsp?sno=<%=rs.getString("sno") %>" >编辑</a>
</td>
</tr>
<% } %>
</table>
<a href="StudentAdd.jsp" >插入</a>
</body>

  (import="java.sql.*" pageEncoding="utf-8",添加sqljdbc4.jar这两条从来不能忘)

结果图:

3.建Servlet:s.java(删除,这个命名有点问题)

package com.chao.webcrud;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class s extends HttpServlet { /**
* 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 { request.setCharacterEncoding("utf-8");
String strStudentID = "";
strStudentID = request.getParameter("sno"); // 删除数据
Connection con = null;
Statement stmt = null;
ResultSet rs = null; String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=student;user=sa;password=1";//sa身份连接 try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(url); String strSql = "delete from stu where sno='" + strStudentID + "'";
stmt = con.createStatement();
stmt.execute(strSql);
}
catch (Exception e)
{
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.println("删除失败,学号:" + strStudentID+"!");
out.close();
return;
} // 跳转
response.sendRedirect("../index.jsp");
} }

删除功能实现并自动跳转到主页

4.插入

先写StudentAdd.jsp

 <body>

<center>

  	<br><br>编辑学生信息<br>
<form name="f1" id="f1" action="servlet/StudentAdd" method="post">
<table border="0">
<tr>
<td>学号:</td>
<td><input type="text" name="sno" id="sna" value=""></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="sna" id="sna" value=""></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value=" 确定 " ></td>
</tr>
</table>
</form> </center>
</body>

 然后写Servlet:StudentAdd.java

package com.chao.webcrud;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class StudentAdd extends HttpServlet { /**
* 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");
String sno = "";
sno = request.getParameter("sno");
String sna = "";
sna = request.getParameter("sna"); // 删除数据
Connection con = null;
Statement stmt = null;
ResultSet rs = null; String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=student;user=sa;password=1";//sa身份连接 try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(url); String strSql = "insert into stu values('" + sno + "','" + sna + "') ";
stmt = con.createStatement();
stmt.execute(strSql);
}
catch (Exception e)
{
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.println("修改失败,学号:" + sno+"!");
out.close();
return;
} // 跳转
response.sendRedirect("../index.jsp");
} }

结果图: 

 

5.修改,同插入类似

StudentEdit.jsp

  <body>
<%
Connection con = null;
Statement stmt = null;
ResultSet rs = null; String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=student;user=sa;password=1";//sa身份连接 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(url);
stmt = con.createStatement(); request.setCharacterEncoding("utf-8");
String strID = "";
strID = request.getParameter("sno");
String SQL = "SELECT * FROM stu where sno='" + strID + "'";
rs = stmt.executeQuery(SQL);
%>
<center> <%if(rs.next()==true){ %> <br><br>编辑学生信息<br>
<form name="f1" id="f1" action="servlet/StudentEdit" method="post">
<table border="0">
<tr>
<td>学号:</td>
<td><input type="text" readonly="readonly" name="sno" id="sna" value="<%=rs.getString("sno") %>"> 学号不允许编辑</td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="sna" id="sna" value="<%=rs.getString("sna") %>"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value=" 确定 " ></td>
</tr>
</table>
</form>
<%} else { %>
<br><br>编辑学生信息<br><br>
修改成功<br><br>
<a href="index.jsp">返回</a>
<%} %>
</center> </body>

  Servlet:StudentEdit.java

package com.chao.webcrud;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class StudentEdit extends HttpServlet { /**
* 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");
String sno = "";
sno = request.getParameter("sno");
String sna = "";
sna = request.getParameter("sna"); // 删除数据
Connection con = null;
Statement stmt = null;
ResultSet rs = null; String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=student;user=sa;password=1";//sa身份连接 try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(url); String strSql = "update stu set sna='" + sna + "' where sno='" + sno + "'";
stmt = con.createStatement();
stmt.execute(strSql);
}
catch (Exception e)
{
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.println("修改失败,学号:" + sno+"!");
out.close();
return;
} // 跳转
response.sendRedirect("../StudentEdit.jsp");
} }

  结果图:

Servlet实现简单CRUD的更多相关文章

  1. 使用Struts2和jQuery EasyUI实现简单CRUD系统(转载汇总)

    使用Struts2和jQuery EasyUI实现简单CRUD系统(一)——从零开始,ajax与Servlet的交互 使用Struts2和jQuery EasyUI实现简单CRUD系统(二)——aja ...

  2. IDEA与Tomcat创建并运行Java Web项目及servlet的简单实现

    创建Web项目 1. File -> New Project ,进入创建项目窗口 2.在 WEB-INF 目录下点击右键, New -> Directory ,创建  classes 和  ...

  3. MongoDB简单CRUD场景

    MongoDB简单CRUD命令操作 (1)新建数据库:use 数据库名 (2)显示所有数据库:show dbs; (3)新建集合(两种方式)  隐式创建:在创建集合的同时往集合里面添加数据---db. ...

  4. SequoiaDB 系列之二 :SequoiaDB的简单CRUD操作

    上一篇通过一系列的操作,终于把SequoiaDB的集群部署到单台机器上了. 建议去安装体验一下吧. 在整个环境的部署的体验来看,并没有MongoDB的部署简单,但是比MongoDB的部署要清晰.Mon ...

  5. Servlet基础简单总结(上)

    Servlet基础一些简单总结(上): 1.Java Servlet是运行在Web服务器上的Java程序2.Java平台给Servlet开发者提供了强大的API/面向对象编程平台无关/强类型/垃圾回收 ...

  6. 学习总结------Servlet的简单理解

    1.什么是Servlet Servlet是一个Java编写的程序,在服务器端运行的(如tomcat) Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览 ...

  7. Servlet过滤器简单探索

    过滤器的工作时机介于浏览器和Servlet请求处理之间,可以拦截浏览器对Servlet的请求,也可以改变Servlet对浏览器的响应. 其工作模式大概是这样的: 一.Filter的原理 在Servle ...

  8. java web(二): servlet的简单使用和介绍

    前言:通过编写一个servlet类,向浏览器发送hello world来阐述servlet. 一:创建动态web项目 1. 首先切换到javaEE视图. 在ecplise左边即项目管理器哪里右键点击新 ...

  9. javaweb servlet jsp简单笔记

    第二章: 1: web 俗称 : 万维网  www 2: web开发 的三大核心: HTML(网页) ,URL(定位),HTTP:(协议) 页面的分类: 静态页面: html+css 动态页面:jsp ...

随机推荐

  1. 【转】Delphi+Halcon实战一:两行代码识别QR二维码

    Delphi+Halcon实战一:两行代码识别QR二维码 感谢网友:绝代双椒( QQ号应原作者要求隐藏了:xxxx6348)的支持 本文是绝代双椒的作品,因为最近在忙zw量化培训,和ziwang.co ...

  2. C语言回顾-整型变量修饰符和一维数组

    1.整型变量修饰符 1)改变整型变量的存储空间 #include <stdio.h> int main(int argc, const char * argv[]) { //改变整型变量占 ...

  3. 关于缺少nvToolsExt64_1.lib时的PhysX的处理

    我本人之所以缺少这个文件是因为我的PhysX如果你的PhysX是从UE4源代码中提取的,那么可能会出现如下错误,提示找不到nvToolsExt64_1.lib(本机是64bit的操作系统) 那就执行u ...

  4. soj 1015 Jill's Tour Paths 解题报告

    题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...

  5. SQL Server取datetime的日期部分

    在c#中有个Date属性用于返回日期,其实就是当天0点. DateTime dtNow = DateTime.Now; DateTime dtNow2 = dtNow.Date; Console.Wr ...

  6. activiti-5.15备份记录

    activiti-5.15用户手册翻译完成 源码下载地址:http://activiti.org/download.html在线浏览地址: http://www.mossle.com/docs/act ...

  7. HTML5学习小结

    HTML5是用于取代1999年所制定的 HTML4.01和XHTML1.0标准的HTML标准版本.HTML5的第一份正式草案已于2008年1月公布:2012年12月,规范已经正式定稿.W3C计划在20 ...

  8. C语言----------链表的简单操作

    #include <stdio.h> #include <malloc.h> typedef struct node{ //定义节点类型 char data; //数据域 st ...

  9. 关于el jstl

    requestScope表示从request范围内取得.其他的还有sessionScope, pageScope和applicationScope,分别对应4种属性范围.而如果直接写${passwor ...

  10. 通过Ajax异步提交的方法实现从数据库获取省份和城市信息实现二级联动(xml方法)

    之前有写过是从JavaScript数组里获取省市信息来实现二级联动,但是似乎有很多需求是要从数据库里获取信息,所以就需要根据异步提交,局部刷新的思想来实现来提高用户交互问题 第一种方法是xml方法 1 ...