Statement getGeneratedKeys - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Statement getGeneratedKeys method.

This code example shows how to use the following methods:executeUpdate, setString, close, getGeneratedKeys
    protected int addLookupEntity(Connection connection, String name, String description)  throws SQLException {
       int newId = -1;
       PreparedStatement statement = connection.prepareStatement(
               "insert into lookup_entity(entity_id,entity_name,description) values(null,?,?)", 
                PreparedStatement.RETURN_GENERATED_KEYS);
       statement.setString(1, name);
       statement.setString(2, description);
       statement.executeUpdate();
       ResultSet keys = statement.getGeneratedKeys();
       keys.next();
       newId = Integer.parseInt(keys.getString(1));
       statement.close();
       return newId;
    } 
 
 
    public void setUpgradeContext(ApplicationContext upgradeContext) {
        this.upgradeContext = upgradeContext;
    } 
 
7
CodeRank
This code example shows how to use the following methods:execute, setInt, setString, getGeneratedKeys
 
        Long userId=null;
        try { 
            con = DBUtils.getConn();
            PreparedStatement stmt = con.prepareStatement("insert into system (display_nm, user, host, port, authorized_keys, status_cd) values (?,?,?,?,?,?)",PreparedStatement.RETURN_GENERATED_KEYS );
            stmt.setString(1, hostSystem.getDisplayNm());
            stmt.setString(2, hostSystem.getUser());
            stmt.setString(3, hostSystem.getHost());
            stmt.setInt(4, hostSystem.getPort());
            stmt.setString(5, hostSystem.getAuthorizedKeys());
            stmt.setString(6, hostSystem.getStatusCd());
            stmt.execute();
 
           ResultSet rs =stmt.getGeneratedKeys();
            if(rs.next()){
                userId=rs.getLong(1);
            } 
            DBUtils.closeStmt(stmt);
 
        } catch (Exception e) {
7
CodeRank
This code example shows how to use the following methods:execute, setLong, getGeneratedKeys
        Long sessionId = null;
        try { 
 
            //insert 
            PreparedStatement stmt = con.prepareStatement("insert into session_log (user_id) values(?)", Statement.RETURN_GENERATED_KEYS);
            stmt.setLong(1, userId);
            stmt.execute();
            ResultSet rs = stmt.getGeneratedKeys();
            if (rs != null && rs.next()) {
                sessionId = rs.getLong(1);
            } 
 
            DBUtils.closeStmt(stmt);
 
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return sessionId;
 
    } 
7
CodeRank
This code example shows how to use the following methods:executeUpdate, setBoolean, setInt, setString, getGeneratedKeys
		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);
			throw new UserStoreException(error);
		} 
		res = stmt.getGeneratedKeys();
		if (res.next()) {
			setId(res.getInt(1));
		} 

7
CodeRank
This code example shows how to use the following methods:execute, setDate, setString, close, getGeneratedKeys
6
CodeRank
This code example shows how to use the following methods:executeUpdate, setInt, setString, getGeneratedKeys
		String sql =
		             "INSERT INTO UM_PERMISSION (UM_RESOURCE_ID, UM_MODULE_ID, UM_TENANT_ID, UM_ACTION) VALUES(?,?,?,?) "; 
 
		stmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
		byte count = 0;
		stmt.setString(++count, getResourceId());
		stmt.setInt(++count, getModuleId());
		stmt.setInt(++count, getTenantId());
		stmt.setString(++count, getAction());
 
		int resCount = stmt.executeUpdate();
		if (resCount == 0) {
			String error = "Insertion faild for the permission";
			log.error(error);
			throw new UserStoreException(error);
		} 
		res = stmt.getGeneratedKeys();
		if (res.next()) {
			setPermissionId(res.getInt(1));
		} 

6
CodeRank
This code example shows how to use the following methods:executeUpdate, setString, close, getGeneratedKeys
			 
//			sql = "INSERT INTO user ( email, username, pass ) " 
//               + "select ?, ?, ? from user where not exists(select * from user as a where a.email=? or a.username=?) "; 
			stmt = conn.prepareStatement(sql);
 
			stmt.setString(1, uo.getE());
			stmt.setString(2, uo.getN());
			stmt.setString(3, uo.getP());
			rowcount = stmt.executeUpdate();
			 
			if (rowcount != 1) {
				isScucces = false;
				throw new SQLException("PrimaryKey Error when updating DB!");
			} 
			ResultSet rs = stmt.getGeneratedKeys();
			if (rs.first()) {
				uo.setId(rs.getInt(1));
			} 
 
		}catch (Exception e) {

6
CodeRank
This code example shows how to use the following methods:executeUpdate, setBytes, setFloat, setInt, setNull
				statement = connection.prepareStatement("UPDATE `players` SET username = ?, password = ?, x = ?, y = ?, plane = ?, appearance = ?, energy = ? WHERE id = ?");
			} 
			 
			int index = 1;
			 
			statement.setString(index++, player.username());
			statement.setString(index++, player.password());
			statement.setInt(index++, player.position().x());
			statement.setInt(index++, player.position().y());
			statement.setInt(index++, player.position().z());
			saveObject(index++, statement, player.appearance());
			statement.setFloat(index++, ((PlayerMovementHandler)player.movement()).energy());
			 
			if (exists)
			{ 
				statement.setInt(index++, player.id());
			} 
			 
			statement.executeUpdate();
			 

6
CodeRank
This code example shows how to use the following methods:executeUpdate, setInt, setString, close, getGeneratedKeys
        Connection con = Conexao.getConnection();
        PreparedStatement ps = con.prepareStatement(
                "insert into meta(Program_idProgram,meta) values (?,?)", 
                Statement.RETURN_GENERATED_KEYS);
 
        ps.setInt(1, obj.getIteractions().get(i).getProgram().getIdProgram());
        ps.setString(2, obj.getIteractions().get(i).getProgram().getMetas().get(k).getMeta());
 
        ps.executeUpdate();
 
        ResultSet rskey = null;
        rskey = ps.getGeneratedKeys();
 
        if (rskey != null && rskey.next()) {
            obj.getIteractions().get(i).getProgram().getMetas().get(k).setIdMeta(rskey.getInt(1));
        } 
 
        ps.close();
        con.close();
    } 

6
CodeRank
This code example shows how to use the following methods:executeUpdate, setInt, close, getGeneratedKeys
        int generKey = 0; 
        try { 
            accessConn = MySQLDAOFactory.createConnection(); 
 
            PreparedStatement query = accessConn.prepareStatement("Insert into worker_request (idworker, idrequest) values (?, ?)", Statement.RETURN_GENERATED_KEYS); 
            query.setInt(1, idworker); 
            query.setInt(2, idrequest); 
            query.executeUpdate(); 
            ResultSet rs = query.getGeneratedKeys(); 
            if (rs.next()) { 
                generKey = rs.getInt(1); 
            } 
            rs.close(); 
            query.close(); 
        } catch (SQLException ex) { 
            System.out.print(ex.getMessage()); 
        } catch (NamingException ex) { 
            System.out.println(ex.getMessage()); 
        } finally { 
            if (accessConn != null) { 
6
CodeRank
getGeneratedKeys Method Overview
Retrieves any auto-generated keys created as a result of executing this Statement object.
See Code Examples for other Java 8 Statement Methods: