⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sql_query.jsp

📁 使用JSTL的SQL标记访问数据库的典型代码
💻 JSP
字号:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
  <title>JSTL:的使用</title>
</head>
<body bgcolor="#FFFFFF">
创建普通的数据源:<br>
<sql:setDataSource
  var="example"
  driver="com.mysql.jdbc.Driver"
  url="jdbc:mysql://localhost:3306/jspdev?autoReconnect=true"
  user="root"
  password="" 
  scope="request"
/>
第一种查询:<hr>
<sql:query var="query" dataSource="${example}">
    SELECT * FROM contact
</sql:query>
<table border="1">
  <c:forEach var="row" items="${query.rows}">
  <tr>
    <td>Name: <c:out value="${row.userName}"/></td>
    <td>mobile: <c:out value="${row.mobile}"/></td>
  </tr>
  </c:forEach>
</table>
<hr>
第2种查询:<hr>
<sql:query var="query2" sql="SELECT * FROM contact where userName=?" dataSource="${example}">
   <sql:param value="hellking"/>
</sql:query>
<table border="1">
  <c:forEach var="row" items="${query2.rows}">
  <tr>
    <td>Name: <c:out value="${row.userName}"/></td>
    <td>mobile: <c:out value="${row.mobile}"/></td>
  </tr>
  </c:forEach>
</table>
<hr>
第3种查询:<hr>
<sql:query var="query3" dataSource="${example}">
    SELECT * FROM contact where userName=?
    <sql:param value="hellking"/>
</sql:query>
<table border="1">
  <c:forEach var="row" items="${query3.rows}">
  <tr>
    <td>Name: <c:out value="${row.userName}"/></td>
    <td>mobile: <c:out value="${row.mobile}"/></td>
  </tr>
  </c:forEach>
</table>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -