当前位置:首页 > JSP教程 > JSP基础教程

JSP+MySQL实例

在本章中,我们将讨论如何使用jsp访问数据库(这里以mysql数据库为例)。并假设您对jdbc应用程序的工作方式有很好的了解。在开始通过jsp进行数据库访问之前,请确保您具有适当的jdbc环境设置以及安装好mysql数据库。

学习本教程的前置知识 -

  • mysql数据安装和简单操作
  • jdbc连接到mysql

如果没有学习过mysql,请参考:mysql教程 。有关如何使用jdbc及其环境设置访问数据库的更多详细信息,可以参阅jdbc教程。

这里,我们从基本概念开始,首先在mysql的testdb数据库中创建一个表:employees,并在该表中创建一些记录,参考以下sql语句:

use testdb;
drop table if exists `employees`;
create table `employees` (
  `id` int(10) unsigned not null auto_increment,
  `name` varchar(64) not null default '',
  `age` int(3) unsigned not null default '0',
  `education` varchar(32) default '' comment '学历',
  `address` varchar(254) default null,
  `salary` float(8,2) unsigned default null,
  primary key (`id`)
) engine=myisam auto_increment=15 default charset=utf8;

-- ----------------------------
-- records of employees
-- ----------------------------
insert into `employees` values ('1', '李小春', '23', '其它', '海口市人民大道1800号', '8900.00');
insert into `employees` values ('2', '张辉', '28', '本科', '广州天河区珠村市场', '15800.98');
insert into `employees` values ('3', '林贤弟', '29', '博士', '广州白云区龙塘村120号', '18990.99');
insert into `employees` values ('4', '王小简', '23', '本科', '海口人民大道1688号', '899.98');
insert into `employees` values ('5', '蔡世杰', '27', '专科', '上海市宝山区联杨路2211弄26号', '15800.00');
insert into `employees` values ('6', '张承龙', '30', '本科', '上海市虹口区虬江路522号', '23000.00');
insert into `employees` values ('7', '李林奕', '26', '本科', '上海市徐汇区漕宝路440号', '32600.00');
insert into `employees` values ('8', '刘皓轩', '28', '研究生', '上海松江九亭立同商务广场', '29000.00');
insert into `employees` values ('9', '周佳豪', '36', '博士', '深圳市宝安区沙井街道办107国道富达工业区b栋', '48000.00');
insert into `employees` values ('10', '陈聪', '23', '本科', '福田区文蔚大厦19层', '9800.00');

打开eclipse创建一个动态web项目:jspmysql, 其项目结构如下所示 -

注意:需要将mysql的连接驱动程序:mysql-connector-java-5.1.40-bin.jar放入到{webapp}/web-info/lib目录下。

1. 查询数据示例

以下示例显示了如何在jsp编程中使用jtsl执行sql select语句。

文件:select.jsp

<%@ page language="java" contenttype="text/html; charset=utf-8"
    pageencoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>jsp+mysql查询数据</title>
</head>
<body>
    <div style="margin: auto; width: 80%">
        <sql:setdatasource var="connection" driver="com.mysql.jdbc.driver"
            url="jdbc:mysql://localhost/testdb?usessl=false&characterencoding=utf8"
            user="root" password="123456" />

        <sql:query datasource="${connection}" var="result">
            select * from employees;
         </sql:query>
        <table border="1" width="100%">
            <tr>
                <th>编号</th>
                <th>名字</th>
                <th>薪水</th>
                <th>地址</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.salary}" /></td>
                    <td><c:out value="${row.address}" /></td>
                </tr>
            </c:foreach>
        </table>
    </div>
</body>
</html>

在编写上面示例代码后,运行项目,打开浏览器访问url: http://localhost:8080/jspmysql/select.jsp , 应该会看到以下结果 -

2. 插入数据示例

以下示例显示了如何在jsp编程中使用jtsl执行sql insert语句。

文件:insert.jsp

<%@ page language="java" contenttype="text/html; charset=utf-8"
    pageencoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>jsp+mysql插入数据</title>
</head>
<body>
    <div style="margin: auto; width: 80%">
        <sql:setdatasource var="connection" driver="com.mysql.jdbc.driver"
            url="jdbc:mysql://localhost/testdb?usessl=false&characterencoding=utf8"
            user="root" password="123456" />

        <sql:update datasource="${connection}" var="count">
         insert into employees(`name`,`age`,`education`,`address`,`salary`) 
         values ('李路路', 24, '本科', '北京朝阳区', 13600);
        </sql:update>

        <sql:query datasource="${connection}" var="result">
            select * from employees;
         </sql:query>
        <table border="1" width="100%">
            <tr>
                <th>编号</th>
                <th>名字</th>
                <th>薪水</th>
                <th>地址</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.salary}" /></td>
                    <td><c:out value="${row.address}" /></td>
                </tr>
            </c:foreach>
        </table>
    </div>
</body>
</html>

在编写上面示例代码后,运行项目,打开浏览器访问url: http://localhost:8080/jspmysql/insert.jsp , 应该会看到以下结果 -

3. 删除数据示例

以下示例显示了如何在jsp编程中使用jtsl执行sql delete语句。

文件:delete.jsp

<%@ page language="java" contenttype="text/html; charset=utf-8"
    pageencoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>jsp+mysql删除数据</title>
</head>
<body>
    <div style="margin: auto; width: 80%">
        <sql:setdatasource var="connection" driver="com.mysql.jdbc.driver"
            url="jdbc:mysql://localhost/testdb?usessl=false&characterencoding=utf8"
            user="root" password="123456" />

        <!-- 执行删除语句 -->
        <sql:update datasource="${connection}" var="count">
            delete from employees where id>5;
        </sql:update>

        <sql:query datasource="${connection}" var="result">
            select * from employees;
         </sql:query>
        <table border="1" width="100%">
            <tr>
                <th>编号</th>
                <th>名字</th>
                <th>薪水</th>
                <th>地址</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.salary}" /></td>
                    <td><c:out value="${row.address}" /></td>
                </tr>
            </c:foreach>
        </table>
    </div>
</body>
</html>

在编写上面示例代码后,运行项目,打开浏览器访问url: http://localhost:8080/jspmysql/delete.jsp , 应该会看到以下结果 -

4. 更新数据示例

以下示例显示了如何在jsp编程中使用jtsl执行sql update语句。

文件:update.jsp

<%@ page language="java" contenttype="text/html; charset=utf-8"
    pageencoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>jsp+mysql更新数据</title>
</head>
<body>
    <div style="margin: auto; width: 80%">
        <sql:setdatasource var="connection" driver="com.mysql.jdbc.driver"
            url="jdbc:mysql://localhost/testdb?usessl=false&characterencoding=utf8"
            user="root" password="123456" />

        <!-- 执行更新语句 -->
         <c:set var = "empid" value = "1"/>
        <sql:update datasource="${connection}" var="count">
            update employees set name='李家诚' where id=?
            <sql:param value = "${empid}" />
        </sql:update>

        <c:set var = "empid" value = "2"/>
        <sql:update datasource = "${connection}" var = "count">
         update employees set salary = '19999.99' where id=?
         <sql:param value = "${empid}" />
        </sql:update>

        <sql:query datasource="${connection}" var="result">
            select * from employees;
         </sql:query>
        <table border="1" width="100%">
            <tr>
                <th>编号</th>
                <th>名字</th>
                <th>薪水</th>
                <th>地址</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.salary}" /></td>
                    <td><c:out value="${row.address}" /></td>
                </tr>
            </c:foreach>
        </table>
    </div>
</body>
</html>

在编写上面示例代码后,运行项目,打开浏览器访问url: http://localhost:8080/jspmysql/update.jsp , 应该会看到以下结果


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!