JDBC结合JSP使用(1)
1. 添加数据
在jsp页面中添加数据,和在serv中添加数据相似。获得页面中提交的数据以后,把数据保存到数据库表中,JSP的代码如下:
add.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="gb2312"%>
<%
request.setCharacterEncoding("gb2312");
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'add.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<form action="" method="post">
<h1><label>请输入部门信息:</label></h1>
<label>部门号:</label><br/>
<input type="text" name="id"/><br/>
<label>部门名:</label><br/>
<input type="text" name="d_name"/><br/>
<label>部门人数:</label><br/>
<input type="text" name="empnumber"/><br/>
<label>地址:</label><br/>
<input type="text" name="address"/><br/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
<%
Connection conn = null;
PreparedStatement ps = null;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("创建数据库驱动成功!");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank","root","1234");
System.out.println("数据库连接成功!");
String sql = "insert into dept(id,d_name,address,empnumber) values(?,?,?,?)";
ps = conn.prepareStatement(sql);
String id = request.getParameter("id");
String d_name = request.getParameter("d_name");
String address = request.getParameter("address");
int empnumber = Integer.parseInt(request.getParameter("empnumber"));
ps.setString(1,id);
ps.setString(2,d_name);
ps.setString(3,address);
ps.setInt(4,empnumber);
int result = ps.executeUpdate();
if(result == 1)
out.print("插入数据成功!");
else
out.print("插入数据失败,请重新插入!");
}catch(Exception e){
out.println("无法连接数据库,请检查数据库连接是否正确!");
}
%>
2. 显示全部数据
在页面中显示全部数据,也就是要把数据库中的全部数据查询出来,要实现这个功能,需要用到实体类,即数据库和实体对象的映射类。实体类代码如下:
DeptVo.java
package com.cn.vo; public class DeptVo {
private String id;
private String address;
private int empnumber;
private String d_name;
private int d_id; public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getEmpnumber() {
return empnumber;
}
public void setEmpnumber(int empnumber) {
this.empnumber = empnumber;
}
public String getD_name() {
return d_name;
}
public void setD_name(String dName) {
d_name = dName;
}
public int getD_id() {
return d_id;
}
public void setD_id(int dId) {
d_id = dId;
} }
编写好实体类型后,就可以在页面中调用该类,在JSP页面中编写JDBC,连接数据库和查询数据,再用JSTL标签库中的c标签遍历输出数据,使用EL表达式取值。JSP中的代码如下:
showAll.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="com.cn.vo.*" %>
<%@ page import="java.sql.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'showAll.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<%
List<DeptVo> list = new ArrayList<DeptVo>();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank","root","1234");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM dept");
while(rs.next()){
DeptVo deptVo = new DeptVo();
deptVo.setId(rs.getString("id"));
deptVo.setAddress(rs.getString("address"));
deptVo.setD_id(rs.getInt("d_id"));
deptVo.setD_name(rs.getString("d_name"));
deptVo.setEmpnumber(rs.getInt("empnumber"));
list.add(deptVo);
}
request.setAttribute("list",list); //把list集合放入request对象中
}catch(Exception e){
e.printStackTrace();
}
%>
<body>
<table border="1" align="center" width="70%">
<tr>
<td>部门编号</td>
<td>部门地址</td>
<td>部门人数</td>
<td>部门名称</td>
<td>部门id</td>
</tr>
<c:forEach items="${list}" var="list">
<tr>
<td>${list.id }</td>
<td>${list.address }</td>
<td>${list.empnumber }</td>
<td>${list.d_name }</td>
<td>${list.d_id }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
3. 显示单条数据信息
显示单条信息就是根据数据的唯一标示符来查询出单条数据的详细信息。在dept表中,主键d_id的值是自动增长的,不会有重复,可以根据d_id查询出单条数据信息。首先要在页面中输入要查询的d_id值,然后根据d_id来查询数据的详细信息。输入查询条件的JSP文件代码如下:
item.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'item.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<form action="ShowById.jsp" method="post">
<label>请输入部门id:</label><br/><br/>
<input type="text" name="d_id"/><br/><br/>
<input type="submit" value="查找"/>
</form>
</body>
</html>
显示在同一页面中的ShowById.jsp文件的代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="com.cn.vo.*" %>
<%@ page import="java.sql.*" %>
<%@page import="javax.servlet.jsp.tagext.TryCatchFinally"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'ShowById.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<%
int d_id = Integer.parseInt(request.getParameter("d_id"));
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("创建驱动成功!");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank","root","1234");
System.out.println("数据库连接成功!");
String sql = "select * from dept where d_id =?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1,d_id);
rs = pstmt.executeQuery();
while(rs.next()){
DeptVo deptVo = new DeptVo();
deptVo.setId(rs.getString("id"));
deptVo.setAddress(rs.getString("address"));
deptVo.setD_id(rs.getInt("d_id"));
deptVo.setD_name(rs.getString("d_name"));
deptVo.setEmpnumber(rs.getInt("empnumber"));
request.setAttribute("deptVo",deptVo);
System.out.println(deptVo.getD_id());
}
}catch(Exception e){
e.printStackTrace();
}
%>
<body>
<jsp:include flush="true" page="item.jsp"></jsp:include>
<hr/>
<h2>d_id值为<%=d_id%>的数据详细信息</h2>
<table border="1" align="center" width="70%">
<tr>
<td>部门编号</td>
<td>部门地址</td>
<td>部门人数</td>
<td>部门名称</td>
<td>部门id</td>
</tr>
<tr>
<td>${deptVo.id }</td>
<td>${deptVo.address }</td>
<td>${deptVo.empnumber }</td>
<td>${deptVo.d_name }</td>
<td>${deptVo.d_id }</td>
</tr>
</table>
</body>
</html>
4. 修改数据
修改数据的时候,要把修改的数据信息显示到页面中,然后在页面中根据实际情况来修改数据,修改数据成功后,数据库会保存修改后的数据。
先编写输入修改数据条件的页面,代码如下:
updateItem.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'updateItem.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<form action="queryToUpdate.jsp" method="post">
<label>请输入部门id:</label><br/><br/>
<input type="text" name="d_id"/><br/><br/>
<input type="submit" value="查找"/>
</form>
</body>
</html>
上述代码中,form表单的action值是queryToUpdate.jsp,当提交查询时,会打开queryToUpdate.jsp页面,在queryToUpdate.jsp页面中查询出的d_id指向的数据信息,并显示到页面中。queryToUpdate.jsp页面代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="com.cn.vo.*" %>
<%@ page import="java.sql.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'queryToUpdate.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<%
response.setCharacterEncoding("gb2312");
request.setCharacterEncoding("gb2312");
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
int d_id = Integer.parseInt(request.getParameter("d_id"));
Class.forName("com.mysql.jdbc.Driver");
System.out.println("创建驱动成功!");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank","root","1234");
System.out.println("数据库连接成功!");
String sql = "select * from dept where d_id=?";
ps = con.prepareStatement(sql);
ps.setInt(1,d_id);
rs = ps.executeQuery();
while(rs.next()){
DeptVo deptVo = new DeptVo();
deptVo.setAddress(rs.getString("address"));
deptVo.setD_id(rs.getInt("d_id"));
deptVo.setD_name(rs.getString("d_name"));
deptVo.setEmpnumber(rs.getInt("empnumber"));
deptVo.setId(rs.getString("id"));
request.setAttribute("deptVo",deptVo);
System.out.println(deptVo.getD_id());
}
}catch(Exception e){
e.printStackTrace();
}
%>
<form action="update.jsp" method="post">
<h1><label>请输入部门信息:</label></h1>
<label>部门号:</label><br/>
<input type="text" name="id" value="${deptVo.id}"/><br/>
<label>部门名:</label><br/>
<input type="text" name="d_name" value="${deptVo.d_name}"/><br/>
<label>部门人数:</label><br/>
<input type="text" name="empnumber" value="${deptVo.empnumber}"/><br/>
<label>地址:</label><br/>
<input type="text" name="address" value="${deptVo.address}"/><br/>
<label>部门id:</label><br/>
<input type="text" name="d_id" value="${deptVo.d_id}"/><br/><br/>
<input type="submit" value="修改"/>
</form>
</body>
</html>
页面效果如下所示:
在上面的页面中提交修改后,会打开form表单action指定的update.jsp页面,在该页面中处理数据修改,并包含了输入修改条件的页面。update.jsp代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'update.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<%
response.setCharacterEncoding("gb2312");
request.setCharacterEncoding("gb2312");
Connection con = null;
PreparedStatement ps = null;
String id = request.getParameter("id");
String address = request.getParameter("address");
int empnumber = Integer.parseInt(request.getParameter("empnumber"));
String d_name = request.getParameter("d_name");
int d_id = Integer.parseInt(request.getParameter("d_id"));
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("数据库驱动创建成功!");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank","root","1234");
System.out.println("数据库连接成功!");
String sql = "update dept set id=?,address=?,empnumber=?,d_name=?,d_id=? where d_id=?";
ps = con.prepareStatement(sql);
ps.setString(1,id);
ps.setString(2,address);
ps.setInt(3,empnumber);
ps.setString(4,d_name);
ps.setInt(5,d_id);
ps.setInt(6,d_id);
ps.executeUpdate();
out.println("<h1>修改成功!</h1>");
}catch(Exception e){
e.printStackTrace();
}
%>
<body>
<jsp:include flush='true' page='updateItem.jsp'></jsp:include>
</body>
</html>
程序运行后界面如下:
JDBC结合JSP使用(1)的更多相关文章
- JDBC+Servlet+jsp(增删查改)
先在mysql新增数据库和表先,把下面的几句代码复制去到mysql运行就可以创建成功了! 创建数据库 create database jdbc01 character set utf8 collat ...
- JDBC结合JSP使用(2)
5. 删除数据 在删除数据的时候,需要指定删除条件,否则会把数据库表中的数据全部删除.在JSP页面中获得删除条件以后,调用JDBC的删除条件,把数据库表中的数据删除.删除操作的JSP页面代码如下: d ...
- 实例: Java代码操作oracle数据库(JDBC+sevrlet+jsp+html)
1, 注册页面 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.or ...
- JDBC+Servlet+JSP实现基本的增删改查(简易通讯录)
前言: 最近学习JavaWeb的过程中,自己实践练手了几个小项目,目前已经上传到我的Github上https://github.com/Snailclimb/JavaWebProject.目前只上传了 ...
- 简单的员工管理系统(Mysql+jdbc+Servlet+JSP)
员工管理系统 因为学业要求,需要完成一个过关检测,但是因为检测之前没有做好准备,且想到之前用mysql+jdbc+Struts2+bootstrap做成了一个ATM系统(主要有对数据的增删改查操作), ...
- jdbc+servlet+jsp实现登录验证
基础知识准备:sql的增删改查. 新增:insert into 表名称(字段名.....)values(字段名....). 修改:update 表名称 set 字段名="新值" ...
- JDBC+Servlet+JSP的学生案例增删改查
数据库信息传输到页面实现. 先进行学生信息页面展示: 接口IStudentDao public interface IStudentDao { /** * 保存操作 * @param stu 学生对象 ...
- JSP简单访问数据库
Java代码 public class DBHelper { private String driverName; private String url; private String user; p ...
- Java web开发,在一个jsp里放太多java代码的后果,摘自 java web轻量级开发面试教程
现要做一个简单的登录页面,如果用户通过验证,会显示Welcome用户名的欢迎词,反之则返回登录页面让用户再次输入 这部分的完整代码是JSPDemo项目里的login.jsp,下面来分析一下关键代码. ...
随机推荐
- thinkphp5.0返回插入数据id
添加数据后如果需要返回新增数据的自增主键,可以使用getLastInsID方法: Db::name('user')->insert($data); $userId = Db::name('use ...
- memory_get_usage()查看PHP脚本使用内存
memory_get_usage()可以查看当前php使用的内存大小.对于优化算法提高内存使用效率还是很实用的,尤其是对当下的移动端程序. <?php echo memory_get_usage ...
- 网络请求+Gson解析--Retrofit 2
其实内部是封装了Okhttp和Gson解析 public class CourseFragmentAPI { public static void get(String userId, BaseCal ...
- Sqlite可视化工具sqliteman安装(转)
Sqlite可视化工具sqliteman安装 1.安装前准备 系统要求:RedHat 6.9 Qt库版本:4.2及以上 2.安装文件 采用源码方式安装 可用下面地址自行下载 https://s ...
- 【定时任务】Timer
Java原生api Timer类就可以实现简单的定时任务.下面将简单介绍一下Timer. 一.使用 Timer 实现定时任务 具体代码如下. 可以看到我们主要是分三步进行的 1.new Timer() ...
- JAVAEE——宜立方商城11:sso登录注册功能实现、通过token获得用户信息、Ajax跨域请求(jsonp)
1. 学习计划 第十一天: 1.sso注册功能实现 2.sso登录功能实现 3.通过token获得用户信息 4.Ajax跨域请求(jsonp) 2. Sso系统工程搭建 需要创建一个sso服务工程,可 ...
- android studio 汉化包 美化包
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 汉化包 百度云盘 下载地址:https://pan.baidu.com/s/1pLjwy ...
- 渗透测试中的bypass技巧
0x00 前言 许多朋友在渗透测试中因为遇到WAF而束手无策,本人应邀,与godkiller一同写下此文,希望能够对许多朋友的问题有所帮助. 此系列一共分为五篇文章,分别如下: 一.架构层绕过WAF ...
- python调用matlab
官网链接: https://ww2.mathworks.cn/help/matlab/matlab_external/call-user-script-and-function-from-python ...
- C++使用new和不使用new创建对象区别
前言 在使用面向对象的时候,发现使用new和不使用new创建的对象区别还是蛮大的,做个总结: 总结 new创建的是一个指向类对象的指针,需要指针进行接收,一处初始化,多处使用,但是不用new创建的话不 ...