try-catch 连接中出现 Java 错误“资源类型 Connection 未实现 java.lang.AutoCloseable”

问题描述 投票:0回答:3

我在 autoclosable 类中遇到此错误,在尝试建立连接以将数据插入 mysql 数据库的行中,在 try-catch 中。我做了所有必要的导入、对包和类的引用,但我没有成功

错误所在行:

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);
        }
    }
    
    }
java class mysql-connector
3个回答
1
投票
try (Connection connection = getConnection();
                PreparedStatement preparedStatement = connection.prepareStatement(INSERT_CRIANCA_SQL))

此语句意味着您可以在 try-with-resources 块中使用任何类,只要它实现 AutoCloseable 接口即可。

可能您的库版本与Java版本不兼容。你可以寻找与java版本兼容的库。


0
投票

您使用了错误的

Connection
。 您正在使用的
Connection
包中的
com.sun.jdi.connect.spi
类不是数据库连接,不能与
java.sql
中的其他类一起使用。 根据 Javadocs,它是“调试器和它所调试的目标虚拟机之间的连接。”。 它也没有实现
AutoCloseable
,导致你的错误。

Connection
的导入更改为从
java.sql
导入。 该接口确实扩展了
AutoCloseable

import java.sql.Connection;

0
投票

就我而言,

httpclient5
httpclient.execute()
在尝试下返回
String

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;
    }
);

© www.soinside.com 2019 - 2024. All rights reserved.