Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package teste;
- import java.util.Scanner;
- import javax.swing.JOptionPane;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- public class BancoDeDados {
- PreparedStatement pesquisa;
- String url = "jdbc:mysql://localhost/java";
- Connection conexao;
- int op = -1;
- Scanner tc = new Scanner(System.in);
- ResultSet resultado;
- String cpf="";
- String nome="";
- String sql="";
- String cond="";
- int telefone;
- public static void main(String[] args) throws SQLException{
- new BancoDeDados();
- }
- public BancoDeDados() throws SQLException{
- conexao = DriverManager.getConnection(url,"root","1234");
- do{
- System.out.println("------ Loja do seu zé ------");
- System.out.println("1 - Mostrar clientes ");
- System.out.println("2 - Cadastrar cliente");
- System.out.println("3 - Mostrar cliente específico");
- System.out.println("4 - Deletar cadastro de cliente");
- System.out.println("0 - Sair");
- op = Integer.parseInt(tc.nextLine());
- }while(op==-1);
- if(op==1){
- printClients();
- }
- else if(op==2){
- regClient();
- }
- else if(op==3){
- printAClient();
- }
- else if(op==4){
- delClient();
- }
- else if(op==0){
- op=-1;
- }
- else{
- System.out.println("Digite uma opção válida");
- }
- }
- public void printClients()throws SQLException{
- try{
- pesquisa = (PreparedStatement)conexao.prepareStatement("SELECT * FROM clientes");
- resultado = pesquisa.executeQuery();
- while(resultado.next()){
- nome = resultado.getString("nome");
- cpf = resultado.getString("cpf");
- telefone = resultado.getInt("telefone");
- System.out.println("----------------------------");
- System.out.println("Nome:"+nome);
- System.out.println("CPF: "+cpf);
- System.out.println("Telefone: "+telefone);
- }
- }
- catch(Exception erro){
- }
- new BancoDeDados();
- }
- public void printAClient() throws SQLException{
- try{
- System.out.println("Digite o CPF do cliente:");
- cpf = tc.nextLine();
- pesquisa = (PreparedStatement)conexao.prepareStatement("SELECT * FROM clientes WHERE cpf='"+cpf+"'");
- resultado = pesquisa.executeQuery();
- while(resultado.next()){
- nome = resultado.getString("nome");
- resultado.getString("cpf");
- telefone = resultado.getInt("telefone");
- System.out.println("----------------------------");
- System.out.println("Nome: "+nome);
- System.out.println("CPF: "+cpf);
- System.out.println("Telefone: "+telefone);
- }
- }
- catch(Exception erro){
- }
- new BancoDeDados();
- }
- public void regClient() throws SQLException{
- nome = "";
- cpf = "";
- telefone = 0;
- System.out.println("Digite o nome do cliente:");
- nome = tc.nextLine();
- System.out.println("Digite o cpf do cliente:");
- cpf = tc.nextLine();
- System.out.println("Digite o telefone do cliente:");
- telefone = Integer.parseInt(tc.nextLine());
- sql = "INSERT INTO clientes(nome,cpf,telefone) values (\""+nome+"\",\""+cpf+"\",\""+telefone+"\")";
- pesquisa = (PreparedStatement) conexao.prepareStatement(sql);
- pesquisa.executeUpdate();
- JOptionPane.showMessageDialog(null, "Usuário cadastrado com sucesso!");
- new BancoDeDados();
- }
- public void delClient(){
- try{
- cpf = "";
- System.out.println("Digite o cpf do cliente que deseja deletar:");
- cpf = tc.nextLine();
- sql = "SELECT * FROM clientes WHERE cpf='"+cpf+"'";
- pesquisa = (PreparedStatement) conexao.prepareStatement(sql);
- resultado = pesquisa.executeQuery();
- if(resultado != null && resultado.next()){
- nome = resultado.getString("nome");
- }
- System.out.println("Deseja mesmo deletar o cliente "+nome+" do CPF "+cpf+"?");
- cond = tc.nextLine();
- if(cond.equalsIgnoreCase("sim")){
- sql = "DELETE FROM clientes WHERE cpf='"+cpf+"'";
- pesquisa = (PreparedStatement) conexao.prepareStatement(sql);
- pesquisa.executeUpdate();
- JOptionPane.showMessageDialog(null, "Usuário deletado com sucesso!");
- }
- else{
- new BancoDeDados();
- }
- new BancoDeDados();
- }catch(Exception erro){System.out.println("deu erro "+erro);}
- }
- }
- -------
- CREATE DATABASE `java`;
- USE java;
- CREATE TABLE `clientes` (
- `nome` varchar(50) NOT NULL,
- `cpf` varchar(13) NOT NULL PRIMARY KEY,
- `telefone` int(11) NOT NULL
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement