本教程假定您已經(jīng)了解了 JDBC 應(yīng)用程序的工作方式。
注意:
jar 包下載(選擇對(duì)應(yīng)的版本):https://downloads.mysql.com/archives/c-j/
下載后把 mysql-connector-java-<對(duì)應(yīng)版本>-bin.jar 拷貝到 tomcat 下 lib 目錄。
MySQL 8.0 以上版本的數(shù)據(jù)庫(kù)連接有所不同:
1、com.mysql.jdbc.Driver 更換為 com.mysql.cj.jdbc.Driver。
MySQL 8.0 以上版本不需要建立 SSL 連接的,需要顯示關(guān)閉。
最后還需要設(shè)置 CST。
加載驅(qū)動(dòng)與連接數(shù)據(jù)庫(kù)方式如下:
<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"/>
從基本概念下手,讓我們來(lái)創(chuàng)建一個(gè)簡(jiǎn)單的表,并在表中創(chuàng)建幾條記錄。
接下來(lái)我們?cè)?MySQL 中創(chuàng)建 NHOOO 數(shù)據(jù)庫(kù),并創(chuàng)建 websites 數(shù)據(jù)表,表結(jié)構(gòu)如下:
CREATE TABLE `websites` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL DEFAULT '' COMMENT '站點(diǎn)名稱', `url` varchar(255) NOT NULL DEFAULT '', `alexa` int(11) NOT NULL DEFAULT '0' COMMENT 'Alexa 排名', `country` char(10) NOT NULL DEFAULT '' COMMENT '國(guó)家', 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ù)表顯示如下:
接下來(lái)的這個(gè)實(shí)例告訴我們?nèi)绾问褂肑STL SQL標(biāo)簽來(lái)運(yùn)行SQL SELECT語(yǔ)句:
<%@ 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 驅(qū)動(dòng)名及數(shù)據(jù)庫(kù) URL 數(shù)據(jù)庫(kù)的用戶名與密碼,需要根據(jù)自己的設(shè)置 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ù)庫(kù)示例 - 菜鳥教程</h1> <table border="1" width="100%"> <tr> <th>ID</th> <th>站點(diǎn)名</th> <th>站點(diǎn)地址</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>
訪問(wèn)這個(gè)JSP實(shí)例,運(yùn)行結(jié)果如下:
這個(gè)實(shí)例告訴我們?nèi)绾问褂肑STL SQL標(biāo)簽來(lái)運(yùn)行SQL INSERT語(yǔ)句:
<%@ 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 驅(qū)動(dòng)名及數(shù)據(jù)庫(kù) URL 數(shù)據(jù)庫(kù)的用戶名與密碼,需要根據(jù)自己的設(shè)置 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 ('菜鳥教程移動(dòng)站', 'http://m.(cainiaoplus.com)', 5093, 'CN'); </sql:update> <sql:query dataSource="${snapshot}" var="result"> SELECT * from websites; </sql:query> <h1>JSP 數(shù)據(jù)庫(kù)示例 - 菜鳥教程</h1> <table border="1" width="100%"> <tr> <th>ID</th> <th>站點(diǎn)名</th> <th>站點(diǎn)地址</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>
訪問(wèn)這個(gè)JSP實(shí)例,運(yùn)行結(jié)果如下:
這個(gè)實(shí)例告訴我們?nèi)绾问褂肑STL SQL標(biāo)簽來(lái)運(yùn)行SQL DELETE語(yǔ)句:
<%@ 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 驅(qū)動(dòng)名及數(shù)據(jù)庫(kù) URL 數(shù)據(jù)庫(kù)的用戶名與密碼,需要根據(jù)自己的設(shè)置 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ù)庫(kù)示例 - 菜鳥教程</h1> <table border="1" width="100%"> <tr> <th>ID</th> <th>站點(diǎn)名</th> <th>站點(diǎn)地址</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>
訪問(wèn)這個(gè)JSP實(shí)例,運(yùn)行結(jié)果如下:
這個(gè)實(shí)例告訴我們?nèi)绾问褂肑STL SQL標(biāo)簽來(lái)運(yùn)行SQL UPDATE語(yǔ)句:
<%@ 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 驅(qū)動(dòng)名及數(shù)據(jù)庫(kù) URL 數(shù)據(jù)庫(kù)的用戶名與密碼,需要根據(jù)自己的設(shè)置 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ù)庫(kù)示例 - 菜鳥教程</h1> <table border="1" width="100%"> <tr> <th>ID</th> <th>站點(diǎn)名</th> <th>站點(diǎn)地址</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>
訪問(wèn)這個(gè)JSP實(shí)例,運(yùn)行結(jié)果如下: