如何实现字符串类型的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); } }
相关文章推荐
- 一个通用的单元测试框架的思考和设计07-实现篇-自动管理测试数据-如何为自增长主键id赋值
- 一个通用的单元测试框架的思考和设计07-实现篇-自动管理测试数据-如何为自增长主键id赋值
- 如何在Oracle 中实现类似自动增加 ID 的功能?
- oracle主键自动增长的实现 Oracle Sequence oracle maxid自动生成--ORA-00001: 违反唯一约束条件 (NAME.SYS_C005547)产生的原因之一(select * from all_cons_columns w
- SQL Server中根据某个字段,ID字段自动增长的实现
- 在存储过程中如何实现将id列表字符串传入in()
- 如何在Oracle 中实现类似自动增加 ID 的功能?
- shell如何实现 数字 字符串自动补零 awk
- 如何写出一条SQL语句:取出表A中第31~40条记录(SQLServer,以自动增长的ID作为主键
- Java实现id自增的字符串类型的数字编号
- oracle中的ID号如何实现自增长
- 如何插入一条记录获取插入后的自动增长ID列的方法.
- MongoDB 实现字段自动增长id Mongodb auto increment ID
- 如何实现oracle中的某个字段自动增长
- JSP中如何取得MSSQL数据库表中自动增长的ID主键值?
- 关于字符和数字类型的索引,Oracle如何实现内部自动转换以及索引使用的验证测试
- MySQL如何创建用户表并将其ID设置为自动增长列?
- 如何在oracle数据库中设置自动增长的id字段
- Access数据库id类型由自动编号改成数字类型后如何再从数字类型改成自动编号呢
- 如何应用Asp.Net Mvc内建功能(DefaultModelBinder)实现简单类型、复杂类型、集合类型,以及字典类型的自动绑定