Easy Tutorial
❮ Jsp Jstl Jstl Core Redirect Tag ❯

JSP Connecting to Database

This tutorial assumes you have a basic understanding of how JDBC applications work. Before you start learning about JSP database access, please visit Java MySQL Connection to set up the necessary drivers and configurations.

>

Note:

You can download the jar package provided by this site:

-

MySQL 5 Version:

-

MySQL 8 Version:

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

Database connection for MySQL 8.0 and above is different:

-

  1. com.mysql.jdbc.Driver should be replaced with com.mysql.cj.jdbc.Driver.

-

MySQL 8.0 and above do not need to establish an SSL connection and need to be explicitly closed.

-

Finally, you also need to set CST.

The method to load the driver and connect to the database is as follows:

&lt;sql:setDataSource var="snapshot" driver="com.mysql.cj.jdbc.Driver"
     url="jdbc:mysql://localhost:3306/tutorialpro?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 insert a few records into it.


Creating Test Data

Next, we will create a tutorialpro database in MySQL and a 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 Rank',
  `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', 'tutorialpro.org', 'http://www.tutorialpro.org', '5892', ''), ('4', '微博', 'http://weibo.com/', '20', 'CN'), ('5', 'Facebook', 'https://www.facebook.com/', '3', 'USA');

The table will display as follows:

<%@ 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>
&lt;!--
JDBC Driver name and database URL 
Database username and password, need to be set according to your own settings
useUnicode=true&characterEncoding=utf-8 to prevent Chinese乱码
 -->
&lt;sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost:3306/tutorialpro?useUnicode=true&characterEncoding=utf-8"
     user="root"  password="123456"/>

&lt;sql:query dataSource="${snapshot}" var="result">
SELECT * from websites;
</sql:query>
<h1>JSP Database Example - tutorialpro.org</h1>
<table border="1" width="100%">
<tr>
   <th>ID</th>
   <th>Site Name</th>
   <th>Site URL</th>
</tr>
&lt;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 example, the result is as follows:


INSERT Operation

This example shows us how to use JSTL SQL tags to run an SQL INSERT statement:

<%@ 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>
&lt;!--
JDBC Driver name and database URL 
Database username and password, need to be set according to your own settings
useUnicode=true&characterEncoding=utf-8 to prevent Chinese乱码
 -->
&lt;sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost:3306/tutorialpro?useUnicode=true&characterEncoding=utf-8"
     user="root"  password="123456"/>
&lt;!--
Insert data
 -->
&lt;sql:update dataSource="${snapshot}" var="result">
INSERT INTO websites (name,url,alexa,country) VALUES ('tutorialpro.org Mobile Site', 'http://m.tutorialpro.org', 5093, 'CN');
</sql:update>
&lt;sql:query dataSource="${snapshot}" var="result">
SELECT * from websites;
</sql:query>
<h1>JSP Database Example - tutorialpro.org</h1>
<table border="1" width="100%">
<tr>
   <th>ID</th>
   <th>Site Name</th>
   <th>Site URL</th>
</tr>
&lt;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 example, the result is as follows:


DELETE Operation

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

<%@ 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>DELETE Operation</title>
</head>
<body>
&lt;!--
JDBC Driver name and database URL 
Database username and password, need to be set according to your own settings
useUnicode=true&characterEncoding=utf-8 to prevent Chinese乱码
 -->
&lt;sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost:3306/tutorialpro?useUnicode=true&characterEncoding=utf-8"
     user="root"  password="123456"/>
&lt;!--
Delete data
 -->
&lt;sql:update dataSource="${snapshot}" var="result">
DELETE FROM websites WHERE id = ?
&lt;sql:param value="${param.id}" />
</sql:update>
&lt;sql:query dataSource="${snapshot}" var="result">
SELECT * from websites;
</sql:query>
<h1>JSP Database Example - tutorialpro.org</h1>
<table border="1" width="100%">
<tr>
   <th>ID</th>
   <th>Site Name</th>
   <th>Site URL</th>
</tr>
&lt;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 example, the result is as follows:


<%@ 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 Database username and password, need to be set according to your configuration useUnicode=true&characterEncoding=utf-8 to prevent Chinese乱码 --> <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/tutorialpro?useUnicode=true&characterEncoding=utf-8" user="root" password="123456"/>

<!-- Delete data with 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 Database Example - tutorialpro.org</h1> <table border="1" width="100%"> <tr> <th>ID</th> <th>Site Name</th> <th>Site URL</th> </tr> <c:forEach var="row" items="${result.rows}"> <tr> <td></td> <td></td> <td></td> </tr> </c:forEach> </table>

</body> </html>


Accessing this JSP example, the output is as follows:

---

## UPDATE Operation

This example demonstrates how to use JSTL SQL tags to execute an SQL UPDATE statement:

<%@ 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 Database username and password, need to be set according to your configuration useUnicode=true&characterEncoding=utf-8 to prevent Chinese乱码 --> <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/tutorialpro?useUnicode=true&characterEncoding=utf-8" user="root" password="123456"/>

<!-- Modify the name of ID 3: change tutorialpro.org to tutorialpro -->

```html
&lt;c:set var="SiteId" value="3"/>

&lt;sql:update dataSource="${snapshot}" var="count">
  UPDATE websites SET name = 'tutorialpro' WHERE Id = ?
  &lt;sql:param value="${SiteId}" />
</sql:update>

&lt;sql:query dataSource="${snapshot}" var="result">
SELECT * from websites;
</sql:query>
<h1>JSP Database Example - tutorialpro.org</h1>
<table border="1" width="100%">
<tr>
   <th>ID</th>
   <th>Site Name</th>
   <th>Site Address</th>
</tr>
&lt;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>

Accessing this JSP example, the output is as follows: ```

❮ Jsp Jstl Jstl Core Redirect Tag ❯