本教程假定您已經了解了 JDBC 應用程序的工作方式。
注意:
jar 包下載(選擇對應的版本):https://downloads.mysql.com/archives/c-j/
下載后把 mysql-connector-java-<對應版本>-bin.jar 拷貝到 tomcat 下 lib 目錄。
MySQL 8.0 以上版本的數(shù)據(jù)庫連接有所不同:
1、com.mysql.jdbc.Driver 更換為 com.mysql.cj.jdbc.Driver。
MySQL 8.0 以上版本不需要建立 SSL 連接的,需要顯示關閉。
最后還需要設置 CST。
加載驅動與連接數(shù)據(jù)庫方式如下:
<sql:setDataSource var="snapshot" driver="com.mysql.cj.jdbc.Driver" url="jdbc:mysql://localhost:3306/NHOOO?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 user="root" password="12345"/>
從基本概念下手,讓我們來創(chuàng)建一個簡單的表,并在表中創(chuàng)建幾條記錄。
接下來我們在 MySQL 中創(chuàng)建 NHOOO 數(shù)據(jù)庫,并創(chuàng)建 websites 數(shù)據(jù)表,表結構如下:
CREATE TABLE `websites` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL DEFAULT '' COMMENT '站點名稱', `url` varchar(255) NOT NULL DEFAULT '', `alexa` int(11) NOT NULL DEFAULT '0' COMMENT 'Alexa 排名', `country` char(10) NOT NULL DEFAULT '' COMMENT '國家', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
插入一些數(shù)據(jù):
INSERT INTO `websites` VALUES ('1', 'Google', 'https://www.google.cm/', '1', 'USA'), ('2', '淘寶', 'https://www.taobao.com/', '13', 'CN'), ('3', '菜鳥教程', '', '5892', ''), ('4', '微博', 'http://weibo.com/', '20', 'CN'), ('5', 'Facebook', 'https://www.facebook.com/', '3', 'USA');
數(shù)據(jù)表顯示如下:
接下來的這個實例告訴我們如何使用JSTL SQL標簽來運行SQL SELECT語句:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.*,java.util.*,java.sql.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <html> <head> <title>SELECT 操作</title> </head> <body> <!-- JDBC 驅動名及數(shù)據(jù)庫 URL 數(shù)據(jù)庫的用戶名與密碼,需要根據(jù)自己的設置 useUnicode=true&characterEncoding=utf-8 防止中文亂碼 --> <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/nhooo?useUnicode=true&characterEncoding=utf-8" user="root" password="123456"/> <sql:query dataSource="${snapshot}" var="result"> SELECT * from websites; </sql:query> <h1>JSP 數(shù)據(jù)庫示例 - 菜鳥教程</h1> <table border="1" width="100%"> <tr> <th>ID</th> <th>站點名</th> <th>站點地址</th> </tr> <c:forEach var="row" items="${result.rows}"> <tr> <td><c:out value="${row.id}"/></td> <td><c:out value="${row.name}"/></td> <td><c:out value="${row.url}"/></td> </tr> </c:forEach> </table> </body> </html>
訪問這個JSP實例,運行結果如下:
這個實例告訴我們如何使用JSTL SQL標簽來運行SQL INSERT語句:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.*,java.util.*,java.sql.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <html> <head> <title>SELECT 操作</title> </head> <body> <!-- JDBC 驅動名及數(shù)據(jù)庫 URL 數(shù)據(jù)庫的用戶名與密碼,需要根據(jù)自己的設置 useUnicode=true&characterEncoding=utf-8 防止中文亂碼 --> <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/nhooo?useUnicode=true&characterEncoding=utf-8" user="root" password="123456"/> <!-- 插入數(shù)據(jù) --> <sql:update dataSource="${snapshot}" var="result"> INSERT INTO websites (name,url,alexa,country) VALUES ('菜鳥教程移動站', 'http://m.(cainiaoplus.com)', 5093, 'CN'); </sql:update> <sql:query dataSource="${snapshot}" var="result"> SELECT * from websites; </sql:query> <h1>JSP 數(shù)據(jù)庫示例 - 菜鳥教程</h1> <table border="1" width="100%"> <tr> <th>ID</th> <th>站點名</th> <th>站點地址</th> </tr> <c:forEach var="row" items="${result.rows}"> <tr> <td><c:out value="${row.id}"/></td> <td><c:out value="${row.name}"/></td> <td><c:out value="${row.url}"/></td> </tr> </c:forEach> </table> </body> </html>
訪問這個JSP實例,運行結果如下:
這個實例告訴我們如何使用JSTL SQL標簽來運行SQL DELETE語句:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.*,java.util.*,java.sql.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <html> <head> <title>SELECT 操作</title> </head> <body> <!-- JDBC 驅動名及數(shù)據(jù)庫 URL 數(shù)據(jù)庫的用戶名與密碼,需要根據(jù)自己的設置 useUnicode=true&characterEncoding=utf-8 防止中文亂碼 --> <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/nhooo?useUnicode=true&characterEncoding=utf-8" user="root" password="123456"/> <!-- 刪除 ID 為 11 的數(shù)據(jù) --> <sql:update dataSource="${snapshot}" var="count"> DELETE FROM websites WHERE Id = ? <sql:param value="${11}" /> </sql:update> <sql:query dataSource="${snapshot}" var="result"> SELECT * from websites; </sql:query> <h1>JSP 數(shù)據(jù)庫示例 - 菜鳥教程</h1> <table border="1" width="100%"> <tr> <th>ID</th> <th>站點名</th> <th>站點地址</th> </tr> <c:forEach var="row" items="${result.rows}"> <tr> <td><c:out value="${row.id}"/></td> <td><c:out value="${row.name}"/></td> <td><c:out value="${row.url}"/></td> </tr> </c:forEach> </table> </body> </html>
訪問這個JSP實例,運行結果如下:
這個實例告訴我們如何使用JSTL SQL標簽來運行SQL UPDATE語句:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.*,java.util.*,java.sql.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <html> <head> <title>SELECT 操作</title> </head> <body> <!-- JDBC 驅動名及數(shù)據(jù)庫 URL 數(shù)據(jù)庫的用戶名與密碼,需要根據(jù)自己的設置 useUnicode=true&characterEncoding=utf-8 防止中文亂碼 --> <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/nhooo?useUnicode=true&characterEncoding=utf-8" user="root" password="123456"/> <!-- 修改 ID 為 3 的名字:菜鳥教程改為 nhooo --> <c:set var="SiteId" value="3"/> <sql:update dataSource="${snapshot}" var="count"> UPDATE websites SET name = 'nhooo' WHERE Id = ? <sql:param value="${SiteId}" /> </sql:update> <sql:query dataSource="${snapshot}" var="result"> SELECT * from websites; </sql:query> <h1>JSP 數(shù)據(jù)庫示例 - 菜鳥教程</h1> <table border="1" width="100%"> <tr> <th>ID</th> <th>站點名</th> <th>站點地址</th> </tr> <c:forEach var="row" items="${result.rows}"> <tr> <td><c:out value="${row.id}"/></td> <td><c:out value="${row.name}"/></td> <td><c:out value="${row.url}"/></td> </tr> </c:forEach> </table> </body> </html>
訪問這個JSP實例,運行結果如下: