Problems when connecting Java with mariadb

41 Views Asked by At

I have this school project about a car store database.

As you see,I have the Marca(It means brand in portuguese) class, the MarcaDAO(BrandDAO), the Conexao(Connection) and the Principal(Main).

Marca.java:

    package lojadecarros.Tables;

    import java.util.List;

    public class Marca {
         private int id;
         private String nome;
         private List<Modelo> modelos;

    public Marca(int id, String nome) {
        this.id = id;
        this.nome = nome;
    }

    public Marca(String nome) {
        this.nome = nome;
    }

    public void addModelo(Modelo modelo){
        modelos.add(modelo);
    }

    public int getId() {
        return id;
    }

    public void setId(int id){
        this.id = id;
    }
    
    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    @Override
    public String toString() {
        return "Marca [id=" + id + ", nome=" + nome + ", modelos=" + modelos + "]";
    }
    
    }

MarcaDAO.java:

    package lojadecarros.TabelasDao.Marca;

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

    import lojadecarros.TabelasDao.Conexao;
    import lojadecarros.Tables.Marca;

    // DAO = Data Access Object
    public class MarcaDAO {
    public Marca create(Marca marca) throws SQLException{
        String sql = """
        INSERT INTO marca (nome)
        VALUES (?);
        """;
        try (
            Connection connection = Conexao.getConnection();
            PreparedStatement statement = connection
            .prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
        ) {
            statement.setString(1, marca.getNome());
            statement.executeUpdate();

            ResultSet rs = statement.getGeneratedKeys();

            if(rs.next()) {
                marca.setId(rs.getInt(1));
            }

            rs.close();

            return marca;
        }
    }

    public Marca update(Marca marca) throws SQLException{
        String sql = """
        UPDATE marca
        SET nome = ?
        WHERE id = ?;
        """;

        try (
            Connection connection = Conexao.getConnection();
            PreparedStatement statement = connection.prepareStatement(sql);
        ) {

            statement.setString(1, marca.getNome());
            statement.setInt(2, marca.getId());
            int linhasAfetadas = statement.executeUpdate();

            if (linhasAfetadas > 0) {
                return marca;
            }
            return null;

            } catch (SQLException e) {
                return null;
            }
    }

    public void delete(Integer id) {
        String sql = "DELETE FROM marca WHERE id = ?;";

        try (
            Connection connection = Conexao.getConnection();
            PreparedStatement statement = connection.prepareStatement(sql);
        ) {
            statement.setInt(1, id);
            statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void delete(Marca marca) {
        delete(marca.getId());
    }

    public Marca findById(Integer id) {
        String sql = "SELECT * FROM marca WHERE id = ?;";

        try (
            Connection connection = Conexao.getConnection();
            PreparedStatement statement = connection.prepareStatement(sql);
        ) {
            statement.setInt(1, id);

            ResultSet rs = statement.executeQuery();

            if (rs.next()) {
                return resultSetToMarca(rs);
            }

            rs.close();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return null;
    }

    private Marca resultSetToMarca(ResultSet rs) throws SQLException {
        return new Marca(
            rs.getInt("id"),
            rs.getString("nome")
        );
    }
    }

Conexao.java:

    package lojadecarros.TabelasDao;

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

    public class Conexao {
    
    public static Connection getConnection() {
        //Class.forName("org.mariadb.jdbc.Driver");
        //String url = "jdbc:mysql://localhost/estudante1?user=estudante1&password=estudante1&useSSL=true";
        String url = "jdbc:mariadb://localhost/javaalunos?user=troarmen&password=0000";
        try {
            return DriverManager.getConnection(url);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            return null;
        }
    }
    }

Principal.java:

    package lojadecarros.TabelasDao.Marca;

    import java.sql.SQLException;

    import lojadecarros.Tables.Marca;

    public class Principal{
    public static void main(String[] args) throws SQLException {
        MarcaDAO marcaDao = new MarcaDAO();
        
        Marca marca = new Marca(0, "Ford");

        Marca marcaCriada = marcaDao.create(marca);

        System.out.println(marcaCriada.getId());

        System.out.println(marcaDao.findById(0));                
    }
    }

When I run the Principal(Main), I get this error:

No suitable driver found for jdbc:mariadb://localhost/javaalunos?
user=troarmen&password=0000
Exception in thread "main"\ java.lang.NullPointerException: Cannot invoke "java.sql.Connection.prepareStatement(String, int)" because "connection" is null
at lojadecarros.TabelasDao.Marca.MarcaDAO.create(MarcaDAO.java:21)
at lojadecarros.TabelasDao.Marca.Principal.main(Principal.java:13)

Beore anyones it, I already have the mariadb connector inside my project. U can see inside the lib folder. Obs: The mysql connecto its because of my school`s lab. When I clone the repositry there, I should use mysql enter image description here

0

There are 0 best solutions below