Advertisement
Guest User

SQL

a guest
Feb 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. package br.com.vinideveloper;
  2.  
  3. import java.sql.*;
  4.  
  5. public class SQL {
  6. // CONSTANTES
  7. private static final String HOST = "localhost"; // NOME DE NOSSA HOST
  8. private static final String USER = "root"; // CONSTANTE DO NOME DE NOSSO USUARIO
  9. private static final String SENHA = ""; // CONSTANTE DO NOME DE NOSSA SENHA
  10.  
  11. // Funções De Conexão
  12. public static Connection getCon(String nameBD)
  13. {
  14. Connection con = null;
  15. try{
  16. Class.forName("com.mysql.jdbc.Driver");
  17. con = DriverManager.getConnection("jdbc:mysql://"+HOST+"/"+nameBD, USER, SENHA);
  18. System.out.println("Carregado com sucesso!");
  19. }catch (SQLException e){
  20. e.printStackTrace();
  21. }catch (ClassNotFoundException e1){
  22. e1.printStackTrace();
  23. }
  24. return con;
  25. }// Fim da função getCon(1param)
  26.  
  27. public static void close(Connection con)
  28. {
  29. try {
  30. con.close();
  31. System.out.println("Conexão encerrada");
  32. }catch ( SQLException e){
  33. e.printStackTrace();
  34. }
  35.  
  36. }// Fim da função close(1param)
  37.  
  38.  
  39. // Funções de execuções mysql
  40. public static void cadastrar(String bd, String tabela, User user)
  41. {
  42. try {
  43. Connection con = getCon(bd);
  44. DatabaseMetaData mtd = con.getMetaData();
  45. ResultSet rs = mtd.getTables(null, null, tabela, null);
  46. if (rs.next())
  47. {
  48. String sql = "INSERT INTO " + tabela + " (ID, NOME, APELIDO, USUARIO, SENHA, EMAIL) VALUES (null,?,?,?,?,?)";
  49. PreparedStatement ps = con.prepareStatement(sql);
  50. ps.setString(1, user.getNome());
  51. ps.setString(2, user.getApelido());
  52. ps.setString(3, user.getUsuario());
  53. ps.setString(4, user.getSenha());
  54. ps.setString(5, user.getEmail());
  55. ps.executeUpdate();
  56. ps.close();
  57. System.out.println("Tabela criada com sucesso!");
  58. con.close();
  59. }else{
  60. System.out.println("Tabela inexistente: " + tabela);
  61. }
  62.  
  63. }catch ( SQLException e ) {
  64. e.printStackTrace();
  65. }
  66. }//Fim da função cadastrar(param1, param2, param3)
  67.  
  68. public static boolean exist(String bd, String tabela, String idf1, String valor1, String idf2, String valor2)
  69. {
  70. try {
  71. Connection con = getCon(bd);
  72. DatabaseMetaData mtd = con.getMetaData();
  73. ResultSet rs = mtd.getTables(null, null, tabela, null);
  74. if(rs.next())
  75. {
  76. String sql = "SELECT * FROM "+ tabela + " WHERE " + idf1 + " = ? AND " + idf2 + " = ?";
  77. PreparedStatement ps = con.prepareStatement(sql);
  78. ps.setString(1, valor1);
  79. ps.setString(2, valor2);
  80. ResultSet rsF = ps.executeQuery();
  81. if(rsF.next())
  82. {
  83. System.out.println("Sucesso - Usuário encontrado");
  84. return true;
  85.  
  86. }else
  87. {
  88. System.out.println("Dados inexistentes!");
  89. return false;
  90. }
  91.  
  92. }else{
  93. System.out.println("Tabela inexistente: " + tabela);
  94. return false;
  95. }
  96.  
  97. }catch ( SQLException e ){
  98. e.printStackTrace();
  99. return false;
  100. }
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement