Advertisement
Guest User

Classe ConnMySQL

a guest
Apr 15th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 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. package database;
  7.  
  8. import java.sql.Connection;     // Conexão
  9. import java.sql.DriverManager;  // Me conecte com driver
  10. import java.sql.ResultSet;      // Repositório de registros vindo de uma consulta
  11. import java.sql.SQLException;   // Tratamento de Erros
  12. import java.sql.Statement;      // Repositório para envio de registros
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. /**
  17.  *
  18.  * @author aluno
  19.  */
  20. public class ConnMySQL {
  21.  
  22.     /**
  23.      * Inicializa os atributos da classe
  24.      */
  25.    
  26.     public static Connection conn = null;
  27.     public static String connUrl = null;
  28.     public static String userName = null;
  29.     public static String password = null;
  30.     public static String dbName = null;
  31.     public static Boolean connSucess = false;
  32.     public static int recordCount = -1;
  33.     public ResultSet rs = null;
  34.     public Statement stmt = null;
  35.    
  36.     /**
  37.      * Construtor da classe
  38.      */
  39.    
  40.     public ConnMySQL() {
  41.         connUrl = "jdbc:mysql://localhost:3306/";
  42.         dbName = "sisflora";
  43.         userName = "root";
  44.         password = "";
  45.     }
  46.  
  47.     /**
  48.      * Trata a abertura da conexão do banco de dados
  49.      */
  50.    
  51.     public void abreConexao() {
  52.         connSucess = false;
  53.         try {
  54.             Class.forName("com.mysql.jdbc.Driver");
  55.         } catch (ClassNotFoundException ex) {
  56.             System.out.println("Classe JDBC não encontrada.");
  57.             ex.printStackTrace();
  58.         }
  59.        
  60.         try {
  61.             conn = DriverManager.getConnection(
  62.                 connUrl + dbName, userName, password);
  63.             connSucess = true;
  64.         } catch (SQLException ex) {
  65.             System.out.println("Erro de conexão com o DB");
  66.             ex.printStackTrace();
  67.         }
  68.     }
  69.    
  70.     /**
  71.      * Fecha a conexão com o banco de dados
  72.      */
  73.    
  74.     public void fechaConexao() {
  75.         try {
  76.             conn.close();
  77.         } catch (SQLException ex) {
  78.             ex.printStackTrace();
  79.         }
  80.         connSucess = false;
  81.     }
  82.    
  83.     /**
  84.      * Executa uma sentença SQL do tipo: insert, update
  85.      * ou delete
  86.      */
  87.    
  88.     public boolean execSQL(String sql) {
  89.         if (sql.isEmpty())
  90.             return false;
  91.         try {
  92.             conn.setAutoCommit(false);
  93.             stmt = conn.prepareStatement(sql);
  94.             stmt = conn.createStatement();
  95.             stmt.executeUpdate(sql);
  96.             conn.commit();
  97.             conn.setAutoCommit(true);
  98.            
  99.             return true;
  100.         } catch (SQLException ex) {
  101.             ex.printStackTrace();
  102.         }
  103.         return false;
  104.     }
  105.    
  106.     /**
  107.      * Faz uma pesquisa do tipo SELECT e retorna o resultset
  108.      */
  109.     public void selectSQL(String sql) {
  110.         rs = null;
  111.         try {
  112.             stmt = conn.createStatement();
  113.             rs = stmt.executeQuery(sql);
  114.         } catch (SQLException ex) {
  115.             ex.printStackTrace();
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement