Advertisement
Guest User

Untitled

a guest
Dec 15th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. package BD;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15. /**
  16. *
  17. * @author a35943
  18. */
  19. public class ligacaoBD {
  20. public final static int NOT_OK=0;
  21. public final static int OK=1;
  22. public final static int CHAVE_DUPLICADA=2;
  23. private String bd;
  24. private Connection cn;
  25. private Statement st;
  26. private int estado;
  27.  
  28.  
  29. public ligacaoBD(String nomeBD){
  30. this.bd=nomeBD;
  31. }
  32.  
  33. public void executeSQL(String sql){
  34. try {
  35. this.st.executeUpdate(sql);
  36. this.estado=OK;
  37. } catch (SQLException ex) {
  38. Logger.getLogger(ligacaoBD.class.getName()).log(Level.SEVERE, null, ex);
  39. //Verifica se chave está duplicada 1586= chave duplicada
  40. if(ex.getErrorCode()==1586)
  41. this.estado=CHAVE_DUPLICADA;
  42. else
  43. this.estado=NOT_OK;
  44. }
  45. }
  46.  
  47. public ResultSet executaConsulta(String sql){
  48.  
  49. ResultSet rs=null;
  50. try {
  51. rs=this.st.executeQuery(sql);
  52. this.estado=OK;
  53. } catch (SQLException ex) {
  54. Logger.getLogger(ligacaoBD.class.getName()).log(Level.SEVERE, null, ex);
  55. this.estado=NOT_OK;
  56. }
  57. return rs;
  58. }
  59.  
  60.  
  61.  
  62. public String getTabelaClientes(){
  63. String tabela= "<table class=\"table table-striped\">\n" +
  64. " <thead>\n" +
  65. " <tr>\n" +
  66. " <th>ID</th>\n" +
  67. " <th>Nome</th>\n" +
  68. " <th>Renda</th>\n" +
  69. " </tr>\n" +
  70. " </thead>\n" +
  71. " <tbody>\n";
  72. ResultSet rs = this.executaConsulta("Select * from clientes");
  73. try {
  74. while(rs.next()){
  75. tabela+="<tr>\n" +
  76. " <td>"+rs.getInt("id")+"</td>\n" +
  77. " <td>"+rs.getString("nome")+"</td>\n" +
  78. " <td>"+rs.getDouble("renda")+"</td>\n" +
  79. " </tr>\n";
  80. }
  81. } catch (SQLException ex) {
  82. Logger.getLogger(ligacaoBD.class.getName()).log(Level.SEVERE, null, ex);
  83. }
  84. tabela+="</tbody>\n" +
  85. "</table>";
  86.  
  87. return tabela;
  88. }
  89.  
  90. public void desligarBaseDados(){
  91. if(this.cn!=null){
  92. try {
  93. this.cn.close();
  94. this.estado=OK;
  95. } catch (SQLException ex) {
  96. Logger.getLogger(ligacaoBD.class.getName()).log(Level.SEVERE, null, ex);
  97. this.estado=NOT_OK;
  98. }
  99. }
  100. }
  101.  
  102.  
  103. public void ligarBaseDados(){
  104. try {
  105. Class.forName("com.mysql.jdbc.Driver");
  106. this.cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/"+this.bd,"root","root");
  107. this.st=cn.createStatement();
  108. this.estado=OK;
  109. } catch (ClassNotFoundException | SQLException ex) {
  110. Logger.getLogger(ligacaoBD.class.getName()).log(Level.SEVERE, null, ex);
  111. this.estado=NOT_OK;
  112. }
  113. }
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. public int getEstado() {
  127. return estado;
  128. }
  129.  
  130. public void setEstado(int estado) {
  131. this.estado = estado;
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement