Problem in a connection class

Asked

Viewed 35 times

0

well I’m with this error in my connection class:

Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql///helpsemeq

I don’t know which part I got wrong in the code:

package Conexao;

import java.sql.Connection;
import java.sql.DriverManager;

import java.sql.SQLException;


public class ConnectionFactory {
    public Connection getConnection(){
        try{
        String nomeUsuario = "root";
        String senhaUsuario = "";
        String enderecoServidor = "localhost";
        String nomeBanco = "helpsemeq";
        return DriverManager.getConnection("jdbc:mysql//"+enderecoServidor+
                "/"+nomeBanco, nomeUsuario, senhaUsuario);

        } catch (SQLException ex){
            System.out.println("fail");
            throw new RuntimeException(ex);
        }
    }
}
  • How is your classpath?

  • would be the mysql library?

  • Probably.

1 answer

0

Here is an example of my mysql connection class: remember to import the mysql library to your project.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author JCR
 *
 * Classe responsável pela conexão com o banco de dados MySQL
 *
 * Database: estoque User: root Password: ""
 *
 */
public class ConnectionFactoryMySQL {

    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost/estoque";
    private static final String USER = "root";
    private static final String PASS = "";

    public static Connection getConnection() {
        try {
            Class.forName(DRIVER);
            return DriverManager.getConnection(URL, USER, PASS);
        } catch (ClassNotFoundException | SQLException ex) {
            throw new RuntimeException("Erro na conexão MySQL", ex);
        }
    }

    public static void closeConnection(Connection con) {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException ex) {
                System.err.println("ERRO MySQL: " + ex);
            }
        }
    }

    public static void closeConnection(Connection con, PreparedStatement stmt) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException ex) {
                System.err.println("ERRO MySQL: " + ex);
            }
        }
        closeConnection(con);
    }

    public static void closeConnection(Connection con, PreparedStatement stmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException ex) {
                System.err.println("ERRO MySQL: " + ex);
            }
        }
        closeConnection(con, stmt);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.