Advertisement
Guest User

Untitled

a guest
Sep 13th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1.  
  2. import DTO.Livro;
  3.  
  4. import java.util.LinkedList;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7. import java.sql.*;
  8.  
  9. public class coneccaoBD {
  10.  
  11. private Connection conn;
  12. private Statement query;
  13. private ResultSet r;
  14.  
  15. public coneccaoBD() {
  16.  
  17. Driver driver = null;
  18.  
  19. try{
  20. Class c = Class.forName("com.mysql.jdbc.Driver");
  21. driver = (Driver) c.newInstance();
  22. DriverManager.registerDriver((Driver)driver);
  23.  
  24. iniciarConexao();
  25. }
  26. catch(Exception e){
  27. System.out.println("errro coneccao");
  28.  
  29. }
  30. }
  31.  
  32. // public void iniciarDriver() {
  33. // try {
  34. // Connection con =
  35. // } catch (Exception ex) {
  36. // System.out.println("Erro ao iniciar o Driver.");
  37. // }
  38. // }
  39.  
  40. public void iniciarConexao() throws SQLException {
  41. String url = "jdbc:mysql://localhost/pr?" + "user=rootx&password=pass";
  42. conn = (Connection) DriverManager.getConnection(url);
  43. System.out.println(url);
  44. }
  45.  
  46. public void iniciarStatement() throws SQLException {
  47. query = (Statement) conn.createStatement();
  48. }
  49.  
  50. public void executaUpdate(String sql) throws SQLException {
  51. query.executeUpdate(sql);
  52. }
  53.  
  54. public ResultSet executaQuery(String sql) throws SQLException {
  55. return query.executeQuery(sql);
  56. }
  57.  
  58. public void fechaStatement() throws SQLException {
  59. query.close();
  60. }
  61.  
  62. public void close() throws SQLException {
  63. conn.close();
  64. }
  65.  
  66. public boolean verificaUser(String name, String pass) {
  67. try {
  68. this.iniciarStatement();
  69. r = query.executeQuery("SELECT * FROM admins");
  70.  
  71. while (r.next()) {
  72. String Adminusername = r.getString("username");
  73. String Adminpassword = r.getString("password");
  74. if (Adminusername.equals(name) && Adminpassword.equals(pass)) {
  75. this.fechaStatement();
  76. return true;
  77. }
  78. }
  79. this.fechaStatement();
  80. return false;
  81.  
  82. } catch (SQLException ex) {
  83. // Logger.getLogger(Controlador.class.getName()).log(Level.SEVERE, null, ex);
  84. }
  85.  
  86. return false;
  87. }
  88.  
  89. public LinkedList<Livro> getLivros() {
  90. LinkedList<Livro> livros = new LinkedList<Livro>();
  91. ResultSet result;
  92. Livro livro;
  93.  
  94. try {
  95. this.iniciarStatement();
  96. String queryString = "select * from livro";
  97.  
  98. result = query.executeQuery(queryString);
  99.  
  100. while (result.next()) {
  101.  
  102. livro = new Livro(result.getString("nome"), result.getString("autor"), result.getString("descricao"));
  103. livros.add(livro);
  104. }
  105.  
  106. this.fechaStatement();
  107. } catch (SQLException ex) {
  108. Logger.getLogger(coneccaoBD.class.getName()).log(Level.SEVERE, null, ex);
  109. }
  110. return livros;
  111. }
  112.  
  113. public void insereUser(String username, String name, String pass, String email) {
  114. try {
  115. this.iniciarStatement();
  116. String stringQuery = "INSERT INTO user (username, name, password, email, registerDate) VALUES (" + "'" + username + "'" + ", " + "'" + name + "'" + ", " + "'" + pass + "'" + ", " + "'" + email + "'" + "," + "curdate())";
  117. System.out.println(stringQuery);
  118. query.executeUpdate(stringQuery);
  119. this.fechaStatement();
  120.  
  121. } catch (SQLException ex) {
  122. // Logger.getLogger(Controlador.class.getName()).log(Level.SEVERE, null, ex);
  123. }
  124.  
  125. }
  126.  
  127. public void alterarPassword(String username, String pass){
  128.  
  129. //update ..
  130.  
  131. }
  132.  
  133.  
  134. //Insere os mails na newsletter..
  135. public void insereEmail(String email) {
  136. try {
  137. this.iniciarStatement();
  138. String stringQuery = "INSERT INTO email VALUES (' "+ email + "')";
  139.  
  140. query.executeUpdate(stringQuery);
  141. this.fechaStatement();
  142.  
  143. } catch (SQLException ex) {
  144. // Logger.getLogger(Controlador.class.getName()).log(Level.SEVERE, null, ex);
  145. }
  146.  
  147. }
  148.  
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement