Advertisement
Guest User

Untitled

a guest
May 15th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. package teste;
  2. import java.util.Scanner;
  3. import javax.swing.JOptionPane;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.PreparedStatement;
  8. import java.sql.SQLException;
  9.  
  10. public class BancoDeDados {
  11.  
  12. PreparedStatement pesquisa;
  13. String url = "jdbc:mysql://localhost/java";
  14. Connection conexao;
  15. int op = -1;
  16. Scanner tc = new Scanner(System.in);
  17. ResultSet resultado;
  18. String cpf="";
  19. String nome="";
  20. String sql="";
  21. String cond="";
  22. int telefone;
  23.  
  24. public static void main(String[] args) throws SQLException{
  25. new BancoDeDados();
  26. }
  27.  
  28. public BancoDeDados() throws SQLException{
  29. conexao = DriverManager.getConnection(url,"root","1234");
  30. do{
  31. System.out.println("------ Loja do seu zé ------");
  32. System.out.println("1 - Mostrar clientes ");
  33. System.out.println("2 - Cadastrar cliente");
  34. System.out.println("3 - Mostrar cliente específico");
  35. System.out.println("4 - Deletar cadastro de cliente");
  36. System.out.println("0 - Sair");
  37. op = Integer.parseInt(tc.nextLine());
  38. }while(op==-1);
  39.  
  40. if(op==1){
  41. printClients();
  42. }
  43. else if(op==2){
  44. regClient();
  45. }
  46. else if(op==3){
  47. printAClient();
  48. }
  49. else if(op==4){
  50. delClient();
  51. }
  52. else if(op==0){
  53. op=-1;
  54. }
  55. else{
  56. System.out.println("Digite uma opção válida");
  57. }
  58. }
  59.  
  60. public void printClients()throws SQLException{
  61. try{
  62. pesquisa = (PreparedStatement)conexao.prepareStatement("SELECT * FROM clientes");
  63.  
  64. resultado = pesquisa.executeQuery();
  65. while(resultado.next()){
  66. nome = resultado.getString("nome");
  67. cpf = resultado.getString("cpf");
  68. telefone = resultado.getInt("telefone");
  69. System.out.println("----------------------------");
  70. System.out.println("Nome:"+nome);
  71. System.out.println("CPF: "+cpf);
  72. System.out.println("Telefone: "+telefone);
  73. }
  74. }
  75. catch(Exception erro){
  76.  
  77. }
  78. new BancoDeDados();
  79. }
  80.  
  81. public void printAClient() throws SQLException{
  82. try{
  83. System.out.println("Digite o CPF do cliente:");
  84. cpf = tc.nextLine();
  85.  
  86. pesquisa = (PreparedStatement)conexao.prepareStatement("SELECT * FROM clientes WHERE cpf='"+cpf+"'");
  87.  
  88. resultado = pesquisa.executeQuery();
  89. while(resultado.next()){
  90. nome = resultado.getString("nome");
  91. resultado.getString("cpf");
  92. telefone = resultado.getInt("telefone");
  93. System.out.println("----------------------------");
  94. System.out.println("Nome: "+nome);
  95. System.out.println("CPF: "+cpf);
  96. System.out.println("Telefone: "+telefone);
  97. }
  98. }
  99. catch(Exception erro){
  100.  
  101. }
  102. new BancoDeDados();
  103. }
  104.  
  105. public void regClient() throws SQLException{
  106. nome = "";
  107. cpf = "";
  108. telefone = 0;
  109.  
  110. System.out.println("Digite o nome do cliente:");
  111. nome = tc.nextLine();
  112. System.out.println("Digite o cpf do cliente:");
  113. cpf = tc.nextLine();
  114. System.out.println("Digite o telefone do cliente:");
  115. telefone = Integer.parseInt(tc.nextLine());
  116.  
  117.  
  118. sql = "INSERT INTO clientes(nome,cpf,telefone) values (\""+nome+"\",\""+cpf+"\",\""+telefone+"\")";
  119. pesquisa = (PreparedStatement) conexao.prepareStatement(sql);
  120. pesquisa.executeUpdate();
  121.  
  122. JOptionPane.showMessageDialog(null, "Usuário cadastrado com sucesso!");
  123. new BancoDeDados();
  124. }
  125.  
  126. public void delClient(){
  127. try{
  128. cpf = "";
  129. System.out.println("Digite o cpf do cliente que deseja deletar:");
  130. cpf = tc.nextLine();
  131. sql = "SELECT * FROM clientes WHERE cpf='"+cpf+"'";
  132. pesquisa = (PreparedStatement) conexao.prepareStatement(sql);
  133. resultado = pesquisa.executeQuery();
  134. if(resultado != null && resultado.next()){
  135. nome = resultado.getString("nome");
  136. }
  137.  
  138.  
  139. System.out.println("Deseja mesmo deletar o cliente "+nome+" do CPF "+cpf+"?");
  140. cond = tc.nextLine();
  141.  
  142. if(cond.equalsIgnoreCase("sim")){
  143. sql = "DELETE FROM clientes WHERE cpf='"+cpf+"'";
  144. pesquisa = (PreparedStatement) conexao.prepareStatement(sql);
  145. pesquisa.executeUpdate();
  146. JOptionPane.showMessageDialog(null, "Usuário deletado com sucesso!");
  147. }
  148. else{
  149. new BancoDeDados();
  150. }
  151. new BancoDeDados();
  152. }catch(Exception erro){System.out.println("deu erro "+erro);}
  153. }
  154. }
  155.  
  156.  
  157.  
  158. -------
  159.  
  160. CREATE DATABASE `java`;
  161. USE java;
  162. CREATE TABLE `clientes` (
  163. `nome` varchar(50) NOT NULL,
  164. `cpf` varchar(13) NOT NULL PRIMARY KEY,
  165. `telefone` int(11) NOT NULL
  166. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement