insert.jsp
| <%@page import="java.sql.Statement"%> |
| <%@page import="java.sql.Connection"%> |
| <%@page import="java.sql.DriverManager"%> |
| <%@ page language="java" contentType="text/html; charset=UTF-8" |
| pageEncoding="UTF-8"%> |
| <% |
| |
| |
| Class.forName("com.mysql.jdbc.Driver"); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/study?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "123456"); |
| |
| String sql = "insert into user(username,password) values('zs','123456')"; |
| |
| Statement stat = conn.createStatement(); |
| |
| |
| |
| |
| |
| |
| int num = stat.executeUpdate(sql); |
| |
| if(num >0){ |
| out.print("增加数据成功"); |
| }else{ |
| out.print("增加数据失败"); |
| } |
| |
| stat.close(); |
| conn.close(); |
| |
| %> |
delete.jsp
| <%@page import="java.sql.Statement"%> |
| <%@page import="java.sql.Connection"%> |
| <%@page import="java.sql.DriverManager"%> |
| <%@ page language="java" contentType="text/html; charset=UTF-8" |
| pageEncoding="UTF-8"%> |
| <% |
| |
| |
| Class.forName("com.mysql.jdbc.Driver"); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/study?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "123456"); |
| |
| String sql = "delete from user where user_id=1"; |
| |
| Statement stat = conn.createStatement(); |
| |
| |
| |
| |
| |
| |
| int num = stat.executeUpdate(sql); |
| |
| if(num >0){ |
| out.print("删除数据成功"); |
| }else{ |
| out.print("删除数据失败"); |
| } |
| |
| stat.close(); |
| conn.close(); |
| |
| %> |
update.jsp
| <%@page import="java.sql.Statement"%> |
| <%@page import="java.sql.Connection"%> |
| <%@page import="java.sql.DriverManager"%> |
| <%@ page language="java" contentType="text/html; charset=UTF-8" |
| pageEncoding="UTF-8"%> |
| <% |
| |
| |
| Class.forName("com.mysql.jdbc.Driver"); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/study?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "123456"); |
| |
| String sql = "update user set password='6543211' where user_id = 2"; |
| |
| Statement stat = conn.createStatement(); |
| |
| |
| |
| |
| |
| |
| int num = stat.executeUpdate(sql); |
| |
| if(num >0){ |
| out.print("更新数据成功"); |
| }else{ |
| out.print("更新数据失败"); |
| } |
| |
| stat.close(); |
| conn.close(); |
| |
| %> |
select.jsp
| <%@page import="java.sql.Statement"%> |
| <%@page import="java.sql.Connection"%> |
| <%@page import="java.sql.DriverManager"%> |
| <%@ page language="java" contentType="text/html; charset=UTF-8" |
| pageEncoding="UTF-8"%> |
| <%@ page import="java.sql.ResultSet" %> |
| |
| <% |
| try { |
| |
| |
| Class.forName("com.mysql.jdbc.Driver"); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/study?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "123456"); |
| |
| String sql = "select * from user"; |
| |
| Statement stat = conn.createStatement(); |
| |
| |
| |
| |
| |
| |
| ResultSet rs = stat.executeQuery(sql); |
| |
| if(rs.next()){ |
| out.println("查询成功"); |
| }else{ |
| out.println("查询失败"); |
| } |
| |
| rs.close(); |
| stat.close(); |
| conn.close(); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| %> |
显示效果更好的select.jsp
页面
| <%@page import="java.sql.Statement"%> |
| <%@page import="java.sql.Connection"%> |
| <%@page import="java.sql.DriverManager"%> |
| <%@ page language="java" contentType="text/html; charset=UTF-8" |
| pageEncoding="UTF-8"%> |
| <%@ page import="java.sql.ResultSet" %> |
| |
| <!DOCTYPE html> |
| <html> |
| <head> |
| <meta charset="UTF-8"> |
| <title>查询结果</title> |
| </head> |
| <body> |
| <table border="1"> |
| <tr> |
| <th>id</th> |
| <th>username</th> |
| <th>password</th> |
| </tr> |
| <% |
| try { |
| |
| Class.forName("com.mysql.jdbc.Driver"); |
| Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/study?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "123456"); |
| |
| String sql = "select * from user"; |
| Statement stat = conn.createStatement(); |
| ResultSet rs = stat.executeQuery(sql); |
| |
| while(rs.next()){ |
| out.println("<tr>"); |
| out.println("<td>" + rs.getInt("id") + "</td>"); |
| out.println("<td>" + rs.getString("username") + "</td>"); |
| out.println("<td>" + rs.getString("password") + "</td>"); |
| out.println("</tr>"); |
| } |
| |
| rs.close(); |
| stat.close(); |
| conn.close(); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| %> |
| </table> |
| </body> |
| </html> |
5.11笔记
标题一、作业
1、要求创建一个数据库叫做company 数据库支持中文;
create database if not exists company charset “utf8”;
2、在company数据库下创建一个员工employees数据表,员工数据表有如下字段:
employ_id 员工编号 整数类型 自动递增的主键
employ_name 员工的姓名
employ_age 员工的年龄
employ_sex 员工的性别
employ_salary 员工的薪资 小数double
employ_department_id 员工部门编号 整数
use company;
| create table if not exists employees( |
| employ_id int primary key auto_increment, |
| employ_name varchar(20), |
| employ_age int, |
| employ_sex varchar(5), |
| employ_salary double, |
| employ_department_id int |
| ); |
3、增加数据(自己增加)
| insert into employees(employ_name,employ_age,employ_sex,employ_salary,employ_department_id) values(“zs”,20,”man”,1000.0,1); |
4、将表中1号部门的所有员工薪资更改为3000.00;
| update employees set employ_salary=3000.00 where employ_department_id = 1; |
5、删除表中年龄在16岁以下的员工信息
| Delete from employees where employ_age <16; |
6、查询表中年龄大于30岁并且薪资小于5000的员工信息
| Select * from employees where employ_age >30 and employ_salary<5000.00; |
7、查询表中每个部门的总人数
| Select employ_department_id,count(*) from employees group by employ_department_id; |
8、查询表中性别为man的所有员工中薪资最高的员工信息
| Select * from employees where employ_sex = “man” order by employees_salary desc limit 1; |
9、查询不同部门的平均薪资avg
| Select employ_department_id,avg(employ_salary) from employees group by employ_department_id; |
10、查询人数最多的前两个部门编号以及部门的人数
| Select employ_department_id,count(*) as total from employees group by employ_department_id order by total desc limit 2; |
二、 JDBC技术
| JDBC技术是Java数据库连接技术,通过JDBC可以实现使用Java程序操作不同的数据库,诸如MySQL、Oracle、SQL Server等等数据库。 |
1、JDBC
操作数据库的步骤
(七步曲)
预备条件:引入依赖 操作MySQL数据库的依赖(包工队)
【注意】依赖属于后端的依赖,前端依赖需要放到webapp
目录下,后端依赖必须放到webappWEB-INFlib
。而且大家还必须注意,前端依赖放到webapp目录下即可引入成功,后端依赖放到lib目录下不算引入成功,还必须进行build path
操作才算引入成功。
(1)加载驱动
(选址)
(2)创建连接
(造桥梁)
(3)在Java中准备一个SQL
语句(物资)
(4)创建小推车
(根据桥梁来创建)
(5)小推车带着SQL语句去MySQL执行,小推车再带着结果返回到Java程序中
(6)Java程序处理
MySQL的返回结果
(7)毁车
炸桥
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现