码迷,mamicode.com
首页 > 数据库 > 详细

jsp导入数据库数据写法(模板)

时间:2018-10-12 01:18:16      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:html4   遍历   stat   encoding   utf-8   try   写法   back   nal   

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>

 

jsp导入数据库数据写法(模板)

标签:html4   遍历   stat   encoding   utf-8   try   写法   back   nal   

原文地址:https://www.cnblogs.com/littlepage/p/9775638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!