Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SQL未命名参数语法_Java_Mysql_Sql_Mariadb - Fatal编程技术网

Java SQL未命名参数语法

Java SQL未命名参数语法,java,mysql,sql,mariadb,Java,Mysql,Sql,Mariadb,我查了两次为什么会这样?我有正确数量的未命名参数,列名称正确 * 您的SQL语法有错误;检查手册 对应于您的MariaDB服务器版本,具有正确的语法to 在“?,?,?,?,?,?,?)”附近使用在第1行 Atreturno=判决执行更新(sql)尝试删除参数sql您可能编辑了问题,尝试了一些方法。一个干净的版本是: public void adiciona(Libro libro) { // Not needed: String sqlQuery = "

我查了两次为什么会这样?我有正确数量的未命名参数,列名称正确

*

您的SQL语法有错误;检查手册 对应于您的MariaDB服务器版本,具有正确的语法to 在“?,?,?,?,?,?,?)”附近使用在第1行


At
returno=判决执行更新(sql)
尝试删除参数
sql

您可能编辑了问题,尝试了一些方法。一个干净的版本是:

public void adiciona(Libro libro) {
    
    // Not needed:
    String sqlQuery = "select count(*) from libro where isbn = ?";
    try (PreparedStatement sentencia = connection.createStatement(sqlQuery)) {
        sentencia.setInt(libro.getIsbn());
        try (ResultSet resultSet = sentencia.executeQuery()) {
            if (resultSet.next() && sentencia.getLong(1) > 0L) {
                return;
            }
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }

   String sql = "insert into libro (isbn, titulo, precio, stock, cod_categoria, cod_editorial) "
        + "values (?, ?, ?, ?, ?, ?)";
    try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
        preparedStatement.setInt(1, libro.getIsbn());
        preparedStatement.setString(2, libro.getTitulo());
        preparedStatement.setDouble(3, libro.getPrecio());
        preparedStatement.setInt(4, libro.getStock());
        preparedStatement.setInt(5, libro.getCod_categoria());
        preparedStatement.setInt(6, libro.getCod_editorial());
        
        int retorno = preparedStatement.executeUpdate();
        if (retorno > 0) {
            System.out.println("added")
        } else {
            System.out.println("not added.");
        }
    } catch (SQLIntegrityConstraintViolationException e) {
        System.out.printf("error duplicado: %s\n", e);
        throw new RuntimeException(e);
    } catch (SQLException e2) {
        throw new RuntimeException(e2);
    }
}

@EmanuelMosby Note
和&sentencia.getLong(1)>0L
我后来添加到了不需要的代码中。
public void adiciona(Libro libro) {
    
    // Not needed:
    String sqlQuery = "select count(*) from libro where isbn = ?";
    try (PreparedStatement sentencia = connection.createStatement(sqlQuery)) {
        sentencia.setInt(libro.getIsbn());
        try (ResultSet resultSet = sentencia.executeQuery()) {
            if (resultSet.next() && sentencia.getLong(1) > 0L) {
                return;
            }
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }

   String sql = "insert into libro (isbn, titulo, precio, stock, cod_categoria, cod_editorial) "
        + "values (?, ?, ?, ?, ?, ?)";
    try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
        preparedStatement.setInt(1, libro.getIsbn());
        preparedStatement.setString(2, libro.getTitulo());
        preparedStatement.setDouble(3, libro.getPrecio());
        preparedStatement.setInt(4, libro.getStock());
        preparedStatement.setInt(5, libro.getCod_categoria());
        preparedStatement.setInt(6, libro.getCod_editorial());
        
        int retorno = preparedStatement.executeUpdate();
        if (retorno > 0) {
            System.out.println("added")
        } else {
            System.out.println("not added.");
        }
    } catch (SQLIntegrityConstraintViolationException e) {
        System.out.printf("error duplicado: %s\n", e);
        throw new RuntimeException(e);
    } catch (SQLException e2) {
        throw new RuntimeException(e2);
    }
}