java - SQL 未命名参数语法
问题描述
我检查了两次为什么会继续发生这种情况?我有正确数量的未命名参数,列名是正确的。
*
您的 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);
}
}
解决方案
您可能编辑了问题,尝试了一些事情。一个干净的版本是:
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);
}
}
推荐阅读
- angular - 用 Typescript 中另一个 observable 的结果填充 observable 中的字段
- rxjs - 从 Observable 重放主题(发出所有以前的事件)
- android - 有没有办法在android中压缩png文件?
- java - 从不同的类添加 JPanel
- ruby-on-rails - 如何使用 Devise gem 在 Ruby on Rails 中保存 has_many 关系?
- mysql - MYSQL ORDER BY strtotime(column) ASC 可能吗?
- pact - 如何在我的 ProviderService 中启动模拟服务器并调用 Web 服务?
- javascript - 为什么我的第二个 if 语句在我的第一个 if 语句为真之后连续运行 2 次?
- symfony - 为 Symfony 设置 Apache 虚拟主机
- itext - 如何用希伯来语生成 PDF?目前生成的 PDF 为空