同步id生成器
程序员文章站
2022-03-19 09:45:27
...
import java.sql.Connection; import org.apache.log4j.Logger; /** * @author rq * */ public class CardIdManager { public static Logger logger = Logger.getLogger(CardIdManager.class); private static CardIdManager cardNum; private Object obj = new Object(); public static CardIdManager getInstance() { if (cardNum == null) { cardNum = new CardIdManager(); } return cardNum; } /** * 同步的方法块获取唯一的商城卡号 * * @param key * @return * @throws Exception */ public long getNextCardNum(String key, Connection con) throws Exception { synchronized (obj) { long result = 0; int num = 0; if (key.length() == 1 && "2".equalsIgnoreCase(key)) { num = 10; } else if (key.length() == 7) { num = 4; } else if (key.length() == 11) { num = 4; key = key.substring(0, 7); } else { logger.error("aa"); throw new Exception("ss"); } long randomNumber = 0; int i = 0; String keyNum; do { i++; randomNumber = Random.getRandomNumber(num); keyNum = key + String.valueOf(randomNumber); logger.warn(i+ "aa" + keyNum); } while (checkNum(keyNum, con)); result = Long.parseLong(key + String.valueOf(randomNumber)); return result; } } /** * 通过查询数据库验证商城卡号是否存在 存在则返回true 反之返回false * * @param cardNum * @return */ private boolean checkNum(String cardNum, Connection con) throws Exception { boolean result = false; // CardMasterDao dao = new CardMasterDao(con); // List<CardMaster> cardList = dao // .findCardMaster("C_ID='" + cardNum + "'"); // if (cardList != null && cardList.size() > 0) { // logger.error(BusinessConfigParameters.W_ACTIVATE_CARD_002); // result = true; // } return result; } public static void main(String[] s) throws Exception { for (int i = 0; i < 10; i++) { System.out.println(CardIdManager.getInstance().getNextCardNum( "15868846882", null)); // System.out.println("======================"); System.out.println(CardIdManager.getInstance().getNextCardNum("2", null)); System.out.println("*******************************"); } } }
import java.util.Date; /** * 随机数生成器 * * @author rq * */ public class Random { private static final String[] aStrRandom = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; /** * 获取num位随机数 * * @param num * @return */ public static long getRandomNumber(int num) { long result = 0; StringBuffer sb = new StringBuffer(); for (int i = 0; i < num; i++) { sb.append((int) (9 * Math.random() + 1)); } result = Long.parseLong(sb.toString()); return result; } public static String getRandomNumberStr(int num, boolean is, int nu) { String result = ""; StringBuffer sb = new StringBuffer(); int base = nu; for (int i = 0; i < num; i++) { if (is && nu == i) { sb.append(" "); nu = nu + base; } sb.append((int) (9 * Math.random() + 1)); } result = sb.toString(); return result; } /** * 获取随机数 * * @param base * @return */ public static int getRandom(int base) { return (int) (base * Math.random() + 1); } /** * 获取num位随机字符 * * @param num * @return */ public static String getRandomString(int num) { String result = ""; for (int i = 0; i < num; i++) { result += aStrRandom[getRandom(aStrRandom.length - 1)]; } return result; } public static void main(String[] q) throws Exception { // Date sDate = new Date(); // String filePath = "D://test//" + new Date().getTime(); // WorkBookManager work = WorkBookManager.getInstance(filePath); // work.initWorkBook(); // work.setMaxRow(1000); // work.setMaxSheetPage(2); // for (int i = 0; i < 1000; i++) { // String cardNum = Random.get3NumStr(String.valueOf(i + 1), 6); // String cardPwd = Random.getRandomNumberStr(17, true, 4); // work.put("1004 " + "001 " + "002 " + cardNum, cardPwd); // } // work.saveFile(); // // ZipUtils.put(filePath, filePath + ".zip"); // Date eDate = new Date(); // System.out // .println("共耗时=>" + (eDate.getTime() - sDate.getTime()) + "毫秒"); System.out.println(getSperator("123456dfvdfvdsavasdfasdf789", 4)); } /** * 获取线程睡眠时间 * * @param num * @return */ public static int getSleepNumber(int num) { return (int) (num * Math.random() + 1); } /** * @param s * @return */ public static String get3NumStr(String s, int num) { int temp = num - s.length(); for (int i = 0; i < temp; i++) { s = "0" + s; } return s; } public static String getSperator(String s, int num) { StringBuffer result = new StringBuffer(); int base = num; for (int i = 0; i < s.length(); i++) { if (i == base) { base += num; result.append(" "); } result.append(s.substring(i, i + 1)); } return result.toString(); } }
上一篇: linux中cd是什么意思?
下一篇: linux的【.】号怎么删除