1 22 23 import org.xquark.mapping.*; 24 import java.sql.*; 25 import java.math.*; 26 27 33 34 public class PurchaseOrderId implements UserGenerator { 35 36 private static int counter = -1; 37 38 public String getXMLType() { 39 return "int"; 40 } 41 42 public synchronized Object getValue(StorageContext context) { 43 if (counter < 0) 44 initCounter(context.getConnection()); 45 return new BigDecimal(++counter); 46 } 47 48 void initCounter(Connection connection) { 49 ResultSet rs = null; 50 Statement statement = null; 51 try { 52 statement = connection.createStatement(); 53 try { 54 rs = statement.executeQuery("select max(\"id\") from \"po\""); 55 } catch (SQLException ex) { 56 rs = statement.executeQuery("select max(id) from po"); 57 } 58 Object result = null; 59 while (rs.next()) { 60 counter = rs.getInt(1); 61 } 62 } 63 catch (SQLException ex) { 64 ex.printStackTrace(); 65 } 66 finally { 67 try { 68 if (rs != null) 69 rs.close(); 70 if (statement != null) 71 statement.close(); 72 } 73 catch (SQLException ex) { 74 } 76 } 77 } 78 } 79 | Popular Tags |