MYSQL数据库连接
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.io.*,java.util.*,java.sql.*"%> 4 <%@ page import="javax.servlet.http.*,javax.servlet.*" %> 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 6 <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> 7 8 <html> 9 <head> 10 <title>SELECT 操作</title> 11 </head> 12 <body> 13 <!-- 14 JDBC 驱动名及数据库 URL 15 数据库的用户名与密码,需要根据自己的设置 16 useUnicode=true&characterEncoding=utf-8 防止中文乱码 17 --> 18 <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" 19 url="jdbc:mysql://localhost:3306/RUNOOB?useUnicode=true&characterEncoding=utf-8" 20 user="root" password="123456"/> 21 22 <sql:query dataSource="${snapshot}" var="result"> 23 SELECT * from websites; 24 </sql:query> 25 <h1>JSP 数据库实例 - 菜鸟教程</h1> 26 <table border="1" width="100%"> 27 <tr> 28 <th>ID</th> 29 <th>站点名</th> 30 <th>站点地址</th> 31 </tr> 32 <c:forEach var="row" items="${result.rows}"> 33 <tr> 34 <td><c:out value="${row.id}"/></td> 35 <td><c:out value="${row.name}"/></td> 36 <td><c:out value="${row.url}"/></td> 37 </tr> 38 </c:forEach> 39 </table> 40 41 </body> 42 </html>