oracle中如何使用序列
2008-10-15 12:15
357 查看
//sequenceName为序列名,length为显示序列的长度例如显示为(0000001)
public String getCircSequence(String sequenceName, int length) throws CircException
{
Connection conn = null;
StringBuffer sb = null;
try
{
conn =JdbcConnection.getInstance().getConnection();//获取链接
int key = nextSequenceNumber(conn, sequenceName);//获取序列
if (key <= 0) return null;
sb = new StringBuffer(length);
String k = Integer.toString(key);
//想要显示的长度减掉获取序列的长度,将得到的长度用0 补位
int padSize = length - k.length();
for (int i = 0; i < padSize; i++) sb.append('0');
sb.append(key);
} catch (Throwable e)
{
e.printStackTrace();
throw new CircException(kDBReadError);
} finally
{
try
{
if (conn != null) conn.close();
} catch (Throwable e)
{
e.printStackTrace();
}
}
if (sb != null) return new String(sb);
else return null;
}
//获知取序列
public int nextSequenceNumber(Connection cc, String seqName) throws SQLException
{
if (cc == null) throw new SQLException();
int k = 0;
Statement stat = cc.createStatement();
ResultSet rs = stat.executeQuery("SELECT "+seqName+".nextval from dual");
if (rs != null && rs.next())
k = rs.getInt(1);
rs.close();
stat.close();
return k;
}
public static void main(String[] args)
{
CircSequence circSequence = new CircSequence();
try
{
String seq = circSequence.getCircSequence("CIR_SEQ_USL_V_USERID", 10);
System.out.println(seq);
} catch (Throwable e)
{
e.printStackTrace();
}
}
public String getCircSequence(String sequenceName, int length) throws CircException
{
Connection conn = null;
StringBuffer sb = null;
try
{
conn =JdbcConnection.getInstance().getConnection();//获取链接
int key = nextSequenceNumber(conn, sequenceName);//获取序列
if (key <= 0) return null;
sb = new StringBuffer(length);
String k = Integer.toString(key);
//想要显示的长度减掉获取序列的长度,将得到的长度用0 补位
int padSize = length - k.length();
for (int i = 0; i < padSize; i++) sb.append('0');
sb.append(key);
} catch (Throwable e)
{
e.printStackTrace();
throw new CircException(kDBReadError);
} finally
{
try
{
if (conn != null) conn.close();
} catch (Throwable e)
{
e.printStackTrace();
}
}
if (sb != null) return new String(sb);
else return null;
}
//获知取序列
public int nextSequenceNumber(Connection cc, String seqName) throws SQLException
{
if (cc == null) throw new SQLException();
int k = 0;
Statement stat = cc.createStatement();
ResultSet rs = stat.executeQuery("SELECT "+seqName+".nextval from dual");
if (rs != null && rs.next())
k = rs.getInt(1);
rs.close();
stat.close();
return k;
}
public static void main(String[] args)
{
CircSequence circSequence = new CircSequence();
try
{
String seq = circSequence.getCircSequence("CIR_SEQ_USL_V_USERID", 10);
System.out.println(seq);
} catch (Throwable e)
{
e.printStackTrace();
}
}
相关文章推荐
- 如何使用Oracle序列
- 关于字符和数字类型的索引,Oracle如何实现内部自动转换以及索引使用的验证测试
- 解决32位工具如何连接64位DB---Oracle Instant Client 使用 说明
- Oracle PL/SQL中如何使用%TYPE和%ROWTYPE
- 如何在Oracle中使用Java存储过程 (详解)
- ORACLE如何使用DBMS_METADATA.GET_DDL获取表,表空间,用户,视图等的DDL语句
- HowTo:如何使用Oracle的Decode函数进行多值判断
- [转]:ORACLE如何使用DBMS_METADATA.GET_DDL获取DDL语句
- 在SQL 2012中使用和Oracle 一样的序列
- Oracle PL/SQL中如何使用%TYPE和%ROWTYPE(转帖)
- 【DBaas】如何使用Oracle Compute Cloud Service
- oracle decode的如何使用
- ORACLE如何使用DBLINK连接另一个数据库[整理]
- 如何在SAS中直接使用Oracle特有函数--sas Pass-Through Facility
- SQL 教程数据库包括:Oracle, Sybase, SQL Server, DB2, Access 等等,您将学到如何使用 SQL 访问和处理数据系统中的数据
- ORACLE中序列的使用
- Oracle序列使用:建立、删除
- 如何在Oracle中使用Java存储过程(详解)
- Oracle 如何创建和使用全文索引
- Oracle 创建自增字段的方法—使用序列方法