use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class Query method firstId.
private int firstId(boolean assumeOnlyOneResult) throws DBException {
String[] keys = table.getKeyColumns();
if (keys.length != 1) {
throw new DBException("Table " + table + " has 0 or more than 1 key columns");
}
StringBuffer selectClause = new StringBuffer("SELECT ");
selectClause.append(keys[0]);
selectClause.append(" FROM ").append(table.getTableName());
String sql = buildSQL(selectClause, true);
int id = -1;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql, trxName);
rs = createResultSet(pstmt);
if (rs.next()) {
id = rs.getInt(1);
}
if (assumeOnlyOneResult && rs.next()) {
// TODO : translate
throw new DBException("QueryMoreThanOneRecordsFound");
}
} catch (SQLException e) {
throw new DBException(e, sql);
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
//
return id;
}
use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class PO method load.
// load
/**
* (re)Load record with m_ID[*]
* @param trxName transaction
* @return true if loaded
*/
public boolean load(String trxName) {
m_trxName = trxName;
boolean success = true;
StringBuffer sql = p_info.buildSelect();
sql.append(" WHERE ").append(get_WhereClause(false));
int size = get_ColumnCount();
// int index = -1;
if (CLogMgt.isLevelFinest())
log.finest(get_WhereClause(true));
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// local trx only
pstmt = DB.prepareStatement(sql.toString(), m_trxName);
for (int i = 0; i < m_IDs.length; i++) {
Object oo = m_IDs[i];
if (oo instanceof Integer)
pstmt.setInt(i + 1, ((Integer) m_IDs[i]).intValue());
else
pstmt.setString(i + 1, m_IDs[i].toString());
}
rs = pstmt.executeQuery();
if (rs.next()) {
success = load(rs);
} else {
if (!log.getLevel().equals(Level.CONFIG))
log.log(Level.WARNING, "NO Data found for " + get_WhereClause(true));
m_IDs = new Object[] { I_ZERO };
success = false;
// throw new DBException("NO Data found for " + get_WhereClause(true));
}
m_createNew = false;
// reset new values
m_newValues = new Object[size];
} catch (Exception e) {
String msg = "";
if (m_trxName != null)
msg = "[" + m_trxName + "] - ";
msg += get_WhereClause(true) + // + ", " + p_info.toString(index)
", SQL=" + sql.toString();
success = false;
m_IDs = new Object[] { I_ZERO };
log.log(Level.SEVERE, msg, e);
// throw new DBException(e);
} finally // Finish
{
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
loadComplete(success);
return success;
}
use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class Query method first.
/**
* Return first PO that match query criteria
* @return first PO
* @throws DBException
*/
@SuppressWarnings("unchecked")
public <T extends PO> T first() throws DBException {
T po = null;
String sql = buildSQL(null, true);
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql, trxName);
rs = createResultSet(pstmt);
if (rs.next()) {
po = (T) table.getPO(rs, trxName);
}
} catch (SQLException e) {
log.log(Level.SEVERE, sql, e);
throw new DBException(e, sql);
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
return po;
}
use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class Query method getIDs.
/**
* Get a Array with the IDs for this Query
* @return Get a Array with the IDs
*/
public int[] getIDs() {
String[] keys = table.getKeyColumns();
if (keys.length != 1) {
throw new DBException("Table " + table + " has 0 or more than 1 key columns");
}
StringBuffer selectClause = new StringBuffer("SELECT ");
selectClause.append(keys[0]);
selectClause.append(" FROM ").append(table.getTableName());
String sql = buildSQL(selectClause, true);
ArrayList<Integer> list = new ArrayList<Integer>();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql, trxName);
rs = createResultSet(pstmt);
while (rs.next()) {
list.add(rs.getInt(1));
}
} catch (SQLException e) {
throw new DBException(e, sql);
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
// Convert to array
int[] retValue = new int[list.size()];
for (int i = 0; i < retValue.length; i++) {
retValue[i] = list.get(i);
}
return retValue;
}
use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class DB method getSQLValueTSEx.
/**
* Get Timestamp Value from sql
* @param trxName trx
* @param sql sql
* @param params array of parameters
* @return first value or null
* @throws DBException if there is any SQLException
*/
public static Timestamp getSQLValueTSEx(String trxName, String sql, Object... params) {
Timestamp retValue = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = prepareStatement(sql, trxName);
setParameters(pstmt, params);
rs = pstmt.executeQuery();
if (rs.next())
retValue = rs.getTimestamp(1);
else
log.fine("No Value " + sql);
} catch (SQLException e) {
throw new DBException(e, sql);
} finally {
close(rs, pstmt);
rs = null;
pstmt = null;
}
return retValue;
}
Aggregations