• 使用jsp链接jdbc数据库并将数据显示出来


    jsp代码:

    <%
    //定义
    String name = null;
    String mima=null;
    String name2 = null;
    String mima2=null;
    String headImgUrl=null;

    String driver = "com.mysql.jdbc.Driver";

    //URL指向要访问的数据库名test1

    String url = "jdbc:mysql://ip:3306/数据库名称";

    //MySQL配置时的用户名

    String user = "数据账号";

    //Java连接MySQL配置时的密码

    String password = "**********";

    //测试sql
    String sqlt1 = "SELECT headImgUrl,headImgUrl as headImgUrl2 from userinfo WHERE province='河北' ORDER BY subscribeTime desc LIMIT 1";
    String sqlt2 = "SELECT headImgUrl,headImgUrl as headImgUrl2 from userinfo WHERE province='河南' ORDER BY subscribeTime desc LIMIT 1";
    String sqlt3 = "SELECT nickname,subscribeTime,headImgUrl from userinfo WHERE province='北京' ORDER BY subscribeTime desc LIMIT 5";
    String sqllb4 = "SELECT nickname,subscribeTime ,headImgUrl from userinfo WHERE province='河北' ORDER BY subscribeTime desc LIMIT 5";
    String sqllb5 = "SELECT nickname,subscribeTime,headImgUrl from userinfo WHERE province='河南' ORDER BY subscribeTime desc LIMIT 5";
    String sqllb6 = "SELECT nickname,subscribeTime,headImgUrl from userinfo WHERE province='北京' ORDER BY subscribeTime desc LIMIT 5";


    try {

    // 1 加载驱动程序

    Class.forName(driver);

    // 2 连接数据库

    Connection connmysql = DriverManager.getConnection(url, user, password);

    // 3 用来执行SQL语句

    Statement statement = connmysql.createStatement();

    // 要执行的SQL语句

    %>
    <!--执行 -->
    <%
    ResultSet rs1 = statement.executeQuery(sqllb5);
    /* ResultSet rs2 = statement.executeQuery(sql2);
    ResultSet rs3 = statement.executeQuery(sql3);
    ResultSet rs4 = statement.executeQuery(sql4);
    ResultSet rs5 = statement.executeQuery(sql5);
    ResultSet rs6 = statement.executeQuery(sql6); */

    //将数据存入result
    List<Map> result = new ArrayList<>();
    while (rs1.next()) {
    Map map = new HashMap();
    name = rs1.getString("nickname");
    mima = rs1.getString("subscribeTime");
    headImgUrl = rs1.getString("headImgUrl");
    map.put("name", name);
    map.put("mima", mima);
    map.put("headImgUrl", headImgUrl);
    result.add(map);
    }
    rs1.close();
    %>

    在这里写jsp循环取数据

    <ul>
    <%
    if (!result.isEmpty()) {
    int i = 0;
    for (; i < result.size(); i++) {
    System.out.print(result);
    String title =result.get(i).get("name").toString();
    String urlpath =result.get(i).get("headImgUrl").toString();
    String timeString =result.get(i).get("mima").toString();
    %>
    <li>
    <p>
    <a
    href=" <%=urlpath %>"
    target="_blank" title="<%=title%>"><%=title%></a>
    </p>
    <%-- <span><%=timeString%></span> --%>
    </li>
    <%
    }
    for (; i < result.size(); i++) {
    %>
    <li style="list-style: none; background: none;">
    <p>
    &nbsp;
    </p>
    </li>
    <%
    }
    }
    %>

    </ul>

    <!--jdbc流关闭 -->
    <%
    /* rs2.close();

    rs3.close();
    rs4.close();
    rs5.close();
    rs6.close(); */
    connmysql.close();
    } catch (ClassNotFoundException e) {
    System.out.println("Sorry,can`t find the Driver!");
    e.printStackTrace();
    } catch (SQLException e) {
    e.printStackTrace();
    } catch (Exception e) {
    e.printStackTrace();
    }

    %>

  • 相关阅读:
    (Toolbar)Android中如何消除Toolbar左边的空白
    (TextView)Android中为TextView赋初始值
    (Edittext)Android中界面中有多个Edittext,如何默认让第二个获取焦点
    (警告)Android中报Custom view `&#215;&#215;&#215;` has setOnTouchListener called on it but does not override performClick警告
    (Toolbar)Android中app:showASAction的值及含义
    个人课程总结
    (list)关于list清空问题的解决
    Ubuntu hive 安装过程中遇到的一些问题
    学习进度——第十七周
    个人课程总结
  • 原文地址:https://www.cnblogs.com/liglacier/p/13452275.html
Copyright © 2020-2023  润新知