//*************************************************************************
//Javaで通番を作成
//import java.sql.*;
//import java.text.SimpleDateFormat;
//import java.util.Calendar;
//*************************************************************************
public int getTuban(){
Calendar t_day = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String seaquence_name = "seq" + format.format(t_day.getTime());
int result1 = 0;
try{
//シーケンス存在確認(無ければ作成)
ResultSet rs_seq = statement.executeQuery("SELECT COUNT(*) AS cnt FROM pg_statio_user_sequences WHERE relname='" + seaquence_name + "'");
rs_seq.next();
int reccount = rs_seq.getInt("cnt");
rs_seq.close();
if(reccount==0){
//過去日のシーケンスを削除
ResultSet rs_dseq = statement.executeQuery("SELECT relname FROM pg_statio_user_sequences WHERE relname like 'seq2%'");
while(rs_dseq.next()){
statement.execute("DROP SEAQUENCE " + rs_dseq.getString("relname"));
}
//新しくシーケンスを作成
StringBuilder sb1 = new StringBuilder("");
sb1.append("CREATE SEQUENCE ").append(seaquence_name);
sb1.append(" INCREMENT 1");
sb1.append(" MINVALUE 1");
sb1.append(" MAXVALUE 9999");
sb1.append(" START 1");
sb1.append(" CACHE 1");
sb1.append(" CYCLE;");
sb1.append("ALTER TABLE ").append(seaquence_name).append(" OWNER TO postgres;");
statement.execute(sb1.toString());
}
//連番呼び出し
ResultSet rs_nextval = statement.executeQuery("SELECT nextval('" + seaquence_name + "') AS tuban");
rs_nextval.next();
result1 = rs_nextval.getInt("tuban");
rs_nextval.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
return result1;
}
0 件のコメント:
コメントを投稿