Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package controle;
- import entidades.Usuario;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Lab03c
- */
- public class Cadastro {
- private Conexao con = new Conexao();
- public void salvarUsuario(Usuario user) {
- PreparedStatement st;
- ResultSet res;
- try {
- int i = 1;
- st = con.getConexao().prepareStatement("insert into usuario(nome, login, senha) values(?,?,?)", PreparedStatement.RETURN_GENERATED_KEYS);
- st.setString(i++, user.getNome());
- st.setString(i++, user.getLogin());
- st.setString(i++, user.getSenha());
- st.execute();
- res = st.getGeneratedKeys();
- if (res.next()) {
- user.setCod(res.getInt(1));
- }
- } catch (SQLException ex) {
- Logger.getLogger(Cadastro.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public Usuario consultaUsuario(String login){
- Usuario user=null;
- PreparedStatement st;
- ResultSet res;
- int i=1;
- try {
- st=con.getConexao().prepareStatement("select cod, nome, login, senha from usuario where login=?");
- st.setString(i++, login);
- res=st.executeQuery();
- if(res.next()){
- user=new Usuario();
- user.setCod(res.getInt("cod"));
- user.setNome(res.getString("nome"));
- user.setLogin(res.getString("login"));
- user.setSenha(res.getString("senha"));
- }
- } catch (SQLException ex) {
- Logger.getLogger(Cadastro.class.getName()).log(Level.SEVERE, null, ex);
- }
- return user;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement