Servlet与Jsp学习笔记--9、JDBC
JDBC连sql server 2005
1、下载sqljdbc_1.2.2828.100_chs
在http://www.microsoft.com/downloads/details.aspx?familyid=C47053EB-3B64-4794-950D-81E1EC91C1BA&displaylang=zh-cn
2、将sqljdbc.jar放入D:"Tomcat"common"lib和D:"Tomcat"webapps"ora"WEB-INF"lib (ora是应用程序目录)
3、server.xml:
<Context path="/ora" docBase="ora" debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/sqlserver" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000" username="wangxin" password="wangxin"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;DatabaseName=ora; "/>
</Context>
...... </Host>
4、web.xml:
<!-- Used by the JSTL database actions -->
<context-param>
<param-name>
javax.servlet.jsp.jstl.sql.dataSource
</param-name>
<param-value>
jdbc/sqlserver
<!-- jdbc:mysql:///test,org.gjt.mm.mysql.Driver -->
</param-value>
</context-param>
5、搞定!
写段代码来测试一下:
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<sql:query var="empDbInfo">
SELECT * FROM Employee
WHERE UserName = ?
<sql:param value="${param.userName}" />
</sql:query>
INSERT
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%--
See if the employee is already defined. If not, insert the
info, else update it.
--%>
<sql:query var="empDbInfo">
SELECT * FROM Employee
WHERE UserName = ?
<sql:param value="${param.userName}" />
</sql:query>
<%--
Deal with the date values: parse the employment date and create a
Date object from it, and create a new variable to hold the current
date.
--%>
<fmt:parseDate value="${param.empDate}" var="parsedEmpDate"
pattern="yyyy-MM-dd" />
<jsp:useBean id="now" class="java.util.Date" />
<c:choose>
<c:when test="${empDbInfo.rowCount == 0}">
<sql:update>
INSERT INTO Employee
(UserName, Password, FirstName, LastName, Dept,
EmpDate, EmailAddr, ModDate)
VALUES(?, ?, ?, ?, ?, ?, ?, ?)
<sql:param value="${param.userName}" />
<sql:param value="${param.password}" />
<sql:param value="${param.firstName}" />
<sql:param value="${param.lastName}" />
<sql:param value="${param.dept}" />
<sql:dateParam value="${parsedEmpDate}" type="date" />
<sql:param value="${param.emailAddr}" />
<sql:dateParam value="${now}" />
</sql:update>
</c:when>
<c:otherwise>
<sql:update>
UPDATE Employee
SET Password = ?,
FirstName = ?,
LastName = ?,
Dept = ?,
EmpDate = ?,
EmailAddr = ?,
ModDate = ?
WHERE UserName = ?
<sql:param value="${param.password}" />
<sql:param value="${param.firstName}" />
<sql:param value="${param.lastName}" />
<sql:param value="${param.dept}" />
<sql:dateParam value="${parsedEmpDate}" type="date" />
<sql:param value="${param.emailAddr}" />
<sql:dateParam value="${now}" />
<sql:param value="${param.userName}" />
</sql:update>
</c:otherwise>
</c:choose>
<%-- Get the new or updated data from the database --%>
<sql:query var="newEmpDbInfo" scope="session">
SELECT * FROM Employee
WHERE UserName = ?
<sql:param value="${param.userName}" />
</sql:query>
<%-- Redirect to the confirmation page --%>
<c:redirect url="confirmation.jsp" />
Search
Form
<html>
<head>
<title>Search in Employee Database</title>
</head>
<body bgcolor="white">
Please enter information about the employee you're looking for.
You can use partial information in all fields.
<form action="find.jsp" method="get">
<table>
<td>First Name:</td>
<td><input type="text" name="firstName">
</td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="lastName">
</td>
</tr>
<tr>
<td>Department:</td>
<td><input type="text" name="dept">
</td>
</tr>
<tr>
<td colspan=2><input type="submit" value="Search"></td>
</tr>
</table>
</form>
</body>
</html>
Find
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<%--
Execute query, with wildcard characters added to the
parameter values used in the search criteria
--%>
<sql:query var="empList" scope="request">
SELECT * FROM Employee
WHERE FirstName LIKE ?
AND LastName LIKE ?
AND Dept LIKE ?
ORDER BY LastName
<sql:param value="%${param.firstName}%" />
<sql:param value="%${param.lastName}%" />
<sql:param value="%${param.dept}%" />
</sql:query>
<jsp:forward page="list.jsp" />
List
<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<html>
<head>
<title>Search Result</title>
</head>
<body bgcolor="white">
<c:choose>
<c:when test="${empList.rowCount == 0}">
Sorry, no employees were found.
</c:when>
<c:otherwise>
The following employees were found:
<p>
<table border="1">
<th>Last Name</th>
<th>First Name</th>
<th>Department</th>
<th>Email Address</th>
<th>Modified</th>
<c:forEach items="${empList.rows}" var="row">
<tr>
<td><c:out value="${row.LastName}" /></td>
<td><c:out value="${row.FirstName}" /></td>
<td><c:out value="${row.Dept}" /></td>
<td><c:out value="${row.EmailAddr}" /></td>
<td><c:out value="${row.ModDate}" /></td>
<td>
<form action="delete.jsp" method="post">
<input type="hidden" name="userName"
value="<c:out value="${row.UserName}" />">
<input type="hidden" name="firstName"
value="<c:out value="${param.firstName}" />">
<input type="hidden" name="lastName"
value="<c:out value="${param.lastName}" />">
<input type="hidden" name="dept"
value="<c:out value="${param.dept}" />">
<input type="submit" value="Delete">
</form>
</td>
</tr>
</c:forEach>
</table>
</c:otherwise>
</c:choose>
</body>
</html>
Update/Delete
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<sql:update>
DELETE FROM Employee
WHERE UserName = ?
<sql:param value="${param.userName}" />
</sql:update>
<c:redirect url="find.jsp">
<c:param name="firstName" value="${param.firstName}" />
<c:param name="lastName" value="${param.lastName}" />
<c:param name="dept" value="${param.dept}" />
</c:redirect>
maxRows
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<html>
<head>
<title>All Employees</title>
</head>
<body bgcolor="white">
<%-- Set number of rows to process --%>
<c:set var="noOfRows" value="2" />
<sql:query var="empList"
sql="SELECT * FROM Employee ORDER BY LastName"
startRow="${param.start}" maxRows="${noOfRows}"
/>
<c:choose>
<c:when test="${empList.rowCount == 0}">
No one seems to work here any more ...
</c:when>
<c:otherwise>
The following people work here:
<p>
<table border="1">
<th>Last Name</th>
<th>First Name</th>
<th>Department</th>
<th>Email Address</th>
<c:forEach items="${empList.rows}" var="row">
<tr>
<td><c:out value="${row.LastName}" /></td>
<td><c:out value="${row.FirstName}" /></td>
<td><c:out value="${row.Dept}" /></td>
<td><c:out value="${row.EmailAddr}" /></td>
</tr>
</c:forEach>
</table>
</c:otherwise>
</c:choose>
<p>
<c:choose>
<c:when test="${param.start > 0}">
<a href="maxrows.jsp?start=<c:out
value="${param.start - noOfRows}" />">
Previous Page</a>
</c:when>
<c:otherwise>
Previous Page
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${empList.limitedByMaxRows}">
<a href="maxrows.jsp?start=<c:out
value="${param.start + noOfRows}" />">
Next Page</a>
</c:when>
<c:otherwise>
Next Page
</c:otherwise>
</c:choose>
</body>
</html>
另一种写法:
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<html>
<head>
<title>All Employees</title>
</head>
<body bgcolor="white">
<%-- Set number of rows to process --%>
<c:set var="noOfRows" value="5" />
<c:if test="${empList == null}">
<sql:query var="empList" scope="session"
sql="SELECT * FROM Employee ORDER BY LastName"
/>
</c:if>
<c:choose>
<c:when test="${empList.rowCount == 0}">
No one seems to work here anymore ...
</c:when>
<c:otherwise>
The following people work here:
<p>
<table border="1">
<th>Last Name</th>
<th>First Name</th>
<th>Department</th>
<th>Email Address</th>
<c:forEach items="${empList.rows}" var="row"
begin="${param.start}" end="${param.start + noOfRows - 1}">
<tr>
<td><c:out value="${row.LastName}" /></td>
<td><c:out value="${row.FirstName}" /></td>
<td><c:out value="${row.Dept}" /></td>
<td><c:out value="${row.EmailAddr}" /></td>
</tr>
</c:forEach>
</table>
</c:otherwise>
</c:choose>
<p>
<c:choose>
<c:when test="${param.start > 0}">
<a href="foreach.jsp?start=<c:out
value="${param.start - noOfRows}" />">
Previous Page</a>
</c:when>
<c:otherwise>
Previous Page
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${param.start + noOfRows < empList.rowCount}">
<a href="foreach.jsp?start=<c:out
value="${param.start + noOfRows}" />">
Next Page</a>
</c:when>
<c:otherwise>
Next Page
</c:otherwise>
</c:choose>
</body>
</html>
目前维护的开源产品:https://gitee.com/475660