Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JSP Database Connection

JSP Database Connection

This tutorial assumes you already understand how JDBC applications work. Before you begin learning JSP database access, please visit Java MySQL Connection to set up the relevant drivers and configuration.

Note:

You can download the jar package provided by this site:

  • MySQL 5 version:mysql-connector-java-5.1.39-bin.jar

  • MySQL 8 version:mysql-connector-java-8.0.19.jar

  • After downloading, copy the mysql-connector-java--bin.jar into the lib directory under tomcat.

    Database connections for MySQL 8.0 and above are different:

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

    
    
    

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


    Create test data

    Next, we create the RUNOOB database in MySQL 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 '站点名称',
    
      `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 some data:

    
    
    INSERT INTO `websites` VALUES ('1', 'Google', 'https://www.google.cm/', '1', 'USA'), ('2', '淘宝', 'https://www.taobao.com/', '13', 'CN'), ('3', 'Runoob Tutorial', 'http://www.runoob.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 run a SQL SELECT 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"%>
    
     
    
    
    
    
    
    SELECT 操作
    
    
    
    
    
    
    
    
    
     
    
    
    
    SELECT * from websites;
    
    
    
    

    JSP 数据库实例 - Runoob Tutorial

    ID 站点名 站点地址

    Accessing this JSP example, the result is as follows:


    INSERT Operation

    This example shows how to use JSTL SQL tags to run a 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"%>
    
     
    
    
    
    
    
    SELECT 操作
    
    
    
    
    
    
    
    
    
    
    
    
    
    INSERT INTO websites (name,url,alexa,country) VALUES ('Runoob Tutorial移动站', 'http://m.runoob.com', 5093, 'CN');
    
    
    
    
    
    SELECT * from websites;
    
    
    
    

    JSP 数据库实例 - Runoob Tutorial

    ID 站点名 站点地址

    Accessing this JSP example, the result is as follows:


    DELETE Operation

    This example shows how to use JSTL SQL tags to run a 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"%>
    
     
    
    
    
    
    
    SELECT 操作
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
      DELETE FROM websites WHERE Id = ?
    
      
    
    
    
    
    
    
    
    SELECT * from websites;
    
    
    
    

    JSP 数据库实例 - Runoob Tutorial

    ID 站点名 站点地址

    Accessing this JSP example, the result is as follows:


    UPDATE Operation

    This example shows how to use JSTL SQL tags to run a 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"%>
    
     
    
    
    
    
    
    SELECT 操作
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
     
    
    
    
      UPDATE websites SET name = 'RUNOOB' WHERE Id = ?
    
      
    
    
    
    
    
    
    
    SELECT * from websites;
    
    
    
    

    JSP 数据库实例 - Runoob Tutorial

    ID 站点名 站点地址

    Accessing this JSP example, the result is as follows: