Summary of Usage of Common SQL Tags for Operating Databases in JSP

  • 2021-09-24 23:19:06
  • OfStack

< sql:setDataSource >
Label setting data source

Grammatical structure:


    <sql:setDataSource url="jdbcUrl" driver="driverClassName" user="userName" password="password" [var = "varName"][scope="{page | request | session | application}"] />


< sql:update >
Adding, deleting and modifying labels

Format 1:


<sql:update sql="sqlUpdate" [var="varName"] [scope="{page|request|session|application}"][dataSource="dataSource"] />

eg:


<%@ page contentType="text/html;charset=GBK"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> Test label </title>
</head>
<sql:setDataSource driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"
 user="liky" password="redhat"
 url="jdbc:sqlserver://localhost:1433;DatabaseName=csu" var="db" />
<body>
<!--  Use here update Label creation 1 Table  -->
<sql:transaction dataSource="${db}">
<sql:update var="update" scope="page">
 if exists(select 1 from sysobjects where name='tmp')
 drop table tmp
 
 create table tmp
 (
 id int identity(1,1) primary key,
 name varchar(20),
 pass varchar(20)
 )
</sql:update>
</sql:transaction>
<!--  Use here update Tag to modify the structure of the table and add 1 Column  -->
<sql:transaction dataSource="${db}">
<sql:update var="update" scope="page">
 alter table tmp add age tinyint
</sql:update>
</sql:transaction>
<br>
</body>
</html>

The second format uses the SQL statement as the ontology content


<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<!DOCTYPE html>
<html>
  <head>
    <title>Update Tag add data </title>
  </head>
  <body>
    <%--  Specify a database link URL , JDBC Driver, username, and password  --%>
    <sql:setDataSource url="jdbc:mysql://localhost:3306/javaweb" driver="com.mysql.jdbc.Driver"
        user="root" password="zhangda890126;;"/>
    <%--  Pass update Tag add data  --%>
    <sql:update>
      INSERT INTO user(userid,username,password) VALUES(null,"admin1","root1");
    </sql:update>
  </body>
</html>



< sql:query > Label
Used to query the data in the database
Format 1:


<sql:query sql="sqlQuery" [var="varName"] [scope="{page|request|session|application}"][dataSource="dataSource"]maxRows="" startRow="startRow" />

The second format uses the SQL statement as the ontology content


<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
  <head>
    <title>Query Use of labels </title>
  </head>
  <body>
    <%--  Specify a database link URL , JDBC Driver, username, and password  --%>
    <sql:setDataSource url="jdbc:mysql://localhost:3306/javaweb" driver="com.mysql.jdbc.Driver"
        user="root" password="zhangda890126;;"/>
    <%--  Pass update Tag add data  --%>
    <sql:query var="result">
      SELECT * FROM user
    </sql:query>
    <%--  Show all the data  --%>
    <center>
      <h3> Query all the data </h3>
      <table border="1">
        <tr>
          <td>userID</td>
          <td>userName</td>
          <td>password</td>
        </tr>
        <%--  Use foreach Loop out all the values  --%>
        <c:forEach items="${result.rows}" var = "row">
          <tr>
            <td>${row.userid}</td>
            <td>${row.username}</td>
            <td>${row.password}</td>
          </tr>
        </c:forEach>
      </table>
    </center>
  </body>
</html>


< sql:param > Labels and < sql:dateParam > Label
< sql:param > There are two formats, namely, with ontology content and without ontology content

The format without ontology content is


<sql:param value="value" />

The format with ontology content is


<sql:param>

 Ontology content 

</sql:param>

< sql:dateParam > The format of the label is:


<sql:dateParam value="value" [type="{timestamp|time|date}"] />

If the parameter is related to time and date, use < sql:dateParam > Label


< sql:transaction > Label
Provide a security mechanism when accessing database (transaction processing security mechanism)

The format is:


<sql:transaction [dataSource="dataSource"] [isolation="read_committed|read_uncomited|repeatabl_read|serializable"]>

<sql:update> or <sql:query>

</sql:transaction>

 


Related articles: