1 18 19 package sync4j.server.store; 20 21 import java.util.ArrayList ; 22 import java.sql.*; 23 import javax.sql.DataSource ; 24 25 import sync4j.framework.tools.DBTools; 26 import sync4j.framework.server.store.*; 27 28 36 public class UtilsPersistentStore { 37 38 40 public static final int SQL_GET_COUNTER = 0; 41 public static final int SQL_UPDATE_COUNTER = 1; 42 43 45 protected String idSpace = null; 46 47 public void setIdSpace(String idSpace) { 48 this.idSpace = idSpace; 49 } 50 51 public String getIdSpace() { 52 return this.idSpace; 53 } 54 55 protected String [] sql = null; 56 57 public void setSql(String [] sql) { 58 this.sql = sql; 59 } 60 61 public String [] getSql() { 62 return this.sql; 63 } 64 65 69 75 protected int readCounter(Connection conn) throws PersistentStoreException { 76 PreparedStatement stmt = null; 77 ResultSet rs = null; 78 int counter = 0; 79 80 try { 81 stmt = conn.prepareStatement(sql[SQL_GET_COUNTER]); 82 rs = stmt.executeQuery(); 83 84 if (rs.next() == false) { 85 throw new NotFoundException("Counter not found for " 86 + idSpace); 87 } 88 counter = rs.getInt(1) + 1; 89 90 rs.close(); rs = null; 91 stmt.close(); stmt = null; 92 93 stmt = conn.prepareStatement(sql[SQL_UPDATE_COUNTER]); 94 stmt.setInt(1, counter); 95 stmt.executeUpdate(); 96 97 } catch (SQLException e) { 98 e.printStackTrace(); 99 throw new PersistentStoreException("Error reading the counter " + counter, e); 100 } finally { 101 DBTools.close(null, stmt, rs); 102 } 103 return counter; 104 } 105 } 106 107 | Popular Tags |