Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package controle;
  7.  
  8. import entidades.Usuario;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14.  
  15. /**
  16. *
  17. * @author Lab03c
  18. */
  19. public class Cadastro {
  20.  
  21. private Conexao con = new Conexao();
  22.  
  23. public void salvarUsuario(Usuario user) {
  24. PreparedStatement st;
  25. ResultSet res;
  26.  
  27. try {
  28. int i = 1;
  29. st = con.getConexao().prepareStatement("insert into usuario(nome, login, senha) values(?,?,?)", PreparedStatement.RETURN_GENERATED_KEYS);
  30. st.setString(i++, user.getNome());
  31. st.setString(i++, user.getLogin());
  32. st.setString(i++, user.getSenha());
  33.  
  34. st.execute();
  35. res = st.getGeneratedKeys();
  36.  
  37. if (res.next()) {
  38. user.setCod(res.getInt(1));
  39. }
  40.  
  41. } catch (SQLException ex) {
  42. Logger.getLogger(Cadastro.class.getName()).log(Level.SEVERE, null, ex);
  43. }
  44.  
  45. }
  46. public Usuario consultaUsuario(String login){
  47. Usuario user=null;
  48. PreparedStatement st;
  49. ResultSet res;
  50.  
  51. int i=1;
  52. try {
  53. st=con.getConexao().prepareStatement("select cod, nome, login, senha from usuario where login=?");
  54. st.setString(i++, login);
  55.  
  56. res=st.executeQuery();
  57.  
  58. if(res.next()){
  59. user=new Usuario();
  60. user.setCod(res.getInt("cod"));
  61. user.setNome(res.getString("nome"));
  62. user.setLogin(res.getString("login"));
  63. user.setSenha(res.getString("senha"));
  64.  
  65. }
  66. } catch (SQLException ex) {
  67. Logger.getLogger(Cadastro.class.getName()).log(Level.SEVERE, null, ex);
  68. }
  69. return user;
  70. }
  71.  
  72.  
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement