sql未命名参数语法

r3i60tvu  于 2021-07-08  发布在  Java
关注(0)|答案(3)|浏览(279)

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

sql语法有错误;检查与您的mariadb服务器版本相对应的手册,以了解在“?,?,?,?,?,?)”附近使用的正确语法在1号线

public void adiciona(Libro libro) {
    try {
    String sql = "insert into libro (isbn, titulo, precio, stock, cod_categoria, cod_editorial) values ( ? , ? , ? , ? ,?,? )";
    String sqlQuery = "select count(*) from libro where isbn = " + libro.getIsbn();
    sentencia = connection.createStatement();
    resultSet = sentencia.executeQuery(sqlQuery);
    resultSet.next();
    if (resultSet.getInt(1) == 0) {
    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());

        retorno = sentencia.executeUpdate(sql);

        preparedStatement.execute();
    } catch (SQLIntegrityConstraintViolationException e) {
        System.out.printf("error duplicado: %s\n",e);
    } 
    catch (SQLException e) {
        throw new RuntimeException(e);
    }
    }else if(retorno> 0) {System.out.println("added");

    }else { System.out.println("no added.");}

}catch (SQLException e) {
    throw new RuntimeException(e);
}
}
2exbekwf

2exbekwf1#

你可能编辑了问题,尝试了一些东西。一个干净的版本是:

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

vq8itlhq2#

您有一个不必要的行,它执行“sql”而不传递参数。删除这两行中的第一行,因为绑定变量包含在第二行中:

retorno = sentencia.executeUpdate(sql);

preparedStatement.execute();
iqih9akk

iqih9akk3#

retorno = sentencia.executeUpdate(sql); 尝试删除参数 sql

相关问题