SQL ----- JDBC 用ID查询某条记录
package demo; import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class Edit
*/
@WebServlet("/edit.do")
public class Edit extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public Edit() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//根据ID查询
String id = request.getParameter("id");//获取参数--需要修改得记录的ID
System.out.println(id); //设置数据库连接参数
String url = "jdbc:mysql://localhost:3306/库名?serverTimezone=UTC";
String user = "用户名";
String password = "密码"; //加载数据库驱动
try {
Class.forName("com.mysql.jdbc.Driver");// 加载数据库的JDBC驱动程序
} catch (ClassNotFoundException e) {
e.printStackTrace();
} Customer customer = new Customer(); try (Connection connection = DriverManager.getConnection(url, user, password)) {//连接数据库 //设置预处理语句,customers为表名
String sql = "SELECT*FROM customers where customerID= ? ";
PreparedStatement statement = connection.prepareStatement(sql);//预处理sql语句
/*
* statement.setInt(1, Integer.parseInt(id));//Integer.parseInt(id)将字符串类型转换成整型
*/
int _id = Integer.parseInt(id);
statement.setInt(1, _id);
ResultSet rs = statement.executeQuery(); while (rs.next()) {
// 将数据库的数据转换成POJO实例
customer.setCustomerID(rs.getInt("CustomerID"));
customer.setCustomerName(rs.getString("CustomerName"));
customer.setContactName(rs.getString("ContactName"));
customer.setAddress(rs.getString("Address"));
customer.setCity(rs.getString("City"));
customer.setPostalCode(rs.getString("PostalCode"));
customer.setCountry(rs.getString("Country")); } rs.close();
statement.close(); } catch (SQLException e) {
e.printStackTrace();
} request.setAttribute("customer", customer);//将获取POJO实例传递到edit界面
request.getRequestDispatcher("edit.jsp").forward(request, response);
} }
SQL ----- JDBC 用ID查询某条记录的更多相关文章
- SQL ------ JDBC 删除指定的某条记录
package demo; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; ...
- mysql 查询一条记录的下一条和上一条记录
如果ID是主键或者有索引,可以直接查找: 方法一: 查询上一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误): select * from tab ...
- mysql_oracle_随机查询几条记录
数据库的随机查询SQL 1. Oracle,随机查询20条 select * from ( select * from 表名 order by dbms_random.value ) where ...
- 【面经】面试官:如何以最高的效率从MySQL中随机查询一条记录?
写在前面 MySQL数据库在互联网行业使用的比较多,有些小伙伴可能会认为MySQL数据库比较小,存储不了很多的数据.其实,这些小伙伴是真的不了解MySQL.MySQL的小不是说使用MySQL存储的数据 ...
- mysql结构相同的三张表查询一条记录\将一张表中的数据插入另外一张表
将一张表中的数据插入另外一张表 1.两张表结构相同 insert into 表1名称 select * from 表2名称 2.两张结构不相同的表 insert into 表1名称(列名1,列名2,列 ...
- sql查询两条记录的时间差
今天突然想到了一个需求,即在一张带有id和time字段的表中,查询相邻时间的时间差. 表的记录如下: 表名为wangxin id是一个不重复的字符串,time是一个时间戳. 现在的需求如下: 比如id ...
- SQL每个用户最后的一条记录
SELECT [ID] ,[UserID] ,[StartDate] ,[EndDate] ,[CreateUser] ,[CreateDate] ,[LastEditUser] ,[LastEdit ...
- mongodb随机查询一条记录的正确方法!
关于从mongodb库中随机取出一条记录的方法的博文很多,其中都提到了下面三种方法: 1.skip过随机数量的记录. DBCursor cursor = coll.find(query); int r ...
- Mysql 按条件排序查询一条记录 top 1 对应Mysql的LIMIT 关键字
项目中需要每次查询一个表中的最新的一条记录,表结构里面有日期字段.只需要显示一条记录. Mysql帮助文档里面的解释 3.6.2. 拥有某个列的最大值的行 任务:找出最贵物品的编号.销售商和价格. 这 ...
随机推荐
- BZOJ 3210: 花神的浇花集会 (切比雪夫距离)
GXZlegend 切比雪夫和曼哈顿距离的互相转化看这里 传送门 CODE #include <bits/stdc++.h> using namespace std; #define LL ...
- 一文学会redis从零到入门
本文参照视屏学习整理:https://www.bilibili.com/video/av16841549/?p=9 相关软件.资料: 基本条件:有虚拟机或相关linux系统,熟悉基本linux操作 本 ...
- i3wm脚本
exec 执行命令 --no-startup-id 有些脚本或者程序不支持启动通知,不加命令,鼠标会长时间空转,60秒左右 exec_always 每次重启i3,使用该命令启动的程序都会重新执行一次, ...
- 【ArcMap】
1.加载图层(1)内容列表中右键添加数据(2)目录列表中拖拽(3)导航中的添加数据 2.编辑要素(1)选中编辑器点击开始编辑(2)在编辑要素中选中要编辑的要素 选择构造工具 执行编辑操作(3)停止编辑 ...
- 微信小程序填坑之旅(1)-app.js中用云开发获取openid,在其他页上用app.globaldata.openid获取为空
参考:小程序如何在其他页面监听globalData中值的变化?https://www.jianshu.com/p/8d1c4626f9a3 原因就是:app.js没执行完时,其他页已经onload了, ...
- 数据结构实验之链表八:Farey序列(SDUT 3331)
#include <bits/stdc++.h> using namespace std; typedef struct node { int data2; int data1;//mu ...
- CodeForces 535C Tavas and Karafs —— 二分
题意:给出一个无限长度的等差数列(递增),每次可以让从l开始的m个减少1,如果某个位置已经是0了,那么可以顺延到下一位减少1,这样的操作最多t次,问t次操作以后从l开始的最长0序列的最大右边界r是多少 ...
- sql语句中where 1=1和 0=1 的作用
sql where 1=1和 0=1 的作用 where 1=1; 这个条件始终为True,在不定数量查询条件情况下,1=1可以很方便的规范语句. 一.不用where 1=1 在多条件查询中的 ...
- Flume-Replicating Channel Selector 单数据源多出口
使用 Flume-1 监控文件变动,Flume-1 使用 Replicating Channel Selector 将变动内容传递给 Flume-2,Flume-2 负责存储到 HDFS.同时 Flu ...
- Docker 数据管理(Volumes)
Docker 容器产生的数据在可写层,如果不通过 docker commit 生成新的镜像,使得数据成为镜像的一部分保存下来,那么当容器删除后,数据自然也就没有了. Docker 提供了三种数据 Mo ...