[code]
package br.com.intacto;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class Usuarios {
private int idUsuario = 0;
private String nome = "";
private String login = "";
private String senha = "";
private String status = "";
public int getIdUsuario() {
return idUsuario;
}
public void setIdUsuario(int idUsuario) {
this.idUsuario = idUsuario;
}
public void setStatus(String status) {
this.status = status;
}
public void setNome(String nome){
this.nome = nome;
}
public String getNome(){
return this.nome;
}
public void setLogin(String login){
this.login = login;
}
public String getLogin(){
return this.login;
}
public void setSenha(String senha){
this.senha = senha;
}
public String getSenha(){
return this.senha;
}
public String getStatus(){
return this.status;
}
public void incluir () throws InstantiationException, IllegalAccessException{
Connection cn = Conexao.obterConexao();
PreparedStatement pst = null;
try{
pst = cn.prepareStatement("insert into usuarios(login,nome,senha) values(?,?,?)");
pst.setString(1, nome);
pst.setString(2, login);
pst.setString(3, senha);
pst.executeUpdate();
status = nome + " Incluido com Sucesso ";
}
catch (SQLException e ){
status = " Falha na inclusão: " + e.getMessage();
}
}
public String verificarUsuario() throws InstantiationException, IllegalAccessException{
Connection cn = Conexao.obterConexao();
try {
String q = "";
q = " select idusuario, nome from usuarios where login = ? and senha = ? ";
PreparedStatement pst = cn.prepareStatement(q);
pst.setString(1, login);
pst.setString(2, senha);
ResultSet rs = pst.executeQuery();
if(rs.next()){
idUsuario = rs.getInt("idusuario");
nome = rs.getString("nome");
status = nome + " localizado!";
return "cadastrado";
}
else{
status = "Login incorreto!";
return "naocadastrado";
}
} catch (SQLException e) {
status = " Falha: " + e.getMessage();
return "falha";
}
}
public ResultSet getLista() throws InstantiationException, IllegalAccessException {
ResultSet rs = null;
Connection cn = Conexao.obterConexao();
PreparedStatement pst = null;
try{
pst = cn.prepareStatement(“select idusuario, nome from usuarios”);
rs = pst.executeQuery();
} catch (SQLException e) {
// TODO: handle exception
}
return rs;
}
public String excluir() throws InstantiationException, IllegalAccessException{
Connection cn = Conexao.obterConexao();
PreparedStatement pst = null;
try{
pst = cn.prepareStatement("delete from usuarios where login=?");
pst.setString(1, login);
pst.executeUpdate();
status = login + " Excluído com Sucesso ";
}
catch (SQLException e ){
status = " Falha na exclusão: " + e.getMessage();
}
return login;
}
}[/code]