标签:
package web; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AllEmp extends HttpServlet{ public void service(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{ Connection conn=null; PreparedStatement ps=null; ResultSet rs = null; String uri = request.getRequestURI(); String path = uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf(".")); try { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=Heros","sa","caxa"); if(path.equals("/load")){ ps=conn.prepareStatement("select * from s_emp where name=?"); String name = request.getParameter("name"); ps.setString(1, name); rs=ps.executeQuery(); PrintWriter pw = response.getWriter(); pw.println("<h1>hello</h1>"); if(rs.next()){ pw.println("<form action=‘update.do‘ method=‘post‘ />"); pw.println("name:"+"<input readonly=‘true‘ type=‘text‘ name=‘name‘ "+"value=‘"+name+"‘/>"+"<br>"); pw.println("salary:"+"<input type=‘text‘ name=‘salary‘ "+"value=‘"+rs.getString(2)+"‘/><br>"); pw.println("<input type=‘submit‘ value=‘ok‘/>"); pw.println("</form>"); } } if(path.equals("/list")){ ps=conn.prepareStatement("select * from s_emp"); rs=ps.executeQuery(); PrintWriter pw = response.getWriter(); //pw.println("<h1>add success</h1>"); pw.println("<a href=‘addEmp.html‘ style=‘font-size:30pt;‘>addEmp</a>"); pw.println("<table width=‘60%‘ border=‘1‘ cellpadding=‘2‘ cellspacing=‘0‘ id=‘table1‘>"); while(rs.next()){ pw.println("<tr><td>"+rs.getString(1)+"</td><td>"+rs.getString(2) +"</td><td><a href=‘del.do?name="+rs.getString(1) +"‘>delete</a> <a href=‘load.do?name="+rs.getString(1) +"‘>update</a></td></tr>"); } pw.print("</table>"); } if(path.equals("/add")){ String name = request.getParameter("name"); String salary = request.getParameter("salary"); ps = conn.prepareStatement("insert into s_emp values(?,?)"); ps.setString(1, name); ps.setString(2, salary); int i = ps.executeUpdate(); PrintWriter pw = response.getWriter(); pw.println("<h1>add success</h1>"); response.sendRedirect("list.do"); } if(path.equals("/del")){ ps=conn.prepareStatement("delete from s_emp where name=?"); String name = request.getParameter("name"); ps.setString(1, name); ps.executeUpdate(); PrintWriter pw = response.getWriter(); response.sendRedirect("list.do"); } if(path.equals("/update")){ ps=conn.prepareStatement("update s_emp set salary=? where name=?"); String salary = request.getParameter("salary"); String name = request.getParameter("name"); ps.setString(1, salary); ps.setString(2, name); ps.executeUpdate(); PrintWriter pw = response.getWriter(); response.sendRedirect("list.do"); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ try { if(rs!=null) rs.close(); if(ps!=null) ps.close(); if(conn!=null) conn.close(); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } } } }
标签:
原文地址:http://www.cnblogs.com/hzzhero/p/4743401.html