Advertisement
Guest User

Untitled

a guest
Mar 11th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. package jdbc;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.Scanner;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11.  
  12. public class Jdbc {
  13.  
  14. public static void main(String[] args) {
  15.  
  16. Scanner sc = new Scanner(System.in);
  17.  
  18. try {
  19. Class.forName("com.mysql.jdbc.Driver");
  20.  
  21. String url = "jdbc:mysql://sql10.freesqldatabase.com/Alunos";
  22. String user = "sql10163288";
  23. String psswrd = "DAvh4R4DkS";
  24.  
  25. Connection con = DriverManager.getConnection(url, user, psswrd);
  26.  
  27. String sql = "select nome from Alunos where matricula = ?";
  28. String sql2 = "update Alunos set nome = ?, idade = ?, turma = ? where matricula = ?";
  29. String sql3 = "insert into Alunos values ('?', ?, '?', '?')";
  30. String sql4 = "delete from Alunos where matricula = ?";
  31. PreparedStatement cons = con.prepareStatement(sql);
  32. PreparedStatement cons2 = con.prepareStatement(sql2);
  33. PreparedStatement cons3 = con.prepareStatement(sql3);
  34. PreparedStatement cons4 = con.prepareStatement(sql4);
  35.  
  36. int opcao = 0;
  37.  
  38. while (opcao != 0) {
  39. System.out.print("");
  40. System.out.print("1 - Buscas no Banco de Dados");
  41. System.out.print("2 - Alterar dados do Banco");
  42. System.out.print("3 - Cadastrar mais pessoas do Banco");
  43. System.out.print("4 - Deletar dados do Banco ");
  44. opcao = sc.nextInt();
  45.  
  46. if (opcao == 1) {
  47. System.out.print("\n Busca no Banco de Dados");
  48. System.out.print("Digite a matrícula a ser pesquisada: ");
  49. String matricula = sc.next();
  50.  
  51. cons.setString(1, matricula);
  52. ResultSet rs = cons.executeQuery();
  53.  
  54. while (rs.next()) {
  55. System.out.print("Nome: " + rs.getString("nome"));
  56. }
  57. } else {
  58. if (opcao == 2) {
  59. System.out.print("\n Alterar dados do Banco");
  60. System.out.print("Digite a matrícula a ser alterada: ");
  61. String matricula = sc.next();
  62.  
  63. System.out.print("\n Digite o nome: ");
  64. String nome = sc.next();
  65. System.out.print("Digite a idade: ");
  66. int idade = sc.nextInt();
  67. System.out.print("Digite a turma: ");
  68. String turma = sc.next();
  69.  
  70. cons2.setString(1, nome);
  71. cons2.setInt(2, idade);
  72. cons2.setString(3, turma);
  73. cons2.setString(4, matricula);
  74. ResultSet rs = cons2.executeQuery();
  75.  
  76. } else {
  77. if(opcao == 3){
  78. System.out.print("\n Cadastro de Alunos");
  79. System.out.print("Digite o nome");
  80. String nome = sc.next();
  81. System.out.print("Digite a idade");
  82. int idade = sc.nextInt();
  83. System.out.println("Digite a matrícula");
  84. String matricula = sc.next();
  85. System.out.print("Digite a turma");
  86. String turma = sc.next();
  87.  
  88. cons3.setString(1, nome);
  89. cons3.setInt(2, idade);
  90. cons3.setString(3, matricula);
  91. cons3.setString(4, turma);
  92.  
  93. ResultSet rs = cons3.executeQuery();
  94.  
  95. } else {
  96. if(opcao == 4){
  97. System.out.print("\n CUIDADO AO DELETAR DADOS DO BANCO");
  98. System.out.print("Digite a matrícula");
  99. String matricula = sc.next();
  100. System.out.print("Digite novamente a matrícula");
  101. String matricula2 = sc.next();
  102.  
  103. if (matricula == matricula2){
  104. cons4.setString(1, matricula);
  105. ResultSet rs = cons4.executeQuery();
  106. }
  107. }
  108. }
  109. }
  110. }
  111.  
  112. }
  113. } catch (ClassNotFoundException ex) {
  114. Logger.getLogger(Jdbc.class.getName()).log(Level.SEVERE, null, ex);
  115. } catch (SQLException ex) {
  116. Logger.getLogger(Jdbc.class.getName()).log(Level.SEVERE, null, ex);
  117. }
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement