List of usage examples for java.sql Statement RETURN_GENERATED_KEYS
int RETURN_GENERATED_KEYS
To view the source code for java.sql Statement RETURN_GENERATED_KEYS.
Click Source Link
From source file:zerogame.info.javapay.dao.UserDao.java
public Player add(final long uin, final int channel, final String accountId, final String accountName) { Player user = new Player(); try {/*from w ww. java2s. c om*/ KeyHolder keyHolder = new GeneratedKeyHolder(); getTemplate().update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection con) throws SQLException { PreparedStatement ps = con.prepareStatement( "insert into player(uin,channel,serverid,accountId,accountName) values(?,?,?,?,?)", Statement.RETURN_GENERATED_KEYS); ps.setString(1, String.valueOf(uin)); ps.setString(2, String.valueOf(channel)); ps.setString(3, accountId); ps.setString(4, accountName); return ps; } }, keyHolder); user.setUin(keyHolder.getKey().intValue()); return user; } catch (Exception e) { logger.warn("add account failed", e); return null; } }
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);// ww w .java 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 ava 2s . com throw new UserStoreException(error); } res = stmt.getGeneratedKeys(); if (res.next()) { setId(res.getInt(1)); } }
From source file:mupomat.controller.ObradaKorisnik.java
@Override public Korisnik dodajNovi(Korisnik entitet) { try {/* ww w . j av a 2 s .c om*/ Connection veza = MySqlBazaPodataka.getConnection(); veza.setAutoCommit(false); PreparedStatement izraz = veza .prepareStatement("insert into osoba (oib,ime,prezime,email) values (?,?,?,?)"); izraz.setString(1, entitet.getOib()); izraz.setString(2, entitet.getIme()); izraz.setString(3, entitet.getPrezime()); izraz.setString(4, entitet.getEmail()); izraz.executeUpdate(); izraz = veza.prepareStatement( "insert into korisnik (oib,automat,datumregistracije,korisnickoime,lozinka,uloga,aktivan) values (?,?,now(),?,?,?,?)", Statement.RETURN_GENERATED_KEYS); izraz.setString(1, entitet.getOib()); izraz.setString(2, entitet.getAutomat()); izraz.setString(3, entitet.getKorisnickoIme()); izraz.setString(4, DigestUtils.md5Hex(entitet.getLozinka())); izraz.setString(5, entitet.getUloga()); izraz.setBoolean(6, entitet.isAktivan()); izraz.executeUpdate(); ResultSet rs = izraz.getGeneratedKeys(); rs.next(); entitet.setSifra(rs.getInt(1)); izraz.close(); veza.commit(); veza.close(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); return null; } return entitet; }
From source file:eclserver.db.objects.ContactsDao.java
public ContactsDao(Connection connection, String addressListName) { this.objectName = addressListName; try {/*from w w w . j a v a 2 s .c om*/ System.out.println("Received connection from factory... building Addresses List. \n"); dbConnection = connection; stmtGetListEntries = dbConnection.prepareStatement(strGetListEntries); stmtSaveNewRecord = dbConnection.prepareStatement(strSaveAddress, Statement.RETURN_GENERATED_KEYS); stmtUpdateExistingRecord = dbConnection.prepareStatement(strUpdateAddress); stmtGetAddress = dbConnection.prepareStatement(strGetAddress); stmtDeleteAddress = dbConnection.prepareStatement(strDeleteAddress); stmtNukeAddresses = dbConnection.prepareStatement(strNukeAddresses); } catch (SQLException ex) { System.out.println("Exception creating AddressDAO: " + ex.getSQLState() + ex.getMessage()); } }
From source file:com.github.viktornar.dao.ExtentDao.java
public void create(Extent extent) { String sql = format("INSERT INTO EXTENT (%s) VALUES (%s)", EXTENT_COLUMNS, questionMarks(EXTENT_COLUMNS)); KeyHolder keyHolder = new GeneratedKeyHolder(); getJdbcTemplate().update(connection -> { PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); ps.setDouble(1, extent.getXmin()); ps.setDouble(2, extent.getYmin()); ps.setDouble(3, extent.getXmax()); ps.setDouble(4, extent.getYmax()); return ps; }, keyHolder);/*w ww . j a v a 2 s . c o m*/ extent.setId((Integer) keyHolder.getKey()); }
From source file:edu.pitt.sis.infsci2730.finalProject.dao.AddressDao.java
/** * add new AddressDBModel/* www .j a v a 2s . c o m*/ * * @param para * @return */ // public static int addAddress(final String[] para) throws SQLException { // String sql = "insert into Address (city,street,state_,zipCode) values (?,?,?,?)"; // return jdbcTemplate.update(sql, // para, // new int[]{java.sql.Types.VARCHAR, java.sql.Types.VARCHAR, java.sql.Types.VARCHAR, java.sql.Types.VARCHAR}); // } public static Long addAddress(final String[] para) throws SQLException { KeyHolder holder = new GeneratedKeyHolder(); jdbcTemplate.update(new PreparedStatementCreator() { String sql = "insert into Address (city,street,state_,zipCode) values (?,?,?,?)"; @Override public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement ps = connection.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS); ps.setString(1, para[0]); ps.setString(2, para[1]); ps.setString(3, para[2]); ps.setString(4, para[3]); return ps; } }, holder); Long newPersonId = holder.getKey().longValue(); return newPersonId; }
From source file:com.nortal.petit.core.dialect.PostgreSqlDialect.java
@SuppressWarnings("unchecked") @Override/* w w w . j av a 2 s .c o m*/ public <B> B insertReturningId(JdbcOperations jdbcOperations, final String sql, String idColumn, final Object... params) { KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcOperations.update(new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection con) throws SQLException { PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); Object[] queryParams = params.length == 1 && params[0] instanceof Object[] ? (Object[]) params[0] : params; ArgPreparedStatementSetter.setValues(ps, queryParams, 1); return ps; } }, keyHolder); return (B) keyHolder.getKeys().get(idColumn); }
From source file:com.adanac.module.blog.dao.MessageDao.java
public Integer save(final String visitorIp, final Date createDate, final String message, final String username, final Integer referenceMessageId) { return execute(new TransactionalOperation<Integer>() { @Override/*from w w w.j a va 2s .c o m*/ public Integer doInConnection(Connection connection) { try { PreparedStatement statement = null; if (referenceMessageId == null) { statement = connection.prepareStatement( "insert into messages (visitor_ip,city,message," + "create_date,username) values (?,?,?,?,?)", Statement.RETURN_GENERATED_KEYS); } else { statement = connection.prepareStatement( "insert into messages (visitor_ip,city,message," + "create_date,username,reference_message_id) values (?,?,?,?,?,?)", Statement.RETURN_GENERATED_KEYS); } statement.setString(1, visitorIp); statement.setString(2, Configuration.isProductEnv() ? HttpApiHelper.getCity(visitorIp) : "?"); statement.setString(3, message); Date finalCommentDate = createDate; if (createDate == null) { finalCommentDate = new Date(); } statement.setTimestamp(4, new Timestamp(finalCommentDate.getTime())); statement.setString(5, username); if (referenceMessageId != null) { statement.setInt(6, referenceMessageId); } int result = statement.executeUpdate(); if (result > 0) { ResultSet resultSet = statement.getGeneratedKeys(); if (resultSet.next()) { return resultSet.getInt(1); } } } catch (SQLException e) { error("save messages failed ...", e); } return null; } }); }
From source file:org.tec.webapp.jdbc.entity.impl.UserRoleDbaImpl.java
/** {@inheritDoc} */ @Override()/*from ww w.j a v a2 s.c om*/ public Long insert(UserRoleBean userRole) { ParameterMap params = getParams(userRole); PreparedStatementCreator creator = PreparedStatementBuilder.getInsertBuilder(TABLE, params, Statement.RETURN_GENERATED_KEYS); return insert(creator); }