Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package database;
- import java.sql.Connection; // Conexão
- import java.sql.DriverManager; // Me conecte com driver
- import java.sql.ResultSet; // Repositório de registros vindo de uma consulta
- import java.sql.SQLException; // Tratamento de Erros
- import java.sql.Statement; // Repositório para envio de registros
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author aluno
- */
- public class ConnMySQL {
- /**
- * Inicializa os atributos da classe
- */
- public static Connection conn = null;
- public static String connUrl = null;
- public static String userName = null;
- public static String password = null;
- public static String dbName = null;
- public static Boolean connSucess = false;
- public static int recordCount = -1;
- public ResultSet rs = null;
- public Statement stmt = null;
- /**
- * Construtor da classe
- */
- public ConnMySQL() {
- connUrl = "jdbc:mysql://localhost:3306/";
- dbName = "sisflora";
- userName = "root";
- password = "";
- }
- /**
- * Trata a abertura da conexão do banco de dados
- */
- public void abreConexao() {
- connSucess = false;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException ex) {
- System.out.println("Classe JDBC não encontrada.");
- ex.printStackTrace();
- }
- try {
- conn = DriverManager.getConnection(
- connUrl + dbName, userName, password);
- connSucess = true;
- } catch (SQLException ex) {
- System.out.println("Erro de conexão com o DB");
- ex.printStackTrace();
- }
- }
- /**
- * Fecha a conexão com o banco de dados
- */
- public void fechaConexao() {
- try {
- conn.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- connSucess = false;
- }
- /**
- * Executa uma sentença SQL do tipo: insert, update
- * ou delete
- */
- public boolean execSQL(String sql) {
- if (sql.isEmpty())
- return false;
- try {
- conn.setAutoCommit(false);
- stmt = conn.prepareStatement(sql);
- stmt = conn.createStatement();
- stmt.executeUpdate(sql);
- conn.commit();
- conn.setAutoCommit(true);
- return true;
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- return false;
- }
- /**
- * Faz uma pesquisa do tipo SELECT e retorna o resultset
- */
- public void selectSQL(String sql) {
- rs = null;
- try {
- stmt = conn.createStatement();
- rs = stmt.executeQuery(sql);
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement