zoukankan      html  css  js  c++  java
  • jsp导入数据库数据写法(模板)

    1.导入表格模板

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <%@ page import="java.sql.*" %>
    <%
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs","root","root");
        PreparedStatement ps=conn.prepareStatement("select * from article");
        ResultSet rs=ps.executeQuery();
    %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>ShowArticleTree</title>
    </head>
    <body>
    <table border="1">
    <%
        while(rs.next()){
    %>
        <tr>
            <td><%=rs.getInt("id") %></td>
            <td><%=rs.getString("cont") %></td>
        </tr>
    <%
        }
    %>
    </table>
    </body>
    <%
        rs.close();
        ps.close();
        conn.close();
    %>
    </html>

    2.树形利用递归进行遍历算法,求根帖子

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
    <%@ page import="java.sql.*" %>
    <%!
        String str="";
        private void tree(Connection conn,int id,int level){
            PreparedStatement ps=null;
            ResultSet rs=null;
            String preStr="";
            for(int i=0;i<level;i++){
                preStr+="****";
            }
            try{
                String sql="select * from article where pid ="+id;
                ps=conn.prepareStatement(sql);
                rs=ps.executeQuery();
                while(rs.next()){
                    str+="<tr><td>"+rs.getInt("id")+"</td><td>"+preStr+
                        rs.getString("cont")+"</td></tr>";
                    if(rs.getInt("isleaf")!=0){
                        tree(conn,rs.getInt("id"),level+1);
                    }
                }
            }catch(SQLException e){
                e.printStackTrace();
            }finally{
                try{
                    if(rs!=null) {
                        rs.close();
                        rs=null;
                    }
                    if(ps!=null) {
                        ps.close();
                        ps=null;
                    }
                }catch(SQLException e2){
                    e2.printStackTrace();
                }
            }
        }
    %>
    <%
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs","root","root");
        PreparedStatement ps=conn.prepareStatement("select * from article where pid=0");
        ResultSet rs=ps.executeQuery();
        while(rs.next()){
            str+="<tr><td>"+rs.getInt("id")+"</td><td>"+rs.getString("cont")+"</td></tr>";
            if(rs.getInt("isleaf")!=0){
                tree(conn,rs.getInt("id"),1);
            }
        }
        rs.close();
        ps.close();
        conn.close();
    %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>ShowArticleTree</title>
    </head>
    <body>
    <table border="1">
    <%=str%>
    </table>
    </body>
    </html>
  • 相关阅读:
    exe自启动的几种方式
    关于 CShellManager 的作用
    DLL 调用 对话框 以及 如何获取调用dll 应用程序(窗口程序)的窗口句柄
    VC++ 2010 创建高级Ribbon界面详解(4)
    HPU--1221 Fibonacci数列
    取一个数的前几位
    HDU--1875 畅通工程再续
    POJ--2485 Highways
    【模板】HDU--1233 畅通工程
    hdu--1856 More is better
  • 原文地址:https://www.cnblogs.com/littlepage/p/9775638.html
Copyright © 2011-2022 走看看