一般都和tomcat一起使用,tomact应用(java)容器,用来处理动态页面
现在写一个小程序,关于员工列表。会用到jdbc。用的是oracal数据库
具体的流程是1,进入html页面,点提交按钮
2,会直接跳转至list页面进行数据插入,将数据插入至oracal数据库
3,跳转页面至listAll,此页面会显示员工的所有信息,包括delete,modifi,和add
4,按个人所需进行曾,查,改,删操作
这个程序使用myecplise
首先写个html文件
<!DOCTYPE html> <html> <head> <title>MyHtml.html</title> <meta name="keywords" content="keyword1,keyword2,keyword3"> <meta name="description" content="this is my page"> <meta name="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body > <form action="list" method="post"> <div > 姓名<input type="text" name="name"><br> 年龄<input type="text" name="age"><br> 工资<input type="text" name="sal"><br> <input type="submit" value="提交"> </div> </form> </body> </html>在这个html中。只有表单,有submit这个提交type,action是用来连接,值就是xml中servlet的url-patten,看下面的xml文件
在这里还需定义一个xml文件。用来作为文件之间的关联。查找的联系是这样的
先在<servlet-mapping>中查找url-mapping,就会有对应的,servler-name,
根据servlet-name,在<servlet>中查找同样的servlet-name,就会获取servlet-class
servlet-class中的内容是这样定义的: 包名.class文件名,
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <servlet> <servlet-name>addemp</servlet-name> <servlet-class>web03.AddEmp</servlet-class> </servlet> <servlet-mapping> <servlet-name>addemp</servlet-name> <url-pattern>/list</url-pattern> </servlet-mapping> <servlet> <servlet-name>listall</servlet-name> <servlet-class>web03.ListAll</servlet-class> </servlet> <servlet-mapping> <servlet-name>listall</servlet-name> <url-pattern>/alllist</url-pattern> </servlet-mapping> <servlet> <servlet-name>delall</servlet-name> <servlet-class>web03.DelEmp</servlet-class> </servlet> <servlet-mapping> <servlet-name>delall</servlet-name> <url-pattern>/alldel</url-pattern> </servlet-mapping> <servlet> <servlet-name>loademp</servlet-name> <servlet-class>web03.LoadEmp</servlet-class> </servlet> <servlet-mapping> <servlet-name>loademp</servlet-name> <url-pattern>/load</url-pattern> </servlet-mapping> <servlet> <servlet-name>modifyemp</servlet-name> <servlet-class>web03.ModifyEmp</servlet-class> </servlet> <servlet-mapping> <servlet-name>modifyemp</servlet-name> <url-pattern>/modify</url-pattern> </servlet-mapping> </web-app>在这个xml文件中。定义了几个操作员工表的操作
1,list 用来插入单条数据
2,load和modify一起使用,用load获取员工的单条记录,然后进行modify操作
3,alldel用来删除员工的单条记录
4,alllist用来获取员工表数据。
接下来,根据前面的html页面会跳转至此下页面。用来插入单条emp数据
public class AllEmp extends HttpServlet { public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html;charset=utf-8"); PrintWriter out = res.getWriter(); String name=req.getParameter("name"); String age=req.getParameter("age"); String sal=req.getParameter("sal"); out.println(name); Connection conn=null; PreparedStatement ps=null; try{ Class.forName("oracle.jdbc.driver.OracleDriver"); conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.202:1521:tarena", "jsd1505", "jsd1505"); ps = conn.prepareStatement("insert into emp_dongruisu(id,name,age,sal) values (id.nextval,?,?,?)" ); ps.setString(1,name); ps.setInt(2,Integer.parseInt(age)); ps.setInt(3,Integer.parseInt(sal)); ps.executeUpdate(); res.sendRedirect("alllist"); }catch(Exception e){ e.printStackTrace(); out.print("错了"); }finally{ if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }此 上面页面的res.sendRedirect(url-pattern),,重定向,会清空之前获取的数据,跳转至url-pattern制定的页面。
接着就是跳转至制定页面,在这里是员工列表页面
public class ListAll extends HttpServlet { public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); res.setContentType("text/html;charset=utf-8"); PrintWriter out=res.getWriter(); Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; try{ Class.forName("oracle.jdbc.driver.OracleDriver"); conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.202:1521:tarena", "jsd1505", "jsd1505"); ps=conn.prepareStatement("select * from emp_dongruisu"); rs=ps.executeQuery(); out.println("<table width='60%' border='1' cellpadding='0' cellspacing='0'>"); out.println("<tr><td>id</td><td>姓名</td><td>年龄</td><td>工资</td><td>操作</td></tr>"); while(rs.next()){ int id=rs.getInt("id"); String name=rs.getString("name"); int age=rs.getInt("age"); int sal=rs.getInt("sal"); out.println("<tr><td>"+id+"</td><td>"+name+"</td><td>"+age+"</td><td>+"+sal+"</td>" +"<td><a href='alldel?id="+id+"'>删除</a>$nbsp;<a href='load?id="+id+"'>修改</a></td></tr>" ); } out.println("</table>"); out.print("<a href='MyHtml.html'>添加员工</a>"); }catch(Exception e){ e.printStackTrace(); }finally{ if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }在这个页面就可进行增,查,改,删这4个操作,根据所选不同,进行不同的操作
接下来是,修改和删除
修改:先要跳转到需要修改单条员工数据的页面,在进行操作修改
public class LoadEmp extends HttpServlet{ public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{ res.setContentType("text/html;charset=utf-8"); PrintWriter out=res.getWriter(); int id=Integer.parseInt(req.getParameter("id")); Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; try{ Class.forName("oracle.jdbc.driver.OracleDriver"); conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.202:1521:tarena", "jsd1505", "jsd1505"); ps= conn.prepareStatement( "select * from emp_dongruisu where id=?" ); ps.setInt(1, id); rs=ps.executeQuery(); if(rs.next()){ String name=rs.getString("name"); int age=rs.getInt("age"); int sal=rs.getInt("sal"); out.println("<form action='modify' method='post'>"); out.println("id:"+id+"<br>"); out.println("姓名:<input name='name' value='"+name+"'><br>"); out.println("年龄:<input name='age' value='"+age+"'><br>"); out.println("工资:<input name='sal' value="+sal+"><br>"); out.println("<input type='hidden' name='id' value='" + id + "'/>"); out.println("<input type='submit' value='提交'>"); out.println("</form>"); } }catch(Exception e){ }finally{ if(conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
public class ModifyEmp extends HttpServlet { public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{ res.setContentType("text/html;charset=utf-8"); PrintWriter out=res.getWriter(); Connection conn=null; PreparedStatement ps=null; try{ Class.forName("oracle.jdbc.driver.OracleDriver"); conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.202:1521:tarena", "jsd1505", "jsd1505"); ps=conn.prepareStatement("update emp_dongruisu set name=?,age=?,sal=? where id=?"); ps.setString(1, req.getParameter("name")); ps.setInt(2,Integer.parseInt(req.getParameter("age"))); ps.setInt(3,Integer.parseInt(req.getParameter("sal"))); ps.setInt(4, Integer.parseInt(req.getParameter("id"))); ps.executeUpdate(); res.sendRedirect("alllist"); }catch(Exception e){ e.printStackTrace(); }finally{ if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
public class DelEmp extends HttpServlet { public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{ res.setContentType("text/html;charset=utf-8"); PrintWriter out=res.getWriter(); int id=Integer.parseInt(req.getParameter("id")); Connection conn=null; PreparedStatement ps=null; try{ Class.forName("oracle.jdbc.driver.OracleDriver"); conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.202:1521:tarena", "jsd1505", "jsd1505"); ps = conn.prepareStatement("delete from emp_dongruisu where id=?" ); ps.setInt(1, id); ps.executeUpdate(); res.sendRedirect("alllist"); }catch(Exception e){ e.printStackTrace(); out.print("wrong"); }finally{ if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }