Advertisement
Alan_Cesar

//Prototipo para acessar banco de dados pelo Java(Eclipse+My

Mar 17th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1.  
  2. //Prototipo para acessar banco de dados pelo Java(Eclipse+Mysql)
  3.  
  4. //Aluno : Alan Cesar.
  5. //Projeto: Unicarioca.
  6. //Softwares: Eclipse + MySQL.
  7. //RJ 15 de Março 2017.
  8.  
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.ResultSet;
  12. import java.sql.Statement;
  13.  
  14. import javax.swing.JOptionPane;
  15.  
  16. public class Projeto01 {
  17.  
  18. private Connection conexao=null;
  19. private Statement estrutura=null;
  20. private ResultSet resposta=null;
  21.  
  22. public void coneCtar(){
  23.  
  24. String servidor="jdbc:mysql://localhost:3306/agenda";
  25. String usuario="root";
  26. String senha=null;
  27. String drive="com.mysql.jdbc.Driver";
  28.  
  29. try{
  30.  
  31. Class.forName(drive);
  32. this.conexao=DriverManager.getConnection(servidor,usuario,senha);
  33. this.estrutura=this.conexao.createStatement();
  34.  
  35. }catch(Exception e){
  36.  
  37. System.out.print("Erro : "+e.getMessage());}
  38. }
  39.  
  40. public boolean coneCtado(){
  41. if(this.conexao != null){
  42.  
  43. return true;
  44. }else{
  45.  
  46. return false;
  47. }
  48. }
  49. public void inserirDados(String Id,String Nome ,String tel){
  50. try{
  51. String consulta="Insert into contato values('"+lerId(Id)+"','"+lerNome(Nome)+"','"+lerTel(tel)+"') ";
  52. this.estrutura.executeUpdate(consulta);
  53. }catch(Exception e){
  54.  
  55. System.out.print("Erro : "+e.getMessage());}
  56. }
  57. public void listarDados(){
  58. try{
  59.  
  60. String consulta = "Select * from contato";
  61. this.resposta=this.estrutura.executeQuery(consulta);
  62. while(this.resposta.next()){
  63.  
  64. System.out.println("Id "+this.resposta.getString("Id")+"Nome "+this.resposta.getString("Nome")+"Tel "+this.resposta.getString("tel"));
  65.  
  66. }
  67.  
  68. }catch(Exception e){
  69.  
  70. System.out.print("Erro : "+e.getMessage());
  71. }
  72.  
  73.  
  74. }
  75. public void fecharBanco(){
  76. try{
  77.  
  78. this.conexao.close();
  79. System.out.print("<<<Acesso encerrado com sucesso>>>");
  80.  
  81. }catch(Exception e){
  82. System.out.print("Erro : "+e.getMessage());
  83.  
  84. }
  85. }
  86. public int lerId(String id){
  87.  
  88. String aS=JOptionPane.showInputDialog("Id : ");
  89. int a=Integer.parseInt(aS);
  90. return a;
  91. }
  92.  
  93. public String lerNome(String nome){
  94. String b=JOptionPane.showInputDialog("Nome : ");
  95. return b;
  96. }
  97. public int lerTel(String tel){
  98. String cS=JOptionPane.showInputDialog("tel : ");
  99. int c=Integer.parseInt(cS);
  100. return c;
  101. }
  102. }
  103.  
  104.  
  105. }catch(Exception e){
  106.  
  107. System.out.println("Erro : "+e.getMessage());
  108.  
  109. }
  110.  
  111.  
  112.  
  113.  
  114. }
  115.  
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement