JSP 连接数据库

本文最后更新于:2024年3月18日 凌晨

JSP 连接数据库

  • 把 mysql-connector-java-<对应版本>-bin.jar 拷贝到 tomcat 下 lib 目录。
  • MySQL 8.0 以上版本的数据库连接有所不同:
    • com.mysql.jdbc.Driver 更换为 com.mysql.cj.jdbc.Driver
    • MySQL 8.0 以上版本不需要建立 SSL 连接的,需要显示关闭。
    • 最后还需要设置 CST
  • 加载驱动与连接数据库方式如下:
1
2
3
<sql:setDataSource var="snapshot" driver="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
user="root" password="12345"/>

SELECT操作

  • 接下来的这个例子告诉我们如何使用JSTL SQL标签来运行SQL SELECT语句:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<%@ 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/test?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>

INSERT操作

  • 这个例子告诉我们如何使用JSTL SQL标签来运行SQL INSERT语句:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<%@ 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/test?useUnicode=true&characterEncoding=utf-8"
user="root" password="123456"/>
<!--
插入数据。
-->
<sql:update dataSource="${snapshot}" var="result">
INSERT INTO websites (name,url,alexa,country) VALUES ('test', 'http://baidu.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>

DELETE操作

  • 这个例子告诉我们如何使用JSTL SQL标签来运行SQL DELETE语句:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<%@ 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/test?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>

UPDATE操作

  • 这个例子告诉我们如何使用JSTL SQL标签来运行SQL UPDATE语句:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<%@ 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/test?useUnicode=true&characterEncoding=utf-8"
user="root" password="123456"/>

<!--
修改 ID 为 3 的名字:Name改为 Test
-->
<c:set var="SiteId" value="3"/>

<sql:update dataSource="${snapshot}" var="count">
UPDATE websites SET name = 'Test' 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>

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!