JSP 連接數據庫
jsp 連接數據庫
本教程假定您已經了解了 jdbc 應用程序的工作方式。在您開始學習 jsp 數據庫訪問之前,請訪問 java mysql 連接 來設置相關驅動及配置。
注意:
你可以下載本站提供的 jar 包:
mysql 5 版本:mysql-connector-java-5.1.39-bin.jar
mysql 8 版本:mysql-connector-java-8.0.19.jar
下載后把 mysql-connector-java-<對應版本>-bin.jar 拷貝到 tomcat 下 lib 目錄。
mysql 8.0 以上版本的數據庫連接有所不同:
-
1、com.mysql.jdbc.driver 更換為 com.mysql.cj.jdbc.driver。
-
mysql 8.0 以上版本不需要建立 ssl 連接的,需要顯示關閉。
-
最后還需要設置 cst。
加載驅動與連接數據庫方式如下:
<sql:setdatasource var="snapshot" driver="com.mysql.cj.jdbc.driver" url="jdbc:mysql://localhost:3306/yapf?usessl=false&servertimezone=utc&useunicode=true&characterencoding=utf-8 user="root" password="12345"/>
從基本概念下手,讓我們來創建一個簡單的表,并在表中創建幾條記錄。
創建測試數據
接下來我們在 mysql 中創建 yapf 數據庫,并創建 websites 數據表,表結構如下:
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;
插入一些數據:
insert into `websites` values ('1', 'google', 'https://www.google.cm/', '1', 'usa'), ('2', '淘寶', 'https://www.taobao.com/', '13', 'cn'), ('3', '碩編程', 'http://www.090948.com', '5892', ''), ('4', '微博', 'http://weibo.com/', '20', 'cn'), ('5', 'facebook', 'https://www.facebook.com/', '3', 'usa');
數據表顯示如下:
select操作
接下來的這個例子告訴我們如何使用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 驅動名及數據庫 url 數據庫的用戶名與密碼,需要根據自己的設置 useunicode=true&characterencoding=utf-8 防止中文亂碼 --> <sql:setdatasource var="snapshot" driver="com.mysql.jdbc.driver" url="jdbc:mysql://localhost:3306/yapf?useunicode=true&characterencoding=utf-8" user="root" password="123456"/> <sql:query datasource="${snapshot}" var="result"> select * from websites; </sql:query> <h1>jsp 數據庫實例 - 碩編程</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例子,運行結果如下:
insert操作
這個例子告訴我們如何使用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 驅動名及數據庫 url 數據庫的用戶名與密碼,需要根據自己的設置 useunicode=true&characterencoding=utf-8 防止中文亂碼 --> <sql:setdatasource var="snapshot" driver="com.mysql.jdbc.driver" url="jdbc:mysql://localhost:3306/yapf?useunicode=true&characterencoding=utf-8" user="root" password="123456"/> <!-- 插入數據 --> <sql:update datasource="${snapshot}" var="result"> insert into websites (name,url,alexa,country) values ('碩編程移動站', 'http://m.yapf.com', 5093, 'cn'); </sql:update> <sql:query datasource="${snapshot}" var="result"> select * from websites; </sql:query> <h1>jsp 數據庫實例 - 碩編程</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例子,運行結果如下:
delete操作
這個例子告訴我們如何使用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 驅動名及數據庫 url 數據庫的用戶名與密碼,需要根據自己的設置 useunicode=true&characterencoding=utf-8 防止中文亂碼 --> <sql:setdatasource var="snapshot" driver="com.mysql.jdbc.driver" url="jdbc:mysql://localhost:3306/yapf?useunicode=true&characterencoding=utf-8" user="root" password="123456"/> <!-- 刪除 id 為 11 的數據 --> <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 數據庫實例 - 碩編程</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例子,運行結果如下:
update操作
這個例子告訴我們如何使用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 驅動名及數據庫 url 數據庫的用戶名與密碼,需要根據自己的設置 useunicode=true&characterencoding=utf-8 防止中文亂碼 --> <sql:setdatasource var="snapshot" driver="com.mysql.jdbc.driver" url="jdbc:mysql://localhost:3306/yapf?useunicode=true&characterencoding=utf-8" user="root" password="123456"/> <!-- 修改 id 為 3 的名字:碩編程改為 yapf --> <c:set var="siteid" value="3"/> <sql:update datasource="${snapshot}" var="count"> update websites set name = 'yapf' where id = ? <sql:param value="${siteid}" /> </sql:update> <sql:query datasource="${snapshot}" var="result"> select * from websites; </sql:query> <h1>jsp 數據庫實例 - 碩編程</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例子,運行結果如下: