您的位置:首页 > 其它

如何实现字符串类型的ID自动增长

2018-02-01 15:31 441 查看
首先查找数据库中是否有暑假,没有设置为0001,否则获取 例如 GQ201802010003 的最后4位数字转成int数组Arrays.sort(array)排序后获取最后一个即最大值3 ,3加1后拼接成GQ201802010004!代码如下:
package com.softeem.dto;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import com.softeem.dao.SongInfoDAO;

public class CreateGetNextId{
/*list集合为表中全部信息,str为最开始的字符串,strid为截取的自动增长的数值部分*/
public static String NextId(String str,List list,String[] strid){
int intid=0;
int[] array = new int[list.size()];
if(list.size()!=0){
for (int i = 0; i < list.size(); i++) {
array[i] = Integer.valueOf(strid[i]);
}
Arrays.sort(array);
intid=array[strid.length - 1];

}
String sid = String.valueOf(intid+1);
switch (sid.length()) {
case 1:
sid = "000" +sid;

break;
case 2:
sid = "00" + sid;

break;
case 3:
sid = "0" + sid;

break;
case 4:
sid = "" + sid;

break;

}
Calendar now = Calendar.getInstance();
String month=""+(now.get(Calendar.MONTH)+1);
String day=""+now.get(Calendar.DAY_OF_MONTH);
if((now.get(Calendar.MONTH)+1)<10){
month="0"+month;
}
if((now.get(Calendar.DAY_OF_MONTH))<10){
day="0"+day;
}
String nextid=str+now.get(Calendar.YEAR)+month+day+sid;

return nextid;

}
public static void main(String[] args) {
SongInfoDAO sdao=new SongInfoDAO();//dao
List<SongInfo> list=sdao.findAll(0, 100000);
String[] str=new String[list.size()];
for(int i=0;i<list.size();i++){
str[i]=list.get(i).getId().substring(10, 14);
}
System.out.println(CreateGetNextId.NextId("GQ", list, str));
}
}
dto :
package com.softeem.dto;

public class SongInfo {
private String id;//歌曲id
private String name;//歌曲名称
private int type;//歌曲类别
private String lyrics;//作词
private String compose;//作曲
private String singer;//演唱人
private int degree;//演唱难度
private String langues;//语言
private int duration;//歌曲时长
private int hotlevel;//流行程度
private int sum;//总计
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getLyrics() {
return lyrics;
}
public void setLyrics(String lyrics) {
this.lyrics = lyrics;
}
public String getCompose() {
return compose;
}
public void setCompose(String compose) {
this.compose = compose;
}
public String getSinger() {
return singer;
}
public void setSinger(String singer) {
this.singer = singer;
}
public int getDegree() {
return degree;
}
public void setDegree(int degree) {
this.degree = degree;
}
public String getLangues() {
return langues;
}
public void setLangues(String langues) {
this.langues = langues;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public int getHotlevel() {
return hotlevel;
}
public void setHotlevel(int hotlevel) {
this.hotlevel = hotlevel;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public SongInfo(String id, String name, int type, String lyrics, String compose, String singer, int degree,
String langues, int duration, int hotlevel, int sum) {
super();
this.id = id;
this.name = name;
this.type = type;
this.lyrics = lyrics;
this.compose = compose;
this.singer = singer;
this.degree = degree;
this.langues = langues;
this.duration = duration;
this.hotlevel = hotlevel;
this.sum = sum;
}
@Override
public String toString() {
return "SongInfo [id=" + id + ", name=" + name + ", type=" + type + ", lyrics=" + lyrics + ", compose="
+ compose + ", singer=" + singer + ", degree=" + degree + ", langues=" + langues + ", duration="
+ duration + ", hotlevel=" + hotlevel + ", sum=" + sum + "]";
}
public SongInfo() {
super();
}

}
dao:
package com.softeem.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.softeem.dbutils.DBUtils;
import com.softeem.dbutils.DBUtils.CallBack;
import com.softeem.dto.SongInfo;

public class SongInfoDAO{

public List<SongInfo> findAll(int pageIndex,int pageSize) {
String sql = "select id,name,type,lyrics,compose,singer,degree,langues,duration,hotlevel from songinfo limit ?,? ";
return DBUtils.queryList(sql, new CallBack<SongInfo>(){
@Override
public List<SongInfo> getDatas(ResultSet rs) {
SongInfo sf=null;
List<SongInfo> list=new ArrayList<SongInfo>();
try {
while(rs.next()){
sf=new SongInfo();
sf.setId(rs.getString("id"));
sf.setName(rs.getString("name"));
sf.setType(rs.getInt("type"));
sf.setLyrics(rs.getString("lyrics"));
sf.setCompose(rs.getString("compose"));
sf.setSinger(rs.getString("singer"));
sf.setDegree(rs.getInt("degree"));
sf.setLangues(rs.getString("langues"));
sf.setDuration(rs.getInt("duration"));
sf.setHotlevel(rs.getInt("hotlevel"));
list.add(sf);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
},pageIndex*pageSize,pageSize);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐