Advertisement
Guilherme-Valle

Banco de Dados MySql + Java

Nov 29th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. Classe aluno:
  2.  
  3. /*
  4. * To change this license header, choose License Headers in Project Properties.
  5. * To change this template file, choose Tools | Templates
  6. * and open the template in the editor.
  7. */
  8. package projetobd;
  9.  
  10. /**
  11. *
  12. * @author aluno
  13. */
  14. public class Aluno {
  15. private String nome;
  16. private int id;
  17. private String cpf;
  18. private int idade;
  19.  
  20. public Aluno (String nome, String cpf, int idade){
  21. this.nome = nome;
  22. this.cpf = cpf;
  23. this.idade = idade;
  24. }
  25.  
  26. public void setNome (String nome){
  27. this.nome = nome;
  28. }
  29.  
  30. public String getNome (){
  31. return this.nome;
  32. }
  33.  
  34. public void setId (int id){
  35. this.id = id;
  36. }
  37.  
  38. public int getId (){
  39. return this.id;
  40. }
  41.  
  42. public void setCPF (String cpf){
  43. this.cpf = cpf;
  44. }
  45.  
  46. public String getCPF (){
  47. return this.cpf;
  48. }
  49.  
  50. public void setIdade (int idade){
  51. this.idade = idade;
  52. }
  53.  
  54. public int getIdade (){
  55. return this.idade;
  56. }
  57. }
  58.  
  59.  
  60. --
  61.  
  62. Classe banco:
  63.  
  64. package projetobd;
  65.  
  66.  
  67. import java.sql.Connection;
  68. import java.sql.DriverManager;
  69. import java.sql.ResultSet;
  70. import java.sql.Statement;
  71. import java.util.ArrayList;
  72.  
  73. /*
  74. * To change this license header, choose License Headers in Project Properties.
  75. * To change this template file, choose Tools | Templates
  76. * and open the template in the editor.
  77. */
  78.  
  79. /**
  80. *
  81. * @author aluno
  82. */
  83. public class Banco {
  84. public Connection conexao=null;
  85. public Statement declaracao=null;
  86. public ResultSet resultado=null;
  87.  
  88.  
  89. public void conectar(){
  90. String usuario = "root";
  91. String senha = "";
  92. String driver = "com.mysql.jdbc.Driver";
  93. String servidor = "jdbc:mysql://localhost:3306/bancopi";
  94.  
  95. try{
  96. this.conexao = DriverManager.getConnection(servidor, usuario, senha);
  97. this.declaracao = this.conexao.createStatement();
  98. }catch(Exception e){
  99. System.out.println("Erro: " +e.getMessage());
  100. }
  101. }
  102.  
  103. public boolean estaConectado(){
  104. if (conexao!= null){
  105. return true;
  106. } else
  107. return false;
  108.  
  109. }
  110.  
  111. public void insereAluno (Aluno aluno){
  112. try {
  113. String sql = "INSERT INTO ALUNO (NOME, CPF, IDADE) VALUES ('"+aluno.getNome()+"','"+aluno.getCPF()+"',"+aluno.getIdade()+")";
  114. this.declaracao.executeUpdate(sql);
  115. }catch(Exception e){
  116. System.out.println("Erro: "+e.getMessage());
  117. }
  118. }
  119.  
  120. public ArrayList<Aluno> listarAlunos(){
  121. ArrayList<Aluno> alunos = new ArrayList<>();
  122. try {
  123. String sql = "SELECT * FROM ALUNO";
  124. Aluno a1;
  125. this.resultado = this.declaracao.executeQuery(sql);
  126. while (this.resultado.next()){
  127. String nome, cpf;
  128. int idade, id;
  129. }
  130. }catch(Exception e){
  131. System.out.println("Erro: "+e.getMessage());
  132. }
  133. return null;
  134. }
  135.  
  136. }
  137.  
  138. -- Classe main
  139.  
  140. /*
  141. * To change this license header, choose License Headers in Project Properties.
  142. * To change this template file, choose Tools | Templates
  143. * and open the template in the editor.
  144. */
  145. package projetobd;
  146.  
  147. /**
  148. *
  149. * @author aluno
  150. */
  151. public class ProjetoBD {
  152.  
  153. /**
  154. * @param args the command line arguments
  155. */
  156. public static void main(String[] args) {
  157. Banco bd = new Banco();
  158. bd.conectar();
  159. boolean result = bd.estaConectado();
  160.  
  161. if (result){
  162. System.out.println("Está conectado! xP\n");
  163.  
  164. } else
  165. System.out.println("Não conectou ;_;");
  166.  
  167.  
  168.  
  169. Aluno a1 = new Aluno("Leticia", "886767", 10);
  170.  
  171. String sql = "INSERT INTO ALUNO (NOME, CPF, IDADE) VALUES ('"+a1.getNome()+"','"+a1.getCPF()+"',"+a1.getIdade()+")";
  172. System.out.println(sql);
  173.  
  174. bd.insereAluno(a1);
  175.  
  176.  
  177.  
  178. }
  179.  
  180.  
  181.  
  182.  
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement