当前位置: 首页>>代码示例>>Java>>正文


Java Statement.getGeneratedKeys方法代码示例

本文整理汇总了Java中java.sql.Statement.getGeneratedKeys方法的典型用法代码示例。如果您正苦于以下问题:Java Statement.getGeneratedKeys方法的具体用法?Java Statement.getGeneratedKeys怎么用?Java Statement.getGeneratedKeys使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.sql.Statement的用法示例。


在下文中一共展示了Statement.getGeneratedKeys方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testBug71672Statement

import java.sql.Statement; //导入方法依赖的package包/类
/**
 * Check the update count and returned keys for an INSERT query using a Statement object. If expectedUpdateCount < 0 then runs Statement.execute() otherwise
 * Statement.executeUpdate().
 */
public void testBug71672Statement(int testStep, Connection testConn, String query, int expectedUpdateCount, int[] expectedKeys) throws SQLException {
    Statement testStmt = testConn.createStatement();

    if (expectedUpdateCount < 0) {
        assertFalse(testStep + ". Stmt.execute() result", testStmt.execute(query, Statement.RETURN_GENERATED_KEYS));
    } else {
        assertEquals(testStep + ". Stmt.executeUpdate() result", expectedUpdateCount, testStmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS));
    }

    ResultSet testRS = testStmt.getGeneratedKeys();
    for (int k : expectedKeys) {
        assertTrue(testStep + ". Row expected in generated keys ResultSet", testRS.next());
        assertEquals(testStep + ". Wrong generated key", k, testRS.getInt(1));
    }
    assertFalse(testStep + ". No more rows expected in generated keys ResultSet", testRS.next());
    testRS.close();
    testStmt.close();
}
 
开发者ID:rafallis,项目名称:BibliotecaPS,代码行数:23,代码来源:StatementRegressionTest.java

示例2: create

import java.sql.Statement; //导入方法依赖的package包/类
public static LogErro create(LogErro logErro) throws SQLException {
    Statement stm
            = Database.createConnection().
                    createStatement();
    String sql
            = "INSERT INTO logs_erros (`usuario`, `data`, `erro`) VALUES ('"
            + logErro.getUsuario().getId() + "','"
            + new Timestamp((System.currentTimeMillis())) + "','"
            + logErro.getErro() + "')";

    stm.execute(sql, Statement.RETURN_GENERATED_KEYS);
    ResultSet rs = stm.getGeneratedKeys();
    rs.next();
    int key = rs.getInt(1);
    logErro.setId(key);
    return logErro;
}
 
开发者ID:Montanheiro,项目名称:SistemaAlmoxarifado,代码行数:18,代码来源:LogErroDAO.java

示例3: create

import java.sql.Statement; //导入方法依赖的package包/类
public static Usuario create(Usuario usuario) throws SQLException {
    Statement stm
            = Database.createConnection().
                    createStatement();
    String sql
            = "INSERT INTO usuarios (`email`, `senha`, `status`, `admin`, `servidor`) VALUES ('"
            + usuario.getEmail() + "','"
            + usuario.getSenha() + "','"
            + usuario.isAtivo() + "','"
            + usuario.isAdmin() + "','"
            + usuario.getServidor().getId() + "')";

    stm.execute(sql, Statement.RETURN_GENERATED_KEYS);
    ResultSet rs = stm.getGeneratedKeys();
    rs.next();
    usuario.setId(rs.getInt(1));
    return usuario;
}
 
开发者ID:Montanheiro,项目名称:SistemaAlmoxarifado,代码行数:19,代码来源:UsuarioDAO.java

示例4: create

import java.sql.Statement; //导入方法依赖的package包/类
public static Produto create(Produto produto) throws SQLException {
    Statement stm
            = Database.createConnection().
                    createStatement();
    String sql
            = "INSERT INTO produtos (`descricao`, `unidade`, `estoque_minimo`) VALUES ('"
            + produto.getDescricao() + "','"
            + produto.getUnidade().getId() + "','"
            + produto.getEstoque_minimo() + "')";

    stm.execute(sql, Statement.RETURN_GENERATED_KEYS);
    ResultSet rs = stm.getGeneratedKeys();
    rs.next();
    int key = rs.getInt(1);
    produto.setId(key);
    return produto;
}
 
开发者ID:Montanheiro,项目名称:SistemaAlmoxarifado,代码行数:18,代码来源:ProdutoDAO.java

示例5: create

import java.sql.Statement; //导入方法依赖的package包/类
public static EntradaItem create(EntradaItem entradaItem) throws SQLException {
    Statement stm
            = Database.createConnection().
                    createStatement();
    String sql
            = "INSERT INTO entrada_itens (`produto`, `entrada`, `quantidade`,"
            + "`validade`, `lote`, `valor_unitario`) VALUES ('"
            + entradaItem.getProduto().getId() + "','"
            + entradaItem.getEntradaId() + "','"
            + entradaItem.getQtd() + "','"
            + entradaItem.getValidade() + "','"
            + entradaItem.getLote() + "','"
            + entradaItem.getValor_unitario() + "')";

    stm.execute(sql, Statement.RETURN_GENERATED_KEYS);
    ResultSet rs = stm.getGeneratedKeys();
    rs.next();
    int key = rs.getInt(1);
    entradaItem.setId(key);
    return entradaItem;
}
 
开发者ID:Montanheiro,项目名称:SistemaAlmoxarifado,代码行数:22,代码来源:EntradaItemDAO.java

示例6: create

import java.sql.Statement; //导入方法依赖的package包/类
public static Servidor create(Servidor servidor) throws SQLException {
    Statement stm
            = Database.createConnection().
                    createStatement();
    String sql
            = "INSERT INTO servidores (`nome`, `siape`, `funcao`, `setor`) VALUES ('"
            + servidor.getNome() + "','"
            + servidor.getSiape() + "','"
            + servidor.getFuncao() + "','"
            + servidor.getSetor().getId() + "')";

    stm.execute(sql, Statement.RETURN_GENERATED_KEYS);
    ResultSet rs = stm.getGeneratedKeys();
    rs.next();
    servidor.setId(rs.getInt(1));
    return servidor;
}
 
开发者ID:Montanheiro,项目名称:SistemaAlmoxarifado,代码行数:18,代码来源:ServidorDAO.java

示例7: getAutoGeneratedKey

import java.sql.Statement; //导入方法依赖的package包/类
private int getAutoGeneratedKey(Statement statement) throws SQLException {
    int key = 0;

    ResultSet resultSet = statement.getGeneratedKeys();
    if (resultSet == null) {
        Log.warn(SQL, "No auto generated key found.");
        return -1;
    }

    while(resultSet.next())
        key = resultSet.getInt(1);

    Log.debug(SQL, "The auto generated key is '"+key+"'.");

    return key;
}
 
开发者ID:tbressler,项目名称:waterrower-workout,代码行数:17,代码来源:DatabaseConnectionService.java

示例8: create

import java.sql.Statement; //导入方法依赖的package包/类
public static HistoricoRelatorio create(HistoricoRelatorio historico) throws SQLException {
    Statement stm
            = Database.createConnection().
                    createStatement();
    String sql
            = "INSERT INTO historico_relatorios (`produto`, `mes`, "
            + "`quantidade`, `valortotal`) VALUES ('"
            + historico.getProduto().getId() + "','"
            + historico.getMes() + "','"
            + historico.getQtd() + "','"
            + historico.getValorTotal() + "')";

    stm.execute(sql, Statement.RETURN_GENERATED_KEYS);
    ResultSet rs = stm.getGeneratedKeys();
    rs.next();
    historico.setId(rs.getInt(1));
    return historico;
}
 
开发者ID:Montanheiro,项目名称:SistemaAlmoxarifado,代码行数:19,代码来源:HistoricoRelatorioDAO.java

示例9: create

import java.sql.Statement; //导入方法依赖的package包/类
public static EmpenhoItem create(EmpenhoItem item) throws SQLException {
    Statement stm = Database.createConnection().createStatement();
    String sql
            = "INSERT INTO itens_empenho (`empenho`, `natureza_despesa`, `produto`,"
            + "`sequencia`, `quantidade`, `valor_unitario`, `item_processo`) VALUES ('"
            + item.getEmpenhoId() + "','"
            + item.getNaturezaDespesa().getId() + "','"
            + item.getProduto().getId() + "','"
            + item.getSequencia() + "','"
            + item.getQtd() + "','"
            + item.getValorUnitario() + "','"
            + item.getItemProcesso() + "')";

    stm.execute(sql, Statement.RETURN_GENERATED_KEYS);
    ResultSet rs = stm.getGeneratedKeys();
    rs.next();
    item.setId(rs.getInt(1));
    return item;
}
 
开发者ID:Montanheiro,项目名称:SistemaAlmoxarifado,代码行数:20,代码来源:EmpenhoItemDAO.java

示例10: create

import java.sql.Statement; //导入方法依赖的package包/类
public static Empenho create(Empenho empenho) throws SQLException {
    Statement stm
            = Database.createConnection().
                    createStatement();
    String sql
            = "INSERT INTO empenhos (`fornecedor`, `emissao`, `numero`, `observacao`, `valor_total`) VALUES ('"
            + empenho.getFornecedor().getId() + "','"
            + empenho.getEmissao() + "','"
            + empenho.getNumero() + "','"
            + empenho.getObservacao() + "','"
            + empenho.getValorTotal() + "')";

    stm.execute(sql, Statement.RETURN_GENERATED_KEYS);
    ResultSet rs = stm.getGeneratedKeys();
    rs.next();
    int key = rs.getInt(1);
    empenho.setId(key);
    
    if(empenho.getItens() != null){
        for (EmpenhoItem item : empenho.getItens()) {
            item.setEmpenhoId(key);
            EmpenhoItemDAO.create(item);
            
            if(ProdutoFornecedorDAO.checkNotExist(
                    empenho.getFornecedor().getId(), 
                    item.getProduto().getId()))
                ProdutoFornecedorDAO.create(
                        new ProdutoFornecedor(empenho.getFornecedor().getId(), 
                                item.getProduto().getId()));
        }
    }
    
    return empenho;
}
 
开发者ID:Montanheiro,项目名称:SistemaAlmoxarifado,代码行数:35,代码来源:EmpenhoDAO.java

示例11: create

import java.sql.Statement; //导入方法依赖的package包/类
public static Visitante create(Visitante visitante) throws SQLException {
    Statement stm
            = Database.createConnection().
                    createStatement();
    String sql
            = "INSERT INTO visitantes (`quantidade`, `setor`) VALUES ('"
            + visitante.getQtd() + "','"
            + visitante.getSetor().getId() + "')";

    stm.execute(sql, Statement.RETURN_GENERATED_KEYS);
    ResultSet rs = stm.getGeneratedKeys();
    rs.next();
    visitante.setId(rs.getInt(1));
    return visitante;
}
 
开发者ID:Montanheiro,项目名称:SistemaAlmoxarifado,代码行数:16,代码来源:VisitanteDAO.java

示例12: getId

import java.sql.Statement; //导入方法依赖的package包/类
/**
 * Retorna o ID que foi gerado no banco de dados
 * @param stmt comando enviado
 * @return ID gerado pelo banco de dados
 * @throws SQLException 
 */
protected Integer getId(Statement stmt) throws SQLException {
    ResultSet chavesGeradas = stmt.getGeneratedKeys();
    
    if (chavesGeradas.next()){
        return chavesGeradas.getInt(1);
    } else {
        return null;
    }
}
 
开发者ID:Ronneesley,项目名称:redesocial,代码行数:16,代码来源:DAOBase.java

示例13: create

import java.sql.Statement; //导入方法依赖的package包/类
public static Unidade create(Unidade unidade) throws SQLException {
    Statement stm
            = Database.createConnection().
                    createStatement();
    String sql
            = "INSERT INTO unidades (`nome`, `abreviacao`) VALUES ('"
            + unidade.getNome() + "','"
            + unidade.getAbreviacao() + "')";

    stm.execute(sql, Statement.RETURN_GENERATED_KEYS);
    ResultSet rs = stm.getGeneratedKeys();
    rs.next();
    unidade.setId(rs.getInt(1));
    return unidade;
}
 
开发者ID:Montanheiro,项目名称:SistemaAlmoxarifado,代码行数:16,代码来源:UnidadeDAO.java

示例14: create

import java.sql.Statement; //导入方法依赖的package包/类
public static Fornecedor create(Fornecedor fornecedor) throws SQLException {
    Statement stm
            = Database.createConnection().
                    createStatement();
    String sql
            = "INSERT INTO fornecedores (`razao_social`, `nome_fantasia`, `cnpj`) VALUES ('"
            + fornecedor.getRazaoSocial() + "','"
            + fornecedor.getNomeFantasia() + "','"
            + fornecedor.getCnpj() + "')";
    stm.execute(sql, Statement.RETURN_GENERATED_KEYS);
    ResultSet rs = stm.getGeneratedKeys();
    rs.next();
    int key = rs.getInt(1);
    fornecedor.setId(key);
    
    if(fornecedor.getEndereco() != null){
        for (Endereco end : fornecedor.getEndereco()) {
            end.setFornecedorId(key);
            EnderecoDAO.create(end);
        }
    }
    
    if(fornecedor.getContato() != null){
        for (Contato contato : fornecedor.getContato()) {
            contato.setFornecedorId(key);
            ContatoDAO.create(contato);
        }
    }
    

    return fornecedor;
}
 
开发者ID:Montanheiro,项目名称:SistemaAlmoxarifado,代码行数:33,代码来源:FornecedorDAO.java

示例15: exeSQLInsert

import java.sql.Statement; //导入方法依赖的package包/类
@Override
public int exeSQLInsert(String sql) {
	// TODO Auto-generated method stub
	ConnectionObject conn = ConnectionManager.borrowConnectionObject(connInfo);
	try 
	{
		Statement stat = conn.getConnection().createStatement();
		//stat.executeUpdate(sql);
		int autoGeneratedKeys = 0;
		stat.executeUpdate(sql);
		ResultSet rs = stat.getGeneratedKeys();
		if(rs.next())
		{
			autoGeneratedKeys = rs.getInt(1);
		}
		stat.close();
		return autoGeneratedKeys;
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		DBCException.logException(DBCException.E_SQL, e);
		return 0;
	}
	finally
	{
		ConnectionManager.returnConnectionObject(conn);
	}
}
 
开发者ID:shaogaige,项目名称:iDataBaseConnection,代码行数:29,代码来源:DataBase.java


注:本文中的java.sql.Statement.getGeneratedKeys方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。