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