English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JSP Database Connection

This tutorial assumes that you are already familiar with the way JDBC applications work.

Note:

 jar package download (select the corresponding version): https://downloads.mysql.com/archives/c-j/

After downloading, download the mysql-connector-java-<corresponding version>-Copy bin.jar to the lib directory under tomcat.

MySQL 8The database connection for versions above 8.0 is different:

  • 1,com.mysql.jdbc.Driver Change to com.mysql.cj.jdbc.Driver.

  • MySQL 8For versions above 8.0, SSL connections do not need to be established and should be explicitly closed.

  • You also need to set CST in the end.

The method of loading the driver and connecting to the database is as follows:

<sql:setDataSource var="snapshot" driver="com.mysql.cj.jdbc.Driver">
     url="jdbc:mysql://localhost:3306/w3codebox?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
     user="root" password="12345"/>

Starting from the basic concepts, let's create a simple table and add several records to it.

Create test data

Next, we create the w3Create the codebox database and create the websites table with the following structure:

CREATE TABLE `websites` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(20) NOT NULL DEFAULT '' COMMENT 'Site name',
  `url` varchar(255) NOT NULL DEFAULT '',
  `alexa` int(11) NOT NULL DEFAULT '0' COMMENT 'Alexa ranking',
  `country` char(10) NOT NULL DEFAULT '' COMMENT 'Country',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

Insert some data:

INSERT INTO `websites` VALUES ('1', Google', 'https://www.google.cm/',1', USA'), ('2', 淘宝', 'https://www.taobao.com/',13', CN'), ('3', 基础教程网', 'http://www.oldtoolbag.com',5892',4', 微博', 'http://weibo.com/',20, 'CN'), ('5', Facebook', 'https://www.facebook.com/',3', USA');

The data table is displayed as follows:

SELECT operation

The following example shows how to use JSTL SQL tags to execute SQL SELECT statements:

<%@ 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 Operation</title>
</head>
<body>
<!--
JDBC driver name and database URL 
The username and password of the database need to be set according to your own settings
useUnicode=true&characterEncoding=utf-8 Prevent Chinese character garbling
 -->
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost:3306/w3codebox?useUnicode=true&characterEncoding=utf-8"
     user="root" password="123456"/>
 
<sql:query dataSource="${snapshot}" var="result">
SELECT * from websites;
</sql:query>
<1>JSP Database Example< - Basic Tutorial Website</h1>
<table border="1" width="100%">
<tr>
   <th>ID</th>
   <th>Site Name</th>
   <th>Site Address</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>

Access this JSP instance, the running result is as follows:

    INSERT operation

This example tells us how to use JSTL SQL tags to execute SQL INSERT statements:

<%@ 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 Operation</title>
</head>
<body>
<!--
JDBC driver name and database URL 
The username and password of the database need to be set according to your own settings
useUnicode=true&characterEncoding=utf-8 Prevent Chinese character garbling
 -->
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost:3306/w3codebox?useUnicode=true&characterEncoding=utf-8"
     user="root" password="123456"/>
<!--
Insert data
 -->
<sql:update dataSource="${snapshot}" var="result">
INSERT INTO websites (name,url,alexa,country) VALUES ('Basic Tutorial Website Mobile', 'http://m.oldtoolbag.com', 5093, 'CN');
</sql:update>
<sql:query dataSource="${snapshot}" var="result">
SELECT * from websites;
</sql:query>
<1>JSP Database Example< - Basic Tutorial Website</h1>
<table border="1" width="100%">
<tr>
   <th>ID</th>
   <th>Site Name</th>
   <th>Site Address</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>

Access this JSP instance, the running result is as follows:

    DELETE Operation

This example shows us how to use JSTL SQL tags to run SQL DELETE statements:

<%@ 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 Operation</title>
</head>
<body>
<!--
JDBC driver name and database URL 
The username and password of the database need to be set according to your own settings
useUnicode=true&characterEncoding=utf-8 Prevent Chinese character garbling
 -->
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost:3306/w3codebox?useUnicode=true&characterEncoding=utf-8"
     user="root" password="123456"/>
<!--
Delete ID 11 data
 -->
<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>
<1>JSP Database Example< - Basic Tutorial Website</h1>
<table border="1" width="100%">
<tr>
   <th>ID</th>
   <th>Site Name</th>
   <th>Site Address</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>

Access this JSP instance, the running result is as follows:

UPDATE Operation

This example shows us how to use JSTL SQL tags to run SQL UPDATE statements:

<%@ 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 Operation</title>
</head>
<body>
<!--
JDBC driver name and database URL 
The username and password of the database need to be set according to your own settings
useUnicode=true&characterEncoding=utf-8 Prevent Chinese character garbling
 -->
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost:3306/w3codebox?useUnicode=true&characterEncoding=utf-8"
     user="root" password="123456"/>
<!--
Modify ID to 3 The name: Basic Tutorial Website is changed to w3codebox"
 -->
<c:set var="SiteId" value="3"/>
 
<sql:update dataSource="${snapshot}" var="count">
  UPDATE websites SET name = 'w3codebox' WHERE Id = ?
  <sql:param value="${SiteId}" />
</sql:update>
<sql:query dataSource="${snapshot}" var="result">
SELECT * from websites;
</sql:query>
<1>JSP Database Example< - Basic Tutorial Website</h1>
<table border="1" width="100%">
<tr>
   <th>ID</th>
   <th>Site Name</th>
   <th>Site Address</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>

Access this JSP instance, the running result is as follows: