Uma coisa quando se traz um único registro não precisa fazer while um simples if (res.next()) { é o suficiente.
Código Exemplo:
@Override
public Funcionario find(Long id) {
PreparedStatement st;
Funcionario func = null;
try {
st = connection
.prepareStatement("SELECT id, nome FROM funcionario WHERE id=? limit 1", true);
st.setLong(1, id);
try (ResultSet res = st.executeQuery()) {
if (res.next())
{
func = new Funcionario(res.getLong(1), res.getString(2));
}
}
st.close();
return func;
} catch (SQLException | ClassNotFoundException ex) {
Logger.getLogger(DalFuncionario.class.getName()).log(Level.SEVERE, null, ex);
}
return func;
}
Para utilizar:
Connection connection = new Connection();
DalFuncionario dalFunc = new DalFuncionario(connection);
Funcionario func = dalFunc.find(2L);
System.out.println(func.getId() + " " + func.getNome());
Dessa forma funciona perfeitamente: Aviso, não crie métodos como static, crie um Interface genérica para que seus métodos DAL fiquem todos padrão.
Exemplo:
public interface IDal<T, T1> {
public T insert(T model);
public boolean edit(T model);
public T find(T1 id);
}
Implementação:
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DalFuncionario implements IDal<Funcionario, Long>{
private final Connection connection;
public DalFuncionario(Connection connection) {
this.connection = connection;
}
@Override
public Funcionario insert(Funcionario model) {
try {
PreparedStatement st = connection
.prepareStatement("INSERT INTO funcionario(nome) values(?)", true);
st.setString(1, model.getNome());
if (st.executeUpdate() > 0) {
try (ResultSet res = st.getGeneratedKeys()) {
if (res.next()){
model.setId(res.getInt(1));
res.close();
st.close();
return model;
}
}
st.close();
}
} catch (SQLException | ClassNotFoundException ex) {
Logger.getLogger(DalFuncionario.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
@Override
public boolean edit(Funcionario model) {
try {
PreparedStatement st = connection
.prepareStatement("UPDATE funcionario SET nome=? WHERE id =?", true);
st.setString(1, model.getNome());
st.setLong(2, model.getId());
return (st.executeUpdate() > 0);
} catch (SQLException | ClassNotFoundException ex) {
Logger.getLogger(DalFuncionario.class.getName()).log(Level.SEVERE, null, ex);
}
return Boolean.FALSE;
}
@Override
public Funcionario find(Long id) {
PreparedStatement st;
Funcionario func = null;
try {
st = connection
.prepareStatement("SELECT id, nome FROM funcionario WHERE id=? limit 1", true);
st.setLong(1, id);
try (ResultSet res = st.executeQuery()) {
if (res.next())
{
func = new Funcionario(res.getLong(1), res.getString(2));
}
}
st.close();
return func;
} catch (SQLException | ClassNotFoundException ex) {
Logger.getLogger(DalFuncionario.class.getName()).log(Level.SEVERE, null, ex);
}
return func;
}
}
Classe Connection
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class Connection {
private java.sql.Connection connect = null;
public Connection() throws ClassNotFoundException, SQLException
{
open();
}
public java.sql.Connection open() throws SQLException, ClassNotFoundException
{
if (connect == null)
{
Class.forName("com.mysql.jdbc.Driver");
connect =
DriverManager
.getConnection("jdbc:mysql://localhost/test?user=root&password=senha");
}
return connect;
}
public void close() throws Throwable
{
if (connect != null) {
connect.close();
}
connect = null;
}
public PreparedStatement prepareStatement(String sql, boolean lastInsertId) throws SQLException, ClassNotFoundException
{
if (lastInsertId){
return connect.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
}
return connect.prepareStatement(sql);
}
}