Pegar último id da tabela[Resolvido]

3 respostas
T

Tenho uma tabela de clientes e quero pegar o ultimo id da tabela que é um campo autoincrement.
To fazendo o código abaixo mas ta dando erro, alguem tem uma dica?

[color=darkblue]	public int getUltimoId() throws SQLException{

String sql = SELECT MAX(ID) FROM CLIENTES;

PreparedStatement stmt = (PreparedStatement) connection.prepareStatement(sql);

ResultSet rs = stmt.executeQuery();

int lastId = rs.getInt(id);
rs.close();
	stmt.close();
		
	return lastId;
}[/color]

dá esso erro:

[color=darkred]

java.sql.SQLException: Column ‘id’ not found.

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)

at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1144)

at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2794)

at webwork.dao.ClienteDAO.getUltimoId(ClienteDAO.java:118)

at webwork.fachada.Fachada.getLastIdCli(Fachada.java:360)

at webwork.teste.ListaClienteTeste.main(ListaClienteTeste.java:26)

[/color]

Diz que a coluna id nao existe. Mas ela existe. no meu metodo listar faço coisa parecida e funciona:

[color=darkblue]public List<Cliente> listar() throws SQLException{

String sql = "SELECT * FROM CLIENTES";
	PreparedStatement stmt = (PreparedStatement) connection.prepareStatement(sql);
	ResultSet rs = stmt.executeQuery();
	
	List&lt;Cliente&gt; clientes = new ArrayList&lt;Cliente&gt;();
	
	while(rs.next()){
		Cliente cliente = new Cliente();
		cliente.setId(rs.getInt("id"));
            clientes.add(cliente);
	}
	
	rs.close();
	stmt.close();
	
	return clientes;

}[/color]

3 Respostas

M

Boa Tarde.
Troque o sua query de:

SELECT MAX(ID) FROM CLIENTES

para:

SELECT MAX(ID) as id FROM CLIENTES

ou voce devera pedir o campo desta forma:

int lastId = rs.getInt(“MAX(ID)”);

:wink:

Espero que ajude.

Att,

Lucas Israel

T

McLuck:
Boa Tarde.
Troque o sua query de:

SELECT MAX(ID) FROM CLIENTES

para:

SELECT MAX(ID) as id FROM CLIENTES

ou voce devera pedir o campo desta forma:

int lastId = rs.getInt(“MAX(ID)”);

:wink:

Espero que ajude.

Att,

Lucas Israel

Obrigado por responder Lucas.

Fiz o que vc sugeriu: SELECT MAX(ID) as id FROM CLIENTES
mas mesmo assim ainda ta dando erro:

[color=darkred]

java.sql.SQLException: Before start of result set

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)

at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841)

at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2653)

at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2794)

at webwork.dao.ClienteDAO.getLastId(ClienteDAO.java:118)

Conectado!!

Conectado!!

id: 41

nome: Thiago

endereço: asdfasfd

id: 42
nome: qqqq
endereço: asdfasfasdf

id: 43
nome: aa
endereço: asdf

at webwork.fachada.Fachada.getLastIdCli(Fachada.java:360)
at webwork.teste.ListaClienteTeste.main(ListaClienteTeste.java:26)

[/color]

essa linha aí que o erro se refere é essa aqui: int lastId = rs.getInt(“id”);

T

Descobri pesquisando aqui no fórum mesmo.

tava faltando eu colocar a linha: rs.next(); antes de int lastId = rs.getInt(“id”);

ficou assim o código:

[color=blue]

public int getLastId() throws SQLException{

String sql = “SELECT MAX(ID) as id FROM CLIENTES”;

PreparedStatement stmt = (PreparedStatement) connection.prepareStatement(sql);

ResultSet rs = stmt.executeQuery();

rs.next();

int lastId = rs.getInt(“id”);
rs.close();
	stmt.close();
		
	return lastId;
}

[/color]

Criado 31 de janeiro de 2010
Ultima resposta 31 de jan. de 2010
Respostas 3
Participantes 2