「java创建序列号」java生成自增序列号
今天给各位分享java创建序列号的知识,其中也会对java生成自增序列号进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
求java自动生成一个序列号的方法,急急急...
package com.test4;
public class Test7 {
public static void main(String[] args) {
System.out.println(getNum("20100505",3));
}
//假设数据库里有个20100505005的编号
private static String getNum(String firstPart, int len) {
//调用数据库获得20100505005这个编号
String oldNum = "20100505005";
int num = Integer.parseInt(oldNum.replace(firstPart,""));
String numStr = ++num +"";
int length = numStr.length();
for (int i = length; i len; i++) {
numStr = "0"+numStr;
}
return firstPart + numStr;
}
}
如何用JAVA生成注册序列号
平常我们都接触过软件注册,输入序列号、激活码、注册码、授权码;对于这些字符码到底代表什么含义不甚了解,但一般来说,这些字符码中都有几个特点:
1、唯一性,肯定是一个唯一的序列号,否则就会存在滥用的问题。
2、加密性,肯定是经过加密或者混乱的,防止大家自己生成序列号。
3、解密性,软件自身肯定可以解密,否则无法验证合法性。
4、可读性,序列号一般都比较标准,方便书写和记忆,所以一般都为数字和字母。
以下给出简单示例:
[java] view plaincopy
/**
* byte转哈希
* @param b
* @return
*/
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n b.length; n++) {
stmp = Integer.toHexString(b[n] 0xFF);
if (stmp.length() == 1)
hs += ("0" + stmp);
else
hs += stmp;
}
return hs.toUpperCase();
}
/**
* 哈希转byte
* @param b
* @return
*/
public static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException("长度不是偶数");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
JAVA 编程 序列号
//哎....
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import javax.swing.*;
import javax.swing.text.MaskFormatter;
public class CodeTest extends JFrame {
private static final long serialVersionUID = 1L;
private JFormattedTextField formatField = null;
private JButton ba = null;
private String pattern = "AAAAA-AAAAA-AAAAA-AAAAA";
private JLabel code = new JLabel("注册码: ");
private JLabel input = new JLabel("序列号: ");
private JTextField codeField = new JTextField();
private JLabel rel = new JLabel();
public CodeTest(){
init();
}
public void init(){
MaskFormatter mft = CodeTest.getFormatter(pattern);
mft.setPlaceholderCharacter('X');
formatField = new JFormattedTextField();
formatField = new JFormattedTextField();
mft.install(formatField);
this.setBounds(200, 200, 240, 240);
this.setResizable(false);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
formatField.setBounds(30, 30, 180, 25);
ba = new JButton("注册");
ba.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(formatField.getText().length() = 1) {
}
String text = formatField.getText();
long vlong = getCode(text);
code.setText("注册码: (" + vlong + ")");
if((vlong + "").equals(codeField.getText())){
rel.setText("注册成功!");
System.exit(0);
} else {
rel.setText("注册失败!");
}
}
});
codeField.setBounds(30, 85, 180, 25);
code.setBounds(30, 60, 180, 25);
ba.setBounds(30, 125, 180, 25);
rel.setBounds(30, 155, 100, 25);
input.setForeground(Color.RED);
code.setForeground(Color.RED);
rel.setForeground(Color.RED);
input.setBounds(30, 5, 100, 25);
this.add(rel);
this.add(codeField);
this.add(input);
this.add(code);
this.add(ba);
this.add(formatField);
ba.setDefaultCapable(true);
this.setVisible(true);
}
public static void main(String[] args) {
new CodeTest();
}
public static MaskFormatter getFormatter(String pattern){
try {
return new MaskFormatter(pattern);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
public long getCode(String text){
char [] chs = text.toCharArray();
long vlong = 0;
for(char c: chs){
if(c != '-'){
vlong += 199 * (int)c;
}
}
return vlong;
}
}
java创建序列号的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java生成自增序列号、java创建序列号的信息别忘了在本站进行查找喔。
发布于:2022-12-27,除非注明,否则均为
原创文章,转载请注明出处。