JavaWeb-06 (项目案例3)

一.项目模糊查询


				
				
<% //编码设置 request.setCharacterEncoding("utf-8"); String strName = request.getParameter("strName"); //sql语句 String sql = "select * from tb_news"; if(strName != null){ //将指定编码的字符串进行解码 通过getBytes("编码") 构造函数new String()编码 strName = new String(strName.getBytes("ISO-8859-1"),"utf-8"); //sql语句拼接模糊查询 sql+=" where ntitle like '%"+strName+"%'"; } System.out.println("模糊查询的关键词为:[sql] "+sql); %>

二.绑定index页面的主题和新闻


					
  •  全部  <% //加载驱动 Class.forName("oracle.jdbc.driver.OracleDriver"); //建立连接 String url = "jdbc:oracle:thin:@localhost:1521:orcl"; Connection conn = DriverManager.getConnection(url, "scott", "123"); //sql String sql = "select * from tb_news_theme"; PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); //sql select count(*) from tb_news_theme int count = 1; while(rs.next()){ count++; out.println(" "+rs.getString(2)+" "); //11 就是总记录数 if(count%11==0){ out.println("
    "); } } %>
<% username = request.getParameter("username"); String tid = request.getParameter("ntid"); if(null!=tid){//说明点击了主题分类 //根据点击的主题分类进行查询 where 条件 sql = "select * from tb_news where ntid = "+Integer.valueOf(tid); }else{//第一次进来,没有点击主题分类 sql = "select * from tb_news";//查询所有新闻 } %>

三.绑定评论,删除评论

<% //定义所谓变量(新闻标题,发布时间,点击量,新闻内容等等) String ntitle = "";//标题 String ndate = "";//发布时间 String nauthor = "";//作者 String ncontent = "";//内容 int ncount = 0;//点击量 //2.获取id String id = request.getParameter("nid"); //转换 int nid = 0; if(null!=id){ nid = Integer.valueOf(id); } //3.获取数据 //加载驱动 Class.forName("oracle.jdbc.driver.OracleDriver"); //建立连接 Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott","123"); //sql语句 String sql = "select * from tb_news where nid = ?"; //将sql语句传入方法返回执行对象 PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, nid); //返回结果集对象 ResultSet rs = ps.executeQuery(); //if改造 if(rs.next()){ ntitle = rs.getString(3); ndate = rs.getString(8); nauthor = rs.getString(4); ncontent = rs.getString(6); ncount = rs.getInt(9); } %>
    <%=ntitle %>                             点击量 <%=ncount %>

    <%=ndate %> <%=nauthor %>
    <%=ncontent %>

    <% //定义一个变量存储指定新闻的评论数 int commentCount = 0; //1.求指定新闻的评论数判断 sql ="select count(*) from tb_comment where cnid = ?"; ps = conn.prepareStatement(sql); ps.setInt(1, nid); rs = ps.executeQuery(); if(rs.next()){ commentCount = rs.getInt(1); } if(commentCount!=0){ //遍历所有的评论 sql ="select * from tb_comment where cnid = ?"; ps = conn.prepareStatement(sql); ps.setInt(1, nid); rs = ps.executeQuery(); while(rs.next()){ sql = "select * from tb_news_users where userid = ?"; ps = conn.prepareStatement(sql); ps.setInt(1, rs.getInt(2)); ResultSet rs2 = ps.executeQuery(); String uname = ""; if(rs2.next()){ uname = rs2.getString(2); } out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); } }else{ out.println(""); } %>
    "); out.println("用户名:"+uname+"
    "); out.println("评论内容:"+rs.getString(5)); out.println("    删除
    "); out.println("评论时间:"+rs.getString(6)+"
    "); out.println("
    "); out.println(" "); out.println("
    暂无评论!

    评 论
    用户名: IP:
//跳转的doComment.jsp页面 <%@page import="java.text.SimpleDateFormat"%> <%@page import="java.util.Date"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.Connection"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% //1.设置编码 request.setCharacterEncoding("utf-8"); //获取数据 //评论编号 int cid = 0;//定义一个变量保存最终的id //链接数据库查询到最大的id 最后+1 //加载驱动 Class.forName("oracle.jdbc.driver.OracleDriver"); //建立连接 String url = "jdbc:oracle:thin:@localhost:1521:orcl"; Connection conn = DriverManager.getConnection(url, "scott", "123"); String sql = "select nvl(max(cid),0) from tb_comment"; PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); if(rs.next()){ cid = rs.getInt(1)+1; } //out.println("cid = "+cid); //用户编号 int cuserid = 0; String username = request.getParameter("username"); //根据用户名查找到该用户的编号 sql = "select * from tb_news_users where username = ?"; ps = conn.prepareStatement(sql); ps.setString(1,username); rs = ps.executeQuery(); if(rs.next()){ cuserid = rs.getInt(1); } //新闻编号 int nid = Integer.valueOf(request.getParameter("nid")); //ip地址 String cip = request.getParameter("cip"); //内容 String ccontent = request.getParameter("ccontent"); //时间 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String cdate = sdf.format(date); //发表评论 sql = "insert into tb_comment values(?,?,?,?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'))"; ps = conn.prepareStatement(sql); ps.setInt(1, cid); ps.setInt(2, cuserid); ps.setInt(3, nid); ps.setString(4, cip); ps.setString(5, ccontent); ps.setString(6, cdate); int n = ps.executeUpdate(); if(n>0){ out.println(""); }else{ out.println(""); } %> //删除评论 <% //删除评论 //设置编码 request.setCharacterEncoding("utf-8"); //获取用户名 String username = request.getParameter("username"); //加载驱动 Class.forName("oracle.jdbc.driver.OracleDriver"); //建立连接 String url = "jdbc:oracle:thin:@localhost:1521:orcl"; Connection conn = DriverManager.getConnection(url, "scott", "123"); int cuserid = 0; String sql = "select * from tb_news_users where username = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, username); ResultSet rs = ps.executeQuery(); if(rs.next()){ cuserid = rs.getInt(1); } //获取cid String id = request.getParameter("cid"); int cid = 0; if(null!=id){ cid = Integer.valueOf(id); } int cid_cuserid = 0; //根据cid进行查找 cuserid int nid = 0;//保存新闻编号 //sql sql = "select * from tb_comment where cid = ?"; ps = conn.prepareStatement(sql); ps.setInt(1, cid); rs = ps.executeQuery(); if(rs.next()){ cid_cuserid = rs.getInt(2); nid = rs.getInt(3); } if(cuserid!=cid_cuserid){//不是当前登录用户所评论的内容 //out.println("不是当前登录用户的评论内容"); out.println(""); }else{ //out.println("是当前登录用户的评论内容"); //根据当前登录的用户以及对应的评论编号进行删除 sql = "delete from tb_comment where cuserid = ? and cid = ?"; ps = conn.prepareStatement(sql); ps.setInt(1, cid_cuserid); ps.setInt(2, cid); int n = ps.executeUpdate(); if(n>0){ out.println(""); }else{ out.println(""); } } %>

你可能感兴趣的:(java,web,java,web)