gpt4 book ai didi

java - 如何通过生成随机电子邮件地址。 SQL Server 中的存储过程或函数

转载 作者:行者123 更新时间:2023-12-01 19:47:30 25 4
gpt4 key购买 nike

我是 T-SQL 新手,我有一个要求,即使用函数或存储过程在 SQL Server 中生成数百万个唯一的随机假电子邮件 ID。

我使用 JAVA 创建了一个程序,可以生成 50 个唯一的电子邮件 ID(如下所述),但我希望在 SQL Server 中也能生成相同的内容。

您能帮助我如何使用 T-SQL 实现吗?任何帮助将不胜感激。

package com.ing.tdm;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.StringTokenizer;

import org.apache.commons.lang3.RandomStringUtils;

public class EmailGenerator {

public static String getEmailDomains(){
String randomElement = "";
String strOfEmailDomains = "aol.com, att.net, comcast.net, facebook.com, gmail.com, gmx.com, googlemail.com, google.com, hotmail.com, hotmail.co.uk, mac.com, me.com, mail.com, msn.com, live.com, sbcglobal.net, verizon.net, yahoo.com, yahoo.co.uk, email.com, fastmail.fm, games.com, gmx.net, hush.com, hushmail.com, icloud.com, iname.com, inbox.com, lavabit.com, love.com , outlook.com, pobox.com, protonmail.com, rocketmail.com, safe-mail.net, wow.com , ygm.com , ymail.com, zoho.com, yandex.com, bellsouth.net, charter.net, cox.net, earthlink.net, juno.com, btinternet.com, virginmedia.com, blueyonder.co.uk, freeserve.co.uk, live.co.uk, ntlworld.com, o2.co.uk, orange.net, sky.com, virgin.net, wanadoo.co.uk, bt.com, sina.com, sina.cn, qq.com, naver.com, hanmail.net, daum.net, nate.com, yahoo.co.jp, yahoo.co.kr, yahoo.co.id, yahoo.co.in, yahoo.com.sg, yahoo.com.ph, 163.com, 126.com, aliyun.com, foxmail.com, hotmail.fr, live.fr, laposte.net, yahoo.fr, wanadoo.fr, orange.fr, gmx.fr, sfr.fr, neuf.fr, free.fr, gmx.de, hotmail.de, live.de, online.de, t-online.de , web.de, yahoo.de, libero.it, virgilio.it, hotmail.it, aol.it, tiscali.it, alice.it, live.it, yahoo.it, email.it, tin.it, poste.it, teletu.it, mail.ru, rambler.ru, yandex.ru, ya.ru, list.ruhotmail.be, live.be, skynet.be, voo.be, tvcablenet.be, telenet.be, hotmail.com.ar, live.com.ar, yahoo.com.ar, fibertel.com.ar, speedy.com.ar, arnet.com.ar, yahoo.com.mx, live.com.mx, hotmail.es,yahoo.com.ar, fibertel.com.ar, speedy.com.ar, arnet.com.ar, yahoo.com.mx, live.com.mx, hotmail.es, hotmail.com.mx, prodigy.net.mx, yahoo.com.br, hotmail.com.br, outlook.com.br, uol.com.br, bol.com.br, terra.com.br, ig.com.br, itelefonica.com.br, r7.com, zipmail.com.br, globo.com, globomail.com, oi.com.br";
StringTokenizer st = new StringTokenizer(strOfEmailDomains, ",");
Random rand = new Random();

List<String> givenList = new ArrayList<String>();

while(st.hasMoreElements()){
String emaildomain = (String) st.nextElement();
givenList.add(emaildomain);
}

if(null!=givenList && givenList.size() > 0){
int randomIndex = rand.nextInt(givenList.size());
randomElement = givenList.get(randomIndex);
}

return randomElement;

}


public static String generateRandomEmail(int length) {
String emailId = "";
String allowedChars = "abcdefghijklmnopqrstuvwxyz" + "1234567890";
String allowedSpecialChars = "_-.";

String temp = RandomStringUtils.random(length, allowedChars);
emailId = (temp.substring(0, temp.length() - 9)
+Character.toString((RandomStringUtils.random(length, allowedSpecialChars)).charAt(0))
+temp.substring(12, 18)
+"@"+getEmailDomains()).replaceAll("\\s","");

System.out.println(emailId);
return emailId;
}

public static void main(String[] args) {
for (int i = 0; i < 50; i++) {
generateRandomEmail(20);
}
}

}

最佳答案

如果您确实想使用 SQL 来执行此操作,请尝试以下方法之一...

DECLARE @randomString VARCHAR(255)
SELECT
@randomString = CONVERT(VARCHAR(255), NEWID())
PRINT @randomString

或者

DECLARE @Length INT = 25
PRINT LEFT(REPLACE(NEWID(), '-', ''), @Length)

关于java - 如何通过生成随机电子邮件地址。 SQL Server 中的存储过程或函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52633332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com