JDBC. 替换所有行中列的值为:

huangapple go评论61阅读模式
英文:

JDBC. Replace in all rows value of the column

问题

Java 11. PostgreSQL.

在数据库中有以下表格

TABLE public.account (
id bigserial NOT NULL,
account_id varchar(100) NOT NULL,
display_name varchar(100) NOT NULL,
is_deleted bool NULL DEFAULT false,
);


这个表格中大约有1000行数据。在代码中,我有一个静态方法,可以返回随机字符串 - Helper.getRandomName()

如何使用JDBC,在这个表格(public.account)的所有行中,将"display_name" 的值替换为 Helper.getRandomName() 的值?
英文:

Java 11. PostgreSQL.

Having following table in db:

TABLE public.account (
	id bigserial NOT NULL,
	account_id varchar(100) NOT NULL,
	display_name varchar(100) NOT NULL,
	is_deleted bool NULL DEFAULT false,
);

There are about 1000 rows in this table. In the code I have a static method, which return random string - Helper.getRandomName()

How, using JDBC, in this table (public.account) for all rows replace "display_name" value with value of Helper.getRandomName()?

答案1

得分: 1

这是一个SQL问题。您需要运行一个更新查询:

UPDATE public.account SET display_name = ?

并将新名称作为参数提供。缺少WHERE子句意味着所有行都将受到影响。

如果您想要逐个更新每一行,那就会更加复杂。您需要执行一个选择语句以找到所有的ID,然后可以使用JDBC准备一批更新操作,为每个ID添加一个WHERE子句。

JDBC只是一个围绕纯SQL执行的轻量级Java包装器。

英文:

This is a SQL question. You need to run an update query:

UPDATE public.account set display_name = ?

And provide the new name as the parameter. The absence of a WHERE clause means that all rows will be affected.

If you want to do this for each row individually, then it's harder. You'll want to do a select statement to find all the IDs, and then you can prepare a batch of updates using JDBC, adding a where clause for each ID.

JDBC is just a thin Java wrapper around plain SQL execution.

huangapple
  • 本文由 发表于 2020年9月10日 21:25:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63830690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定