Java error "The resource type Connection does not implement java.lang.AutoCloseable" in the try-catch connection

I'm having this error in the autoclosable class, on the line where I trying to establish a connection to insert data into mysql database, in the try-catch.I did all the necessary imports, references to packages and classes, but I was not successful

Line of the error:

try (Connection connection = getConnection();
                PreparedStatement preparedStatement = connection.prepareStatement(INSERT_CRIANCA_SQL))
package dao;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

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

import model.Crianca;

public class CriancaDao {
    
    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_CRIANCA_SQL = "INSER INTO crianca" + "(nome,cpf,email,dataNasc,senha,responsavel,dentistaResponsavel,avatar,atendimento) VALUES " + "(? , ? , ? , ? , ? , ?, ?, ?, ?); ";
    private static final String SELECT_CRIANCA_BY_ID = "select id, nome,cpf,email,dataNasc,senha,responsavel,dentistaResponsavel,avatar,atendimento from crianca  where id =?";
    private static final String SELECT_ALL_CRIANCA = "select * from crianca";
    private static final String DELETE_CRIANCA_SQL = "select from crianca where id = ?";
    private static final String UPDATE_CRIANCA_SQL = "update crianca set name = ?, cpf ?, email = ?, dataNasc = ?, senha = ?, responsavel = ?, dentistaResponsavel = ?, avatar = ?, atendimento = ? ;";
    
    public CriancaDao() {
        
    }
    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 insertCrianca(Crianca crianca) throws SQLException {
        System.out.println(INSERT_CRIANCA_SQL);
        try (Connection connection = getConnection();
                PreparedStatement preparedStatement = connection.prepareStatement(INSERT_CRIANCA_SQL)) {
            preparedStatement.setString(1,  crianca.getNome());
            preparedStatement.setString(2,  crianca.getCpf());
            preparedStatement.setString(3,  crianca.getEmail());
            preparedStatement.setString(4,  crianca.getDataNasc());
            preparedStatement.setString(5,  crianca.getSenha());
            preparedStatement.setString(6,  crianca.getResponsavel());
            preparedStatement.setString(7,  crianca.getDentistaResponsavel());
            preparedStatement.setString(8,  crianca.getAvatar());
            preparedStatement.setString(9,  crianca.getAtendimento());
            System.out.println(preparedStatement);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            printSQLException(e);
        }
    }
    
    }

Upvotes: 2

Views: 5486

Answers (3)

yu yang Jian
yu yang Jian

Reputation: 7161

In my case, httpclient5 let httpclient.execute() return String under the try:

try( CloseableHttpClient httpClient = HttpClients.custom()
    .setConnectionManager(connectionManager)
    .build();

 String str = httpClient.execute(getMethod, 
 response -> {
      if (response.getCode() >= 300) {
          throw new ClientProtocolException(new StatusLine(response).toString());
      }
      
      final int statusCode = response.getCode();
      final HttpEntity entity = response.getEntity();
      String responseString = EntityUtils.toString(entity, "UTF-8");
      return responseString;
    }
);

Upvotes: 0

rgettman
rgettman

Reputation: 178263

You are using the wrong Connection. The Connection class you're using, from the package com.sun.jdi.connect.spi, isn't a database connection and can't be used with other classes from java.sql. It's "[a] connection between a debugger and a target VM which it debugs.", according to the Javadocs. It also does not implement AutoCloseable, causing your error.

Change your import for Connection to import from java.sql. That interface does extend AutoCloseable.

import java.sql.Connection;

Upvotes: 0

vaibhavsahu
vaibhavsahu

Reputation: 682

try (Connection connection = getConnection();
                PreparedStatement preparedStatement = connection.prepareStatement(INSERT_CRIANCA_SQL))

This statement means you can use any class inside try-with-resources block as long as it implements AutoCloseable interface.

Maybe your library version is not compatible with Java version. you can look for library that is compatible with java version.

Upvotes: 1

Related Questions