List of usage examples for java.sql PreparedStatement getGeneratedKeys
ResultSet getGeneratedKeys() throws SQLException;
Statement
object. From source file:org.wso2.carbon.identity.claim.metadata.mgt.dao.ClaimDAO.java
public int addClaim(Connection connection, String claimDialectURI, String claimURI, int tenantId) throws ClaimMetadataException { PreparedStatement prepStmt = null; ResultSet rs = null;//from w ww . jav a 2 s .c o m int claimId = 0; String query = SQLConstants.ADD_CLAIM; try { String dbProductName = connection.getMetaData().getDatabaseProductName(); prepStmt = connection.prepareStatement(query, new String[] { DBUtils.getConvertedAutoGeneratedColumnName(dbProductName, SQLConstants.ID_COLUMN) }); prepStmt.setString(1, claimDialectURI); prepStmt.setInt(2, tenantId); prepStmt.setString(3, claimURI); prepStmt.setInt(4, tenantId); prepStmt.executeUpdate(); rs = prepStmt.getGeneratedKeys(); if (rs.next()) { claimId = rs.getInt(1); } } catch (SQLException e) { throw new ClaimMetadataException( "Error while adding claim " + claimURI + " to dialect " + claimDialectURI, e); } finally { IdentityDatabaseUtil.closeResultSet(rs); IdentityDatabaseUtil.closeStatement(prepStmt); } return claimId; }
From source file:org.wso2.carbon.identity.authorization.core.jdbc.dao.JDBCRolePermissionDAO.java
@Override protected void insert(PreparedStatement stmt, ResultSet res, Connection connection) throws SQLException, UserStoreException { String sql = "INSERT INTO UM_ROLE_PERMISSION (UM_PERMISSION_ID, UM_ROLE_NAME, UM_IS_ALLOWED, UM_TENANT_ID) VALUES(?,?,?,?) "; stmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); byte count = 0; stmt.setInt(++count, getPermissionId()); stmt.setString(++count, getRoleName()); stmt.setBoolean(++count, isAuthorized()); stmt.setInt(++count, getTenantId()); int resCount = stmt.executeUpdate(); if (resCount == 0) { String error = "Insertion faild for the permission"; log.error(error);//from ww w . j ava 2 s . c o m throw new UserStoreException(error); } res = stmt.getGeneratedKeys(); if (res.next()) { setId(res.getInt(1)); } }
From source file:org.wso2.carbon.identity.authorization.core.jdbc.dao.JDBCUserPermissionDAO.java
@Override protected void insert(PreparedStatement stmt, ResultSet res, Connection connection) throws SQLException, UserStoreException { String sql = "INSERT INTO UM_USER_PERMISSION (UM_PERMISSION_ID, UM_USER_NAME, UM_IS_ALLOWED, UM_TENANT_ID) VALUES(?,?,?,?) "; stmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); byte count = 0; stmt.setInt(++count, getPermissionId()); stmt.setString(++count, getUserName()); stmt.setBoolean(++count, isAuthorized()); stmt.setInt(++count, getTenantId()); int resCount = stmt.executeUpdate(); if (resCount == 0) { String error = "Insertion faild for the permission"; log.error(error);//from w w w . j av a 2s . co m throw new UserStoreException(error); } res = stmt.getGeneratedKeys(); if (res.next()) { setId(res.getInt(1)); } }
From source file:net.mindengine.oculus.frontend.service.runs.JdbcTestRunDAO.java
@Override public Long saveRun(SavedRun savedRun) throws Exception { PreparedStatement ps = getConnection() .prepareStatement("insert into saved_runs (name, user_id, date) values (?, ?, ?)"); ps.setString(1, savedRun.getName()); ps.setLong(2, savedRun.getUserId()); ps.setTimestamp(3, new Timestamp(savedRun.getDate().getTime())); logger.info(ps);//from w w w. java 2 s .c o m ps.executeUpdate(); ResultSet rs = ps.getGeneratedKeys(); if (rs.next()) { return rs.getLong(1); } return null; }
From source file:net.mindengine.oculus.frontend.service.test.JdbcTestDAO.java
@Override public Long createTestGroup(TestGroup testGroup) throws Exception { PreparedStatement ps = getConnection() .prepareStatement("insert into test_groups (name, description, project_id) values (?, ?, ?)"); logger.info(ps);/*from www . j a va 2 s .c o m*/ ps.setString(1, testGroup.getName()); ps.setString(2, testGroup.getDescription()); ps.setLong(3, testGroup.getProjectId()); ps.executeUpdate(); ResultSet rs = ps.getGeneratedKeys(); Long groupId = 0L; if (rs.next()) { groupId = rs.getLong(1); } return groupId; }
From source file:org.wso2.carbon.device.mgt.core.notification.mgt.dao.impl.NotificationDAOImpl.java
@Override public int addNotification(int deviceId, int tenantId, Notification notification) throws NotificationManagementException { Connection conn;/*from w ww.ja v a 2s .co m*/ PreparedStatement stmt = null; ResultSet rs; int notificationId = -1; try { conn = NotificationManagementDAOFactory.getConnection(); String sql = "INSERT INTO DM_NOTIFICATION(DEVICE_ID, OPERATION_ID, STATUS, DESCRIPTION, TENANT_ID) " + "VALUES (?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt.setInt(1, deviceId); stmt.setInt(2, notification.getOperationId()); stmt.setString(3, notification.getStatus().toString()); stmt.setString(4, notification.getDescription()); stmt.setInt(5, tenantId); stmt.execute(); rs = stmt.getGeneratedKeys(); if (rs.next()) { notificationId = rs.getInt(1); } } catch (Exception e) { throw new NotificationManagementException( "Error occurred while adding the " + "Notification for device id : " + deviceId, e); } finally { NotificationDAOUtil.cleanupResources(stmt, null); } return notificationId; }
From source file:net.mindengine.oculus.frontend.service.customization.JdbcCustomizationDAO.java
@Override public Long addCustomization(Customization customization) throws Exception { PreparedStatement ps = getConnection() .prepareStatement("insert into customizations " + "(unit, " + "project_id, " + "name, " + "description, " + "type, " + "subtype, " + "group_name) values (?,?,?,?,?,?,?)"); ps.setString(1, customization.getUnit()); ps.setLong(2, customization.getProjectId()); ps.setString(3, customization.getName()); ps.setString(4, customization.getDescription()); ps.setString(5, customization.getType()); ps.setString(6, customization.getSubtype()); ps.setString(7, customization.getGroupName()); logger.info(ps);/*from w w w . ja va 2s . c o m*/ ps.execute(); ResultSet rs = ps.getGeneratedKeys(); if (rs.next()) { return rs.getLong(1); } return null; }
From source file:com.surfs.storage.common.datasource.jdbc.JdbcDao.java
@Override public Object insert(String poolName, String sql, Object... params) throws Exception { Connection conn = null;//from www . j av a2s .c o m PreparedStatement ps = null; ResultSet rs = null; try { conn = getConnection(poolName); ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); for (int i = 0; i < params.length; i++) { if (params[i] instanceof String) ps.setString(i + 1, (String) params[i]); else if (params[i] instanceof Integer) ps.setInt(i + 1, (Integer) params[i]); else if (params[i] instanceof Long) ps.setLong(i + 1, (Long) params[i]); else if (params[i] instanceof Timestamp) ps.setTimestamp(i + 1, (Timestamp) params[i]); } ps.execute(); rs = ps.getGeneratedKeys(); if (rs.next()) return rs.getObject(1); return null; } catch (Exception e) { throw e; } finally { JdbcUtils.closeResultset(rs); JdbcUtils.closeStatement(ps); JdbcUtils.closeConnect(conn); } }
From source file:org.wso2.carbon.identity.oauth.dao.TestOAuthDAOBase.java
protected int createBaseOAuthApp(String databaseName, String clientId, String secret, String username, String appName, String callback, String appState, String backchannelLogout) throws Exception { PreparedStatement statement = null; try (Connection connection = getConnection(databaseName)) { statement = connection.prepareStatement(ADD_OAUTH_APP_SQL); statement.setString(1, clientId); statement.setString(2, secret);/*from ww w . java 2 s .co m*/ statement.setString(3, username); statement.setInt(4, -1234); statement.setString(5, "PRIMARY"); statement.setString(6, appName); statement.setString(7, "OAuth-2.0"); statement.setString(8, callback); statement.setString(9, "password"); statement.setString(10, appState); statement.setString(11, backchannelLogout); statement.execute(); ResultSet resultSet = statement.getGeneratedKeys(); if (resultSet.next()) { return resultSet.getInt(1); } } finally { if (statement != null) { statement.close(); } } return -1; }
From source file:org.wso2.carbon.device.mgt.core.dao.impl.AbstractApplicationDAOImpl.java
@Override public List<Integer> removeApplications(List<Application> apps, int tenantId) throws DeviceManagementDAOException { Connection conn = null;/*w w w . ja v a 2s . c om*/ PreparedStatement stmt = null; ResultSet rs = null; List<Integer> applicationIds = new ArrayList<>(); try { conn = this.getConnection(); conn.setAutoCommit(false); stmt = conn.prepareStatement("DELETE DM_APPLICATION WHERE APP_IDENTIFIER = ? AND TENANT_ID = ?", new String[] { "id" }); for (Application app : apps) { stmt.setString(1, app.getApplicationIdentifier()); stmt.setInt(2, tenantId); stmt.addBatch(); } stmt.executeBatch(); rs = stmt.getGeneratedKeys(); if (rs.next()) { applicationIds.add(rs.getInt(1)); } return applicationIds; } catch (SQLException e) { try { if (conn != null) { conn.rollback(); } } catch (SQLException e1) { log.error("Error occurred while roll-backing the transaction", e); } throw new DeviceManagementDAOException("Error occurred while removing bulk application list", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } }