zoukankan      html  css  js  c++  java
  • 在jsp页面直接读取mysql数据库显示数据

    闲来无事,学学java,虽说编程语言相通,但是接触一门新知识还是有些疑惑,边学边记录,方便以后温故。

    直接给出代码:

     1 <%@page import="java.sql.ResultSet"%>
     2 <%@page import="com.mysql.jdbc.Statement"%>
     3 <%@page import="java.sql.DriverManager"%>
     4 <%@page import="com.mysql.jdbc.Connection"%>
     5 <%@ page language="java" contentType="text/html; charset=gbk"
     6     pageEncoding="gbk"%>
     7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     8 <html>
     9 <head>
    10 <meta http-equiv="Content-Type" content="text/html; charset=gbk">
    11 <title>Insert title here</title>
    12 </head>
    13 <body>
    14     <%
    15         //加载驱动
    16         Class.forName("com.mysql.jdbc.Driver");
    17         //建立连接
    18         Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/house", "root",
    19                 "123456");
    20         //创建Statement
    21         Statement stm = (Statement) conn.createStatement();
    22         //执行查询
    23         ResultSet rs = stm.executeQuery("select username,pwd from user");
    24     %>
    25     <table border="1" width="300">
    26         <%
    27             //遍历结果
    28             while (rs.next()) {
    29         %>
    30         <tr>
    31             <td><%=rs.getString(1)%></td>
    32             <td><%=rs.getString(2)%></td>
    33         </tr>
    34         <%
    35             }
    36         %>
    37     </table>
    38 </body>
    39 </html>

    要把mysql驱动包放入项目的WEB-INF/lib下面。刚开始写时候没有对Connection和Statement进行强制转换,老是有红色××,所以要注意注意。

  • 相关阅读:
    Linux scp、ssh命令
    Linux ps、top、free、uname命令
    适配器模式
    Linux不能进入图形化界面运行yum不管用
    SpringMVC 测试 mockMVC
    Idea和Git集成,并且Git管理不同的秘钥,idea分别提交项目到GitLab和GitHub
    多线程
    Java中Volatile关键字详解
    Spring事务管理--(二)嵌套事物详解
    jConsole, jVisualvm, btrace 区别和联系
  • 原文地址:https://www.cnblogs.com/luoxiaozhao/p/6952832.html
Copyright © 2011-2022 走看看