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

6 篇文章 0 订阅
6 篇文章 0 订阅

首先查找数据库中是否有数据,没有设置为0001,否则获取 例如 GQ201802010003 的最后4位数字转成int数组Arrays.sort(array)排序后获取最后一个即最大值3 ,3加1后拼接成GQ201802010004!代码如下(dto<实体类>与dto 获取list集合使用):

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为ID最开始的字符(如学生就为SD),
                *strid为截取的自动增长的数值部分(最后的4位) 故ID就为SD201801010001样式
                */
		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);
	}

	
	
	
			
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值