【Javaweb-05】(绑定数据,查看详情,删除,修改)

本文介绍了JavaWeb中如何通过表单、超链接和JavaScript进行页面跳转并传递参数,展示了如何绑定数据库数据,实现查看详情、删除和修改功能。通过示例代码演示了使用JDBC连接Oracle数据库进行CRUD操作。
摘要由CSDN通过智能技术生成

form表单提交后如需传递参数  2种形式 

    <form action = "1.jsp?password=123" method = "post">
        账号: <input type = "text" name = "username"/>
        <!-- HTMLform标签时  hidden 隐藏域标签 -->
        <!-- 作用:页面传值  不会在页面上显示 -->
        <input type ="hidden" name = "sex" value = "男"/>
        <br/><br/>
        <input type ="submit" value = "登录"/>
    </form>
    
    <hr/>
    <h5>不带参数超链接页面跳转</h5>
    <a href = "1.jsp">跳转到1.jsp页面</a>
    <h5>带参数超链接页面跳转</h5>
    <a href = "1.jsp?hobby=打游戏">跳转到1.jsp页面</a>
    
    <hr/>
    <button type = "button" onclick = "one()">不带参数跳转</button>
    <button type = "button" onclick = "two()">带参数跳转</button>
    <script type="text/javascript">
        function one(){
            //location对象跳转
            location.href = "1.jsp";
        }
        function two(){
            location.href = "1.jsp?age=12";
        }
    
    </script>

我们一般只要进入到一个网站后,发现就会有相应的消息展示在我们面前,那么这个功能就是我们今天要讲的内容之一:绑定数据

<%
                    Connection conn=null;
                    PreparedStatement ps=null;
                    ResultSet rs=null;
                    try{
                        
                        Class.forName("oracle.jdbc.driver.OracleDriver");
                        conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "123");
                        String sql="select * from tb_t281_news";
                        ps=conn.prepareStatement(sql);
                        
                        rs=ps.executeQuery();
                        while(rs.next()){
                            %>
                            <li> <a href="admin_newsDetail.jsp?nid=<%=rs.getInt(1) %>"><%=rs.getString(3)%></a> <span> 作者:<%=rs.getString(4) %>

                                 <a href='admin_eaitNews.jsp?nid=<%=rs.getInt(1) %>'>修改</a>      <a href='#' οnclick='return clickdel(<%=rs.getInt(1)%>)'>删除</a>
                        </span> </li>
                            
                            <% 
                        }
                        
                    }catch(Exception e){
                        e.printStackTrace();
                    }finally{
                        //关闭连接
                        if(conn!=null && conn.isClosed()){
                            conn.close();
                        }
                        if(ps!=null){
                            ps.close();
                        }
                        if(rs!=null){
                            rs.close();
                        }                    
                        }                    
                    
                    %>

查看详情
<%
                        //定义全局变量存储数据库查询的结果  将来可以通过jsp表达式可以在该页面的任意一个位置进行输出
                        String ntitle = "";
                        String nauthor = "";
                        String nsummary = "";
                        String ncontent = "";
                        String ndate = "";
                        int ncount = 0;
                        
                        
                        int nid=0;
                        //1.设置编码格式
                        request.setCharacterEncoding("utf-8");
                        //获取传过来的nid
                        String id=request.getParameter("nid");
                        
                        if(id!=null){
                            nid=Integer.valueOf(id);
                        }
                        
                        Class.forName("oracle.jdbc.driver.OracleDriver");
                        Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "123");
                        String sql="select * from tb_t281_news where nid=? ";
                        PreparedStatement ps=conn.prepareStatement(sql);
                        ps.setInt(1, nid);
                        ResultSet rs=ps.executeQuery();
                        
                        while(rs.next()){
                            ntitle=rs.getString(3);
                            nauthor=rs.getString(4);
                            nsummary=rs.getString(5);
                            ncontent=rs.getString(6);
                            ndate=rs.getString(8);
                            ncount=rs.getInt(9);
                        }
                        %>

得到值后直接嵌套在js代码中

<td align="right"><%=ntitle %></td>

<td align="right">访问量:<%=ncount %></td>

删除
<%
    //设置编码格式
    request.setCharacterEncoding("utf-8");
    
    String id=request.getParameter("nid");
    
    int nid=0;
    if(id!=null){
        nid=Integer.valueOf(id);
    }
    
    
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "123");
    String sql="delete from tb_t281_news where nid="+nid;
    
    PreparedStatement ps=conn.prepareStatement(sql);
    
    int n=ps.executeUpdate();
    
    if(n>0){
        out.println("<script>alert('删除成功'); window.location.href='admin.jsp'</script>");    
    }else {
        out.println("<script>alert('删除失败'); window.location.href='admin.jsp'</script>");    

    }
    

%>

修改
<%
//1.设置编码格式
    request.setCharacterEncoding("utf-8");
//2.获取传过来的值
    String id=request.getParameter("nid");
    int nid=0;
     if(id!=null){
            nid=Integer.valueOf(id);
        }
         int ntid=0;
        String ntidd=request.getParameter("ntid");
        if(ntidd!=null){
            ntid=Integer.valueOf(ntidd);
        }    
        //标题
        String ntitle=request.getParameter("ntitle");
        //作者
        String nauthor=request.getParameter("nauthor");
        //摘要
        String nsummary=request.getParameter("nsummary");
        //内容
        String ncontent=request.getParameter("ncontent");
        //发布时间
        Date date =new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String ndate=sdf.format(date);
        //点击量
        int ncount=0;
        
        Class.forName("oracle.jdbc.driver.OracleDriver");
        //2.建立连接
        String url="jdbc:oracle:thin:@localhost:1521:orcl";
           Connection conn= DriverManager.getConnection(url,"scott", "123");
        //3.编写sql语句
        String sql="update tb_t281_news set ntid=?,ntitle=?,nauthor=?,nsummary=?,ncontent=?, ndate = to_date(?,'yyyy-mm-dd hh24:mi:ss') where nid=?";
        
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setInt(1, ntid);
        ps.setString(2, ntitle);
        ps.setString(3, nauthor);
        ps.setString(4, nsummary);
        ps.setString(5, ncontent);
        ps.setString(6, ndate);
        ps.setInt(7, nid);
        int n=ps.executeUpdate();
        if(n>0){
            out.println("<script>alert('修改成功');location.href='admin.jsp'</script>");
        }else{
            out.println("<script>alert('修改失败');location.href='admin_eaitNews.jsp'</script>");
        }


%>
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值