jsp按規則生成編碼問題
阿新 • • 發佈:2018-11-13
這個功能需求是這樣的,編碼是由5個條件限定而成,前四個是從列表中隨意選擇,後一個是根據型別按照序列遞增,整個編碼是由這5個條件組合而成,如下圖所示:
此外還需滿足:1)生成的最終編碼是唯一的;2)可以批量生成。
我們先看單項生成的例子。
一、單項生成
背景:資料庫裡有匯入歷史資料,生成規則是按照選擇的字元+流水號組合而成,且生成的編碼唯一;流水號自增,從01開始。
思路:從資料庫裡select出5級條件限定的流水號的最大值,遞增1;組合5個條件即可;解決了生成唯一性的問題,不需要檢查重複值。
生成程式碼:
public ArrayList<SkuCode> singen(String l1,String l2,String l3,String l4,String l5) throws SQLException, ParseException {//引數為前臺傳入的5個選項值 Connection conn = null; PreparedStatement ps = null; SkuCode sc=null; ArrayList<SkuCode> SCList = new ArrayList<SkuCode>();//物件列表 Calendar cal = Calendar.getInstance(); java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd"); String today=format.format(Calendar.getInstance().getTime());//生成時間 if(l5!=null&&l5.equals("H+流水號")) {//流水號型別 String sql1 = "select max(l5) from SKU where l5 like \"%H%\" and type=2 and l1 like ? and l2 like ? and l3 like ? and l4 like ?";//5個選項值確定的那一類編碼的流水號最大值的sql語句 conn=DBUtil.getConnection(); ps=conn.prepareStatement(sql1); ps.setString(1, l1); ps.setString(2, l2); ps.setString(3, l3); ps.setString(4, l4); ResultSet rs=ps.executeQuery();//結果集 while(rs.next()) { String max=rs.getString(1); if(max != null) {//流水號最大值不為空就遞增 int temp=Integer.valueOf(max.substring(1)).intValue(); temp++; if(temp>0&&temp<10) {//流水號格式 l5="H"+"0"+String.valueOf(temp); }else if(temp>9) {//流水號格式 l5="H"+String.valueOf(temp); } }else { int temp=1;//流水號最大值為空就從1開始 l5="H"+"0"+String.valueOf(temp); } sc=new SkuCode();//物件賦值 sc.setL1(l1); sc.setL2(l2); sc.setL3(l3); sc.setL4(l4); sc.setL5(l5); sc.setSku(l1+"-"+l2+"-"+l3+"-"+l4+"-"+l5);//生成編碼格式 sc.setCreatetime(today); SCList.add(sc);//新增到物件列表 } } else if(l5!=null&&l5.equals("S+流水號")) {//同上理 String sql2 = "select max(l5) from SKU where l5 like \"%s%\" and type=2 and l1 like ? and l2 like ? and l3 like ? and l4 like ?"; conn=DBUtil.getConnection(); ps=conn.prepareStatement(sql2); ps.setString(1, l1); ps.setString(2, l2); ps.setString(3, l3); ps.setString(4, l4); ResultSet rs=ps.executeQuery(); while(rs.next()) { String max=rs.getString(1); if(max != null) { int temp=Integer.valueOf(max.substring(1)).intValue(); temp++; if(temp>0&&temp<10) { l5="S"+"0"+String.valueOf(temp); }else if(temp>9) { l5="S"+String.valueOf(temp); } }else { int temp=1; l5="S"+"0"+String.valueOf(temp); } sc=new SkuCode(); sc.setL1(l1); sc.setL2(l2); sc.setL3(l3); sc.setL4(l4); sc.setL5(l5); sc.setSku(l1+"-"+l2+"-"+l3+"-"+l4+"-"+l5); sc.setCreatetime(today); SCList.add(sc); } } return SCList;//返回物件列表 }
寫入資料庫程式碼:
//新增方法(寫入資料庫) public void add(SkuCode sc) throws SQLException { Connection conn = null; PreparedStatement ps = null; String sql = "insert into SKU(l1,l2,l3,l4,l5,sku,createtime,type)values(?,?,?,?,?,?,?,2)";//插入語句 try{ conn = DBUtil.getConnection(); ps = conn.prepareStatement(sql); ps.setString(1, sc.getL1()); ps.setString(2, sc.getL2()); ps.setString(3, sc.getL3()); ps.setString(4, sc.getL4()); ps.setString(5, sc.getL5()); ps.setString(6, sc.getSku()); ps.setString(7, sc.getCreatetime()); ps.executeUpdate(); }catch(SQLException e){ e.printStackTrace(); throw new SQLException("新增資料失敗"); } }
二、多項生成
多項生成的基本程式碼與單項生成基本相同,不過在生成時多了一項迴圈,前端傳來引數生成多少條,這邊就迴圈幾次
生成程式碼(部分):
public ArrayList<SkuCode> gen(String l1,String l2,String l3,String l4,String l5,String num) throws SQLException,ParseException { Connection conn = null; PreparedStatement ps = null; SkuCode sc=null; ArrayList<SkuCode> SCList = new ArrayList<SkuCode>(); Calendar cal = Calendar.getInstance(); java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd"); String today=format.format(Calendar.getInstance().getTime()); int k = Integer.valueOf(num).intValue();//這個是前端傳入的生成條數 if(l5!=null&&l5.equals("H+流水號")) { String sql1 = "select max(l5) from SKU where l5 like \"%H%\" and type=2 and l1 like ? and l2 like ? and l3 like ? and l4 like ?"; conn=DBUtil.getConnection(); ps=conn.prepareStatement(sql1); ps.setString(1, l1); ps.setString(2, l2); ps.setString(3, l3); ps.setString(4, l4); ResultSet rs=ps.executeQuery(); while(rs.next()) { String max=rs.getString(1); if(max==null) { int temp=0; for(int i=0;i<k;i++){//注意這塊迴圈,是多項生成和單項生成的區別所在 temp++; if(temp>0&&temp<10) { l5="H"+"0"+String.valueOf(temp); }else { l5="H"+String.valueOf(temp); } sc=new SkuCode(); sc.setL1(l1); sc.setL2(l2); sc.setL3(l3); sc.setL4(l4); sc.setL5(l5); sc.setSku(l1+"-"+l2+"-"+l3+"-"+l4+"-"+l5); sc.setCreatetime(today); SCList.add(sc); } }else { int temp=Integer.valueOf(max.substring(1)).intValue(); for(int i=0;i<k;i++){ temp++; if(temp>0&&temp<10) { l5="H"+"0"+String.valueOf(temp); }else { l5="H"+String.valueOf(temp); } sc=new SkuCode(); sc.setL1(l1); sc.setL2(l2); sc.setL3(l3); sc.setL4(l4); sc.setL5(l5); sc.setSku(l1+"-"+l2+"-"+l3+"-"+l4+"-"+l5); sc.setCreatetime(today); SCList.add(sc); } } } }
三、檢測重複值
背景:這塊是遺留問題,資料庫裡的歷史資料導致有部分編碼的流水號是從中途開始的,例如從80開始;那麼按照上述規則取最大值肯定是從81開始遞增;實際需要從01開始。
思路:流水號直接從01開始遞增,將整個編碼跟資料庫比對,如果資料庫裡存在,則跳過這個流水號往下遞增,如果不存在則輸出結束迴圈。
缺點:資料量大的話效能會崩潰
生成程式碼(部分):
//多項生成方法
public ArrayList<SkuCode> gen(String l1,String l2,String l3,String l4,String l5,String num) throws SQLException,ParseException {
Connection conn = null;
PreparedStatement ps = null;
SkuCode sc=null;
ArrayList<SkuCode> SCList = new ArrayList<SkuCode>();
Calendar cal = Calendar.getInstance();
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
String today=format.format(Calendar.getInstance().getTime());
int k = Integer.valueOf(num).intValue();
if(l5!=null&&l5.equals("H+流水號")) {
int temp=0;//從0開始
int count=0;
for(int i=0;i<k;i++){//生成條數的迴圈
do {//因為從0開始肯定要先執行一次,所以採用do-while迴圈
temp++;
if(temp>0&&temp<10) {//流水號格式
l5="H"+"0"+String.valueOf(temp);
}else{
l5="H"+String.valueOf(temp);
}
sc=new SkuCode();
sc.setL1(l1);
sc.setL2(l2);
sc.setL3(l3);
sc.setL4(l4);
sc.setL5(l5);
sc.setSku(l1+"-"+l2+"-"+l3+"-"+l4+"-"+l5);
String sku=sc.getSku();
String sql1 = "select count(sku) from SKU where sku like ? and type=2";//資料庫比對是否存在該項生成的編碼
conn=DBUtil.getConnection();
ps=conn.prepareStatement(sql1);
ps.setString(1, sku);
ResultSet rs=ps.executeQuery();
while(rs.next()) {
count=rs.getInt(1);
}
}while(count!=0);//不存在則結束迴圈;存在的話返回流水號繼續遞增再比對
sc.setCreatetime(today);
SCList.add(sc);//新增物件列表
}
}
四、前端傳入部分程式碼
<%
request.setCharacterEncoding("utf-8");
String l1=request.getParameter("l1");
String l2=request.getParameter("l2");
String l3=request.getParameter("l3");
String l4=request.getParameter("l4");
String l5=request.getParameter("l5");
SkuCodeDaoImpl skuCode = new SkuCodeDaoImpl();
ArrayList<SkuCode> scList = skuCode.singen(l1,l2,l3,l4,l5);//傳參
for (SkuCode s : scList){
%>
<tr align=center>
<td><%=s.getSku()%></td>
<%
SkuCode sc=new SkuCode();//賦值
sc.setL1(s.getL1());
sc.setL2(s.getL2());
sc.setL3(s.getL3());
sc.setL4(s.getL4());
sc.setL5(s.getL5());
sc.setSku(s.getSku());
sc.setCreatetime(s.getCreatetime());
skuCode.add(sc);//寫入資料庫
%>
<%}%>
以上就是全部實現方式,新手編碼不成熟,以作總結。