程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 一個生成唯一序號的服務

一個生成唯一序號的服務

編輯:關於JAVA

package com.highcom.seqgen.dao.jdbc;

import java.sql.*;import Javax.sql.*;

import org.apache.commons.logging.*;import org.springframework.beans.factory.*;import org.springframework.context.*;import org.springframework.jdbc.core.*;import org.springframework.jdbc.object.*;import com.highcom.seqgen.dao.*;import com.highcom.seqgen.domain.*;

public class SequenceDaoJdbcImpl implements SequenceDao, InitializingBean {

private DataSource dataSource; private static Log logger = LogFactory.getLog(SequenceDaoJdbcImpl.class);

// SequenceQuery sequenceQuery; SequenceUpdate sequenceUpdate; SequenceInsert sequenceInsert; // protected static final String INSERT_SQL = "insert into sequence(seq_name,seq_value) values( ? , ? )"; protected static final String UPDATE_SQL = "update sequence set seq_value = ? where seq_name = ?"; protected static final String SELECT_SQL = "select seq_name,seq_value from sequence where seq_name =?";

public SequenceDaoJdbcImpl() {

}

public int getSequence(String seq_name) { int result; Object obj = sequenceQuery.findObject(new Object[] {seq_name}); if (obj == null) { sequenceInsert.insert(seq_name, 0); }

Sequence seq = (Sequence) sequenceQuery.findObject(new Object[] {seq_name}); sequenceUpdate.update(seq.getName(), seq.getValue() + 1); result = seq.getValue() + 1; return result; }

public void afterPropertIEsSet() throws Exception { if (dataSource == null) { logger.error("Must set dataSource bean property on " + getClass()); throw new ApplicationContextException( "Must set dataSource bean property on " + getClass()); } // sequenceQuery = new SequenceQuery(this.dataSource); sequenceUpdate = new SequenceUpdate(this.dataSource); sequenceInsert = new SequenceInsert(this.dataSource); }

public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; }

///////////////////////jdbc內部類 class SequenceInsert extends SqlUpdate { public SequenceInsert(DataSource dataSource) { super(dataSource, INSERT_SQL); declareParameter(new SqlParameter(Types.VARCHAR)); declareParameter(new SqlParameter(Types.INTEGER)); compile(); }

public void insert(String seqName, int segValue) { Object[] objs = new Object[] { seqName, new Integer(segValue)}; super.update(obJS); } }

class SequenceUpdate extends SqlUpdate { public SequenceUpdate(DataSource dataSource) { super(dataSource, UPDATE_SQL); declareParameter(new SqlParameter(Types.INTEGER)); declareParameter(new SqlParameter(Types.VARCHAR));

compile();

}

public void update(String seqName, int segValue) { Object[] objs = new Object[] { new Integer(segValue), seqName}; super.update(obJS); } }

class SequenceQuery extends MappingSqlQuery { public SequenceQuery(DataSource dataSource) { super(dataSource, SELECT_SQL); declareParameter(new SqlParameter(Types.VARCHAR)); compile(); }

protected Object mapRow(ResultSet resultSet, int _int) throws SQLException { Sequence seq = new Sequence(); seq.setName(resultSet.getString("seq_name")); seq.setValue(resultSet.getInt("seq_value")); return seq; } }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved