Skip to content

Instantly share code, notes, and snippets.

@handersonbf
Created May 18, 2016 14:54
Show Gist options
  • Save handersonbf/07a4125103b6facd7a6a6f6f33ab1917 to your computer and use it in GitHub Desktop.
Save handersonbf/07a4125103b6facd7a6a6f6f33ab1917 to your computer and use it in GitHub Desktop.
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import br.triadworks.javaweb.modelo.Caloteiro;
public class CaloteiroDAO {
private Connection connection;
public CaloteiroDAO(Connection connection){
this.connection = connection;
}
public void adiciona(Caloteiro caloteiro){
String sql = "insert into caloteiro " +
"(nome, email, devendo, dataDivida) " +
"values (?,?,?,?)";
try {
//preparando a insercao
PreparedStatement pstmt = connection.prepareStatement(sql);
//setando os valores
pstmt.setString(1, caloteiro.getNome());
pstmt.setString(2, caloteiro.getEmail());
pstmt.setInt(3, caloteiro.getDevendo());
pstmt.setDate(4, new Date(caloteiro.getDataDivida().getTimeInMillis()));
pstmt.execute();
pstmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public List<Caloteiro> getLista(){
try{
PreparedStatement stmt = this.connection.
prepareStatement("select * from caloteiro");
List<Caloteiro> caloteiros = new ArrayList<Caloteiro>();
Caloteiro caloteiro = null;
ResultSet rs = stmt.executeQuery();
while(rs.next()){
String nome = rs.getString("nome");
String email = rs.getString("email");
int devendo = rs.getInt("devendo");
Calendar dataDivida = Calendar.getInstance();
dataDivida.setTime(rs.getDate("dataDivida"));
//criando o objeto caloteiro
caloteiro = new Caloteiro();
caloteiro.setNome(nome);
caloteiro.setEmail(email);
caloteiro.setDevendo(new Integer(devendo));
caloteiro.setDataDivida(dataDivida);
caloteiros.add(caloteiro);
}
rs.close();
stmt.close();
return caloteiros;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment