English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Questo tutorial presuppone che tu abbia già familiarità con il funzionamento delle applicazioni JDBC.
Attenzione:
Download del pacchetto jar (seleziona la versione corretta): https://downloads.mysql.com/archives/c-j/
Dopo il download, copia mysql-connector-java-<corrispondente versione>-bin.jar nella directory lib di Tomcat.
La connessione al database di MySQL 8.0 e versioni superiori è diversa:
1、com.mysql.jdbc.Driver Sostituisci con com.mysql.cj.jdbc.Driver.
Le versioni superiori a MySQL 8.0 non richiedono una connessione SSL e devono essere chiaramente disattivate.
Infine, è necessario impostare il CST.
Ecco come caricare il driver e connettersi al database:
<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"/>
Partendo dai concetti di base, creiamo una tabella semplice e inseriamo alcuni record al suo interno.
Successivamente, creiamo il database w3codebox in MySQL e creiamo la tabella websites, la struttura è la seguente:
CREATE TABLE `websites` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL DEFAULT '' COMMENT 'Nome del sito', `url` varchar(255) NOT NULL DEFAULT '', `alexa` int(11) NOT NULL DEFAULT '0' COMMENT 'Posizione Alexa', `country` char(10) NOT NULL DEFAULT '' COMMENT 'Nazione', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
Inserire alcuni dati:
INSERT INTO `websites` VALUES ('1', 'Google', 'https://www.google.cm/', '1', 'USA'), ('2', '淘宝', 'https://www.taobao.com/', '13', 'CN'), ('3', '基础教程网', 'http://it.oldtoolbag.com', '5892', ''), ('4', '微博', 'http://weibo.com/', '20', 'CN'), ('5', 'Facebook', 'https://www.facebook.com/', '3', 'USA');
La tabella dei dati è visualizzata come segue:
Il seguente esempio ci mostra come utilizzare i tag SQL JSTL per eseguire una query 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>Operazione SELECT</title> </head> <body> <!-- Nome del driver JDBC e URL del database Il nome utente e la password del database devono essere configurati in base alle tue impostazioni useUnicode=true&characterEncoding=utf-8 per prevenire la confusione dei caratteri cinesi --> <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> <h1>Esempio di database JSP - oldtoolbag.com</h1> <table border="1" width="100%"> <tr> <th>ID</th> <th>Nome del sito</th> <th>Indirizzo del sito</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>
Accedi a questa istanza di JSP, i risultati di esecuzione sono i seguenti:
Questo esempio ci mostra come utilizzare i tag SQL JSTL per eseguire una query 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>Operazione SELECT</title> </head> <body> <!-- Nome del driver JDBC e URL del database Il nome utente e la password del database devono essere configurati in base alle tue impostazioni useUnicode=true&characterEncoding=utf-8 per prevenire la confusione dei caratteri cinesi --> <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/w3codebox?useUnicode=true&characterEncoding=utf-8" user="root" password="123456"/> <!-- Inserimento dei dati --> <sql:update dataSource="${snapshot}" var="result"> INSERT INTO websites (name, url, alexa, country) VALUES ('基础教程网移动站', 'http://m.oldtoolbag.com', 5093, 'CN'); </sql:update> <sql:query dataSource="${snapshot}" var="result"> SELECT * from websites; </sql:query> <h1>Esempio di database JSP - oldtoolbag.com</h1> <table border="1" width="100%"> <tr> <th>ID</th> <th>Nome del sito</th> <th>Indirizzo del sito</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>
Accedi a questa istanza di JSP, i risultati di esecuzione sono i seguenti:
Questo esempio ci mostra come utilizzare i tag SQL JSTL per eseguire una query 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>Operazione SELECT</title> </head> <body> <!-- Nome del driver JDBC e URL del database Il nome utente e la password del database devono essere configurati in base alle tue impostazioni useUnicode=true&characterEncoding=utf-8 per prevenire la confusione dei caratteri cinesi --> <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/w3codebox?useUnicode=true&characterEncoding=utf-8" user="root" password="123456"/> <!-- Elimina i dati con 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>Esempio di database JSP - oldtoolbag.com</h1> <table border="1" width="100%"> <tr> <th>ID</th> <th>Nome del sito</th> <th>Indirizzo del sito</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>
Accedi a questa istanza di JSP, i risultati di esecuzione sono i seguenti:
Questo esempio ci mostra come utilizzare i tag SQL JSTL per eseguire una query 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>Operazione SELECT</title> </head> <body> <!-- Nome del driver JDBC e URL del database Il nome utente e la password del database devono essere configurati in base alle tue impostazioni useUnicode=true&characterEncoding=utf-8 per prevenire la confusione dei caratteri cinesi --> <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/w3codebox?useUnicode=true&characterEncoding=utf-8" user="root" password="123456"/> <!-- Modifica il nome ID 3: da base教程网 a 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> <h1>Esempio di database JSP - oldtoolbag.com</h1> <table border="1" width="100%"> <tr> <th>ID</th> <th>Nome del sito</th> <th>Indirizzo del sito</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>
Accedi a questa istanza di JSP, i risultati di esecuzione sono i seguenti: