Java Dao class showing the following error:The Resource type Connection does not implement java.lang.Autocloseable

Asked

Viewed 12 times

-1

The error is being presented where I perform Try

try (Connection connection = getConnection();

And consequently, my Preparedstatement says that the Connection type is not visible in that line: PreparedStatement preparedStatement = connection.prepareStatement(INSERT_PROFISSIONAL_SQL))

Complete code below:

package dao;

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

import com.sun.jdi.connect.spi.Connection;

import model.Profissional;

public class ProfissionalDao {
    
    private String jdbcURL = "jdbc:mysql://localhost:3306/userdb?useSSL=false";
    private String jdbcUsername = "root";
    private String jdbcPassword = "root";
    private String jdbcDriver = "com.mysql.jdbc.Driver";
    
    private static final String INSERT_PROFISSIONAL_SQL = "INSER INTO profissional" + "(nome,cpf,email,dataNasc,senha,matriculaCRO) VALUES " + "(? , ? , ? , ? , ? , ?); ";
    private static final String SELECT_PROFISSIONAL_BY_ID = "select id, name, matriculaCRO from users where id =?";
    private static final String DELETE_ALL_PROFISSIONAL_SQL = "select from profissional where id = ?";
    private static final String UPDATE_PROFISSIONAL_SQL = "update profissional set name = ?, cpf ?, email = ?, dataNasc = ?, senha = ?, matriculaCRO = ?;";
    
    public ProfissionalDao() {
        
    }
    protected Connection getConnection() {
        Connection connection = null;
        try {
            Class.forName("jdbcDriver");
            connection = (Connection) DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassword);     
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return connection;
    }
    
    public void insertProfissional(Profissional profissional) throws SQLException {
        System.out.println(INSERT_PROFISSIONAL_SQL);
        try (Connection connection = getConnection();
                PreparedStatement preparedStatement = connection.prepareStatement(INSERT_PROFISSIONAL_SQL)) {
            preparedStatement.setString(1,  profissional.getNome());
            preparedStatement.setString(2,  profissional.getCpf());
            preparedStatement.setString(3,  profissional.getEmail());
            preparedStatement.setString(4,  profissional.getDataNasc());
            preparedStatement.setString(5,  profissional.getSenha());
            preparedStatement.setString(6,  profissional.getMatriculaCRO());
            System.out.println(preparedStatement);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            printSQLException(e);
        }
    }
}
No answers

Browser other questions tagged

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