public static void delete(DBInfo info, Connection conn) throws Exception { PreparedStatement pstmt = null; try { StringBuffer sql = new StringBuffer(); sql.append("delete from " + info.getTable() + " where "); int i = 0; for (DBField f : info.getPKeys()) { sql.append(f.getDBName() + " = ?"); if (i < (info.getPKeys().size() - 1)) { sql.append(" and "); } i++; } pstmt = conn.prepareStatement(sql.toString()); i = 0; for (DBField f : info.getPKeys()) { DBHelper.setPreparedStatement(pstmt, (i + 1), f.getValue()); i++; } pstmt.execute(); } catch (Exception e) { throw e; } finally { DBUtil.closePreparedStatement(pstmt); } }
public static void getObject(Object obj, DBInfo info, Connection conn) throws Exception { PreparedStatement pstmt = null; ResultSet rs = null; try { StringBuffer sql = new StringBuffer(); sql.append("select "); int i = 0; for (DBField f : info.getAllFields()) { sql.append(f.getDBName()); if (i < (info.getAllFields().size() - 1)) { sql.append(", "); } i++; } sql.append(" from " + info.getTable() + " where "); i = 0; for (DBField f : info.getPKeys()) { sql.append(f.getDBName() + " = ?"); if (i < (info.getPKeys().size() - 1)) { sql.append(" and "); } i++; } pstmt = conn.prepareStatement(sql.toString()); i = 0; for (DBField f : info.getPKeys()) { DBHelper.setPreparedStatement(obj, pstmt, (i + 1), f); i++; } rs = pstmt.executeQuery(); if (rs.next()) { for (DBField f : info.getAllFields()) { ObjectHelper.setObjectField(obj, f, rs); } } else { throw new Exception("Binder -> Sorry can not find object"); } } catch (Exception e) { throw e; } finally { DBUtil.closePreparedStatement(pstmt, rs); } }
public static String getPasswordField(Object obj, DBInfo info, String field_name, Connection conn) throws Exception { String s; PreparedStatement pstmt = null; ResultSet rs = null; try { StringBuffer sql = new StringBuffer(); sql.append("select " + field_name + " from " + info.getTable() + " where "); int i = 0; for (Iterator it = info.getPKeys().iterator(); it.hasNext(); i++) { DBField f = (DBField) it.next(); sql.append(f.getDBName() + " = ?"); if (i < (info.getPKeys().size() - 1)) { sql.append(" and "); } } pstmt = conn.prepareStatement(sql.toString()); i = 0; for (Iterator it = info.getPKeys().iterator(); it.hasNext(); i++) { DBField f = (DBField) it.next(); DBHelper.setPreparedStatement(obj, pstmt, (i + 1), f); } rs = pstmt.executeQuery(); if (rs.next()) { s = rs.getString(1); } else { throw new Exception("Binder -> Error in retriving password field"); } } catch (Exception e) { throw e; } finally { DBUtil.closePreparedStatement(pstmt, rs); } return s; }
public static boolean exists(Object obj, DBInfo info, Connection conn) throws Exception { boolean flag = false; PreparedStatement pstmt = null; ResultSet rs = null; try { StringBuffer sql = new StringBuffer(); sql.append("select "); int i = 0; for (DBField f : info.getAllFields()) { sql.append(f.getDBName()); break; } sql.append(" from " + info.getTable() + " where "); i = 0; for (DBField f : info.getPKeys()) { sql.append(f.getDBName() + " = ?"); if (i < (info.getPKeys().size() - 1)) { sql.append(" and "); } i++; } pstmt = conn.prepareStatement(sql.toString()); i = 0; for (DBField f : info.getPKeys()) { DBHelper.setPreparedStatement(obj, pstmt, (i + 1), f); i++; } rs = pstmt.executeQuery(); if (rs.next()) { flag = true; } } catch (Exception e) { throw e; } finally { DBUtil.closePreparedStatement(pstmt, rs); } return flag; }
public static void setBinaryField(DBInfo info, String field_name, InputStream in, Connection conn) throws Exception { PreparedStatement pstmt = null; try { StringBuffer sql = new StringBuffer(); sql.append("update " + info.getTable()); sql.append(" set "); sql.append(field_name); sql.append(" = ?"); sql.append(" where "); int i = 0; for (DBField f : info.getPKeys()) { sql.append(f.getDBName()); sql.append(" = ?"); if (i < (info.getPKeys().size() - 1)) { sql.append(" and "); } i++; } pstmt = conn.prepareStatement(sql.toString()); pstmt.setBinaryStream(1, in, in.available()); i = 1; for (DBField f : info.getPKeys()) { DBHelper.setPreparedStatement(pstmt, (i + 1), f.getValue()); i++; } pstmt.execute(); } catch (Exception e) { throw e; } finally { DBUtil.closePreparedStatement(pstmt); } }