Sunday, June 29, 2008

Sequence Number in J2EE/Oracle web app

In a medium to large scale web app we have different modules with relevant tables.
In those tables we have sequence number which has to be filled in automatically. So to create this kind of auto generated numbers we use Sequences in Oracle.


public static final Class CLASS_NAME = PKSequenceUtility.class;
public static Integer populateApplnData(String tableName){
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
int seqNum = 0;
try {
con = DBUtility.getConnection();
// Constants.SCHEMA_NAME+".sq_"+tableName+".nextval from dual ==> "own_abc.sq_requesttable.nextval from dual"
String sql = "select "+Constants.SCHEMA_NAME+".sq_"+tableName+".nextval from dual";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
seqNum = rs.getInt(1);
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
finally
{
DBUtility.closeResultSet(rs);
DBUtility.closeStatement(ps);
DBUtility.releaseConnection(con);
}
return (seqNum);
}

No comments: