sql:setDataSource>
標(biāo)簽設(shè)定數(shù)據(jù)源
語法結(jié)構(gòu):
sql:setDataSource url="jdbcUrl" driver="driverClassName" user="userName" password="password" [var = "varName"][scope="{page | request | session | application}"] />
sql:update>
標(biāo)簽進行增刪改的操作
第一種格式:
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>測試標(biāo)簽/title> /head> sql:setDataSource driver="com.microsoft.sqlserver.jdbc.SQLServerDriver" user="liky" password="redhat" url="jdbc:sqlserver://localhost:1433;DatabaseName=csu" var="db" /> body> !-- 這里使用update標(biāo)簽創(chuàng)建一個表 --> 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> !-- 這里使用update標(biāo)簽修改表的結(jié)構(gòu),添加一個列 --> sql:transaction dataSource="${db}"> sql:update var="update" scope="page"> alter table tmp add age tinyint /sql:update> /sql:transaction> br> /body> /html>
第二種格式是將SQL語句作為本體內(nèi)容
%@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標(biāo)簽添加數(shù)據(jù)/title> /head> body> %-- 指定數(shù)據(jù)庫鏈接URL,JDBC驅(qū)動,用戶名和密碼 --%> sql:setDataSource url="jdbc:mysql://localhost:3306/javaweb" driver="com.mysql.jdbc.Driver" user="root" password="zhangda890126;;"/> %-- 通過update標(biāo)簽添加數(shù)據(jù) --%> sql:update> INSERT INTO user(userid,username,password) VALUES(null,"admin1","root1"); /sql:update> /body> /html>
sql:query>標(biāo)簽
用來查詢數(shù)據(jù)庫中的數(shù)據(jù)
第一種格式:
sql:query sql="sqlQuery" [var="varName"] [scope="{page|request|session|application}"][dataSource="dataSource"]maxRows="" startRow="startRow" />
第二種格式是將SQL語句作為本體內(nèi)容
%@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標(biāo)簽的使用/title> /head> body> %-- 指定數(shù)據(jù)庫鏈接URL,JDBC驅(qū)動,用戶名和密碼 --%> sql:setDataSource url="jdbc:mysql://localhost:3306/javaweb" driver="com.mysql.jdbc.Driver" user="root" password="zhangda890126;;"/> %-- 通過update標(biāo)簽添加數(shù)據(jù) --%> sql:query var="result"> SELECT * FROM user /sql:query> %-- 顯示所有的數(shù)據(jù) --%> center> h3>查詢所有的數(shù)據(jù)/h3> table border="1"> tr> td>userID/td> td>userName/td> td>password/td> /tr> %-- 使用foreach循環(huán)輸出所有的值 --%> 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>標(biāo)簽和sql:dateParam>標(biāo)簽
sql:param>有兩種格式即:帶有本體內(nèi)容和不帶本體內(nèi)容的
不帶本體內(nèi)容的格式為
sql:param value="value" />
帶本體內(nèi)容的格式為
sql:param> 本體內(nèi)容 /sql:param>
sql:dateParam>標(biāo)簽的格式為:
sql:dateParam value="value" [type="{timestamp|time|date}"] />
如果參數(shù)與時間和日期有關(guān)的話就使用sql:dateParam>標(biāo)簽
sql:transaction>標(biāo)簽
提供存取數(shù)據(jù)庫時的一種安全機制(事物處理安全機制)
格式為:
sql:transaction [dataSource="dataSource"] [isolation="read_committed|read_uncomited|repeatabl_read|serializable"]> sql:update> or sql:query> /sql:transaction>
標(biāo)簽:開封 臨汾 烏蘭察布 銅川 白銀 自貢 吉林 山南
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《JSP中操作數(shù)據(jù)庫的常用SQL標(biāo)簽用法總結(jié)》,本文關(guān)鍵詞 JSP,中,操作,數(shù)據(jù)庫,的,常用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。