哦,一不小心现在6点了 吃个饭啥的 就赶紧回实验室搞定培哥的这些有趣的问题,
前面的输出信息就没什么好提的了,老师说过一次了,主要说说后面的删除表。借鉴牟球的思路,主要是出于直接用QurrayALL无法依次的到EMPNO 所以将代码整合到了service服务中。上目录结构
删除需要新建一个servlet并实现sql删除语句 以下为新建的servlet内容 名字为DeletSQL.java
package com.lckiss.action; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class DeletSQL implements Servlet { @Override public void init(ServletConfig config) throws ServletException { } @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { // 从传输过来的链接中获取empno值 String empno = req.getParameter("empno"); // 设置格式,防止乱码 res.setContentType("text/html;charset=GBK"); PrintWriter out = res.getWriter(); // 内部打印,方便检测是否出错 System.out.println(empno); // 传入empno主键,删除其附属值 boolean rs = getConnection(empno); // 打印删除结果 System.out.println(rs); if (rs) { out.println("" + empno + " 已删除成功
"); } else { out.println("失败"); } out.flush(); } public boolean getConnection(String empno) { String url = "jdbc:oracle:thin:@10.1.5.5:1521:orcl"; String user = "你的帐号"; String password = "你的密码"; new oracle.jdbc.driver.OracleDriver(); try { Connection conn = DriverManager.getConnection(url, user, password); String sql = "delete from emp where EMPNO=" + empno + ""; ResultSet rs = conn.createStatement().executeQuery(sql); while (rs.next()) { System.out.println(sql); return true; } conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } @Override public void destroy() { } @Override public ServletConfig getServletConfig() { return null; } @Override public String getServletInfo() { return null; } }
——这是以前的登录类Login
package com.lckiss.action; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class UserLogin implements Servlet { @Override public void destroy() { } @Override public ServletConfig getServletConfig() { return null; } @Override public String getServletInfo() { return null; } @Override public void init(ServletConfig config) throws ServletException { System.out.println("已初始化"); } @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { String name = req.getParameter("name"); String pwd = req.getParameter("pwd"); res.setContentType("text/html;charset=GBK"); PrintWriter out = res.getWriter(); // 内部输出一次看从网页获取的数据是否错误 System.out.println(name); System.out.println(pwd); // 检查数据库登录是否成功 boolean check = getConnection(name, pwd); // 成功为true,反之失败 System.out.println(check); if (check) { // true则执行,否则输出失败 // 表格样式,这里由于经历问题,不考虑div包裹 out.println("编号 , 姓名 , 工作 , HIREDATA , SAL , COMM , DEPTNO.
"); // 数据库相关信息 String url = "jdbc:oracle:thin:@10.1.5.5:1521:orcl"; String user = "你的帐号"; String password = "你的密码"; new oracle.jdbc.driver.OracleDriver(); // 新建一个list集合对象 List list = new ArrayList(); try { // 分别为连接,查询语句,以及返回的结果集 Connection conn = DriverManager.getConnection(url, user, password); String sql = "select * from emp"; ResultSet rs = conn.createStatement().executeQuery(sql); // 按行循环,读完转到下一行 while (rs.next()) { int no = rs.getInt("EMPNO"); String ename = rs.getString("ENAME"); String job = rs.getString("JOB"); Date hiredata = rs.getDate("HIREDATE"); int mgr = rs.getInt("MGR"); int comm = rs.getInt("COMM"); int sal = rs.getInt("SAL"); int deptno = rs.getInt("DEPTNO"); // 以上为所以列名 // 因为list.add中插入这段字符串容易出错,所以单独新建一个字符串 String del = "http://localhost/day_1/del?empno=" + no + ""; list.add(no + " ," + ename + " ," + job + " ," + mgr + " ," + hiredata + " ," + sal + " ," + comm + " ," + deptno + " ," + " 删除"); // 内部输出检查传递的servlet地址是否出错 System.out.println(del); } // 循环list集合,每一个字符串复制给一个object对象,并强制转String并输出到浏览器打印 for (Object c : list) { String l = (String) c; out.println("" + l + "
"); } // 关闭Oracle conn数据流 conn.close(); } catch (SQLException e) { e.printStackTrace(); } // out.println("成功"); } else { out.println("失败"); } out.flush(); } // 验证是否连接成功 public boolean getConnection(String name, String pwd) { String url = "jdbc:oracle:thin:@10.1.5.5:1521:orcl"; String user = "你的帐号"; String password = "你的密码"; new oracle.jdbc.driver.OracleDriver(); try { Connection conn = DriverManager.getConnection(url, user, password); String sql = "select * from emp where ENAME='" + name + "' and JOB ='" + pwd + "'"; ResultSet rs = conn.createStatement().executeQuery(sql); while (rs.next()) { // 关闭Oracle conn数据流 conn.close(); return true; } } catch (SQLException e) { e.printStackTrace(); } return false; } }
最后,好像有人需要servlet的部署方式,截图吧,懒得搬代码了html和servlet
最后的最后结果是这样的
有没有发现什么 ,对的 手抖 删了一行,自作孽不可活
Anr-C 2016.9.18
本站由以下主机服务商提供服务支持:
0条评论