Clase Usuario
/*
- 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 br.com.lojavirtual.modelo;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
/**
*
-
@author rafael
*/
public class UsuarioDao {
private final String user = "root";
private final String password = "Tamires90()";
private final String url="jdbc:mysql://localhost:3306/LojaVirtual";
private Connection con;
private Statement st;
private PreparedStatement pst;
List<Usuario> lista = new ArrayList<>();
public int conectar(){
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url, user, password);
st = con.createStatement();
return 0;
} catch(ClassNotFoundException e){
System.out.println("Driver de conexão não encontrado " + e.getMessage());
return 1;
} catch(SQLException ex){
System.out.println("Base de dados não Encontrada " + ex.getMessage());
return 2;
}
}
public int desconectar(){
try{
con.close();
return 0;
} catch(SQLException ex){
System.out.println("Falha ao desconectar" + ex.getMessage());
return 1;
}
}
public int inserir(String nome, String email, String telefone, String login, String senha){
try{
st.executeUpdate("INSERT INTO usuario(nome, email, telefone, login, senha) VALUES ('"+nome+"','"+email+"','"+telefone+"','"+login+"','"+senha+"')");
return 0;
} catch(SQLException e){
System.out.println(e);
return 1;
}
}
public List pesquisar(){
try{
ResultSet rs = st.executeQuery("SELECT * FROM usuario ORDER BY nome");
while(rs.next()){
Usuario usuario = new Usuario();
usuario.setNome(rs.getString("nome"));
usuario.setEmail(rs.getString("email"));
usuario.setTelefone(rs.getString("telefone"));
lista.add(usuario);
}
return lista;
} catch(SQLException ex){
return lista;
}
}
}