java 如何在jsp中生成从1000开始的唯一ID序列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5712053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to generate sequence of unique id in jsp starting from 1000
提问by Akshatha
i want code in jsp to generate unique id starting from 1000. Which data type can i use for it, and how to go about it. Can anyone please giude me......
我希望jsp中的代码从1000开始生成唯一的id。我可以使用哪种数据类型,以及如何去做。任何人都可以请指导我......
采纳答案by Jigar Joshi
You can use java.util.Random
, Use nextInt()
method and simply add 1000
to it, if you want it to be starting from 1000
, you can simply take first no as 1000
您可以使用java.util.Random
, UsenextInt()
方法并简单地添加1000
到它,如果您希望它从 开始1000
,您可以简单地将第一个 no 作为1000
Also See
另见
回答by Ken Chan
If you want to generate a random integer within a certain range , you can use the following snippets :
如果要生成某个范围内的随机整数,可以使用以下代码段:
public int generateRandomNumber(int start, int end ){
Random random = new Random();
long fraction = (long) ((end - start + 1 ) * random.nextDouble());
return ((int)(fraction + start));
}
For example , to get a random integer within 1000 and 8888 , you can call generateRandomNumber(1000, 8888);
例如,要获得 1000 和 8888 之间的随机整数,您可以调用 generateRandomNumber(1000, 8888);
If you want to write all the java code inside a JSP (tough I don't suggest this approach too ) , you can create a JSP page like this .You can get a random integer after every refresh.
如果你想在一个 JSP 中编写所有的 java 代码(虽然我也不推荐这种方法),你可以像这样创建一个 JSP 页面。你可以在每次刷新后得到一个随机整数。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.Random"%>
<%!
public int generateRandomNumber(int start, int end ){
Random random = new Random();
long fraction = (long) ((end - start + 1 ) * random.nextDouble());
return ((int)(fraction + start));
}
%>
<!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=ISO-8859-1">
<title>TEST RANDOM NUMBER</title>
</head>
<body>
<h1>Generate Random Number:<%=generateRandomNumber(1000,8888)%></h1>
</body>
</html>
回答by DaveH
These answers all talk about Random numbers - the OP is asking about unique numbers. Using Random numbers, you still get the possibility of duplicates ( although that chance is admittedly small )
这些答案都在谈论随机数 - OP 正在询问唯一数字。使用随机数,你仍然有可能出现重复(虽然这个机会确实很小)
An easy way to get a unique number would be just to have a class that has one static synchronised method that increments a statically declared counter and returns it. Seed the counter to start at 1000.
获取唯一编号的一种简单方法是拥有一个具有一个静态同步方法的类,该方法递增静态声明的计数器并返回它。将计数器设置为从 1000 开始。
I would implement this in a java class, rather than JSP. As Jogar points out earlier, putting raw java in a JSP can soon get out of hand.
我会在 java 类中实现它,而不是在 JSP 中。正如 Jogar 之前指出的那样,将原始 Java 放入 JSP 很快就会失控。
If your application may end up running on more than one JVM ( such as in an application server cluster ) and the number needs to unique across the entire cluster, then this solution won't work. You'll need to use an external counter, such as the database based solution mentioned above.
如果您的应用程序最终可能在多个 JVM 上运行(例如在应用程序服务器集群中),并且该数量需要在整个集群中是唯一的,那么此解决方案将不起作用。您需要使用外部计数器,例如上面提到的基于数据库的解决方案。
回答by Vivek Goel
you can use AUT_INCREMENT feature of db. mysql db to generate unique id .
您可以使用 db 的 AUT_INCREMENT 功能。mysql db 生成唯一 id 。
create table `TableName`(
`v` int UNSIGNED NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`v`)
) Engine='Default' auto_increment=1000 comment='' row_format=Default