MySQL'de iki tablom var tableAve tableB. İki sorgu yürütmeye çalışıyorum:
executeQuery(query1)
executeQuery(query2)
Ama şu hatayı alıyorum:
can not issue data manipulation statements with executeQuery().
Ne anlama geliyor?
MySQL'de iki tablom var tableAve tableB. İki sorgu yürütmeye çalışıyorum:
executeQuery(query1)
executeQuery(query2)
Ama şu hatayı alıyorum:
can not issue data manipulation statements with executeQuery().
Ne anlama geliyor?
Yanıtlar:
Verileri işlemek için gerçekten ihtiyacınız olan executeUpdate()şey executeQuery().
İşte executeUpdate()zaten kendi başına bir cevap olan javadoc'tan bir alıntı :
Bir INSERT, UPDATE veya DELETE ifadesi veya SQL DDL ifadesi gibi hiçbir şey döndürmeyen bir SQL ifadesi olabilen verilen SQL ifadesini yürütür.
Yaylı önyükleme kullanıyorsanız, bir @ Değiştirme notu eklemeniz yeterlidir.
@Modifying
@Query
(value = "UPDATE user SET middleName = 'Mudd' WHERE id = 1", nativeQuery = true)
void updateMiddleName();
@Modifying(clearAutomatically = true) @TransactionalSorunumu şunu kullanarak çözdüm : silme sorgumu tanımlayan @Query ek açıklamasının hemen üstünde
executeUpdate()Veri işleme ifadeleri yayınlamak için kullanın . executeQuery()yalnızca SELECT sorguları içindir (yani bir sonuç kümesi döndüren sorgular).
Sil sorgu için - Kullanım @Modifyingve @Transactionalöncesi @Querygibi: -
@Repository
public interface CopyRepository extends JpaRepository<Copy, Integer> {
@Modifying
@Transactional
@Query(value = "DELETE FROM tbl_copy where trade_id = ?1 ; ", nativeQuery = true)
void deleteCopyByTradeId(Integer id);
}
java.sql.SQLException: Can not issue data manipulation statements with executeQuery()Hatayı vermez .
Düzenle:
Bu cevap birçok olumlu oy aldığından, daha fazla anlayış için sizi belgelere de yönlendireceğim.
By default, CRUD methods on repository instances are transactional. For read operations,
the transaction configuration readOnly flag is set to true.
All others are configured with a plain @Transactional so that default transaction
configuration applies.
Indicates a query method should be considered as modifying query as that changes the way
it needs to be executed. This annotation is only considered if used on query methods defined
through a Query annotation). It's not applied on custom implementation methods or queries
derived from the method name as they already have control over the underlying data access
APIs or specify if they are modifying by their name.
Queries that require a @Modifying annotation include INSERT, UPDATE, DELETE, and DDL
statements.
Bunun executeUpdateiçin.
Farkın çok kısa bir özeti: http://www.coderanch.com/t/301594/JDBC/java/Difference-between-execute-executeQuery-executeUpdate
Bu kod benim için çalışıyor: Değerleri bir INSERT ile ayarlıyorum ve bu değerin LAST_INSERT_ID () değerini bir SELECT; Java NetBeans 8.1, MySql ve java.JDBC.driver kullanıyorum
try {
String Query = "INSERT INTO `stock`(`stock`, `min_stock`,
`id_stock`) VALUES ("
+ "\"" + p.get_Stock().getStock() + "\", "
+ "\"" + p.get_Stock().getStockMinimo() + "\","
+ "" + "null" + ")";
Statement st = miConexion.createStatement();
st.executeUpdate(Query);
java.sql.ResultSet rs;
rs = st.executeQuery("Select LAST_INSERT_ID() from stock limit 1");
rs.next(); //para posicionar el puntero en la primer fila
ultimo_id = rs.getInt("LAST_INSERT_ID()");
} catch (SqlException ex) { ex.printTrace;}
@Modifying
@Transactional
@Query(value = "delete from cart_item where cart_cart_id=:cart", nativeQuery = true)
public void deleteByCart(@Param("cart") int cart);
@Query'den önce @Modifying ve @Transnational eklemeyi unutmayın. benim için çalışıyor.
JPA ile yerel sorgu kullanarak bazı koşullara sahip kaydı silmek için, yukarıda belirtilen ek açıklamalar önemlidir.
Parantez üzerindeki executeUpdate () işlevinin yanı sıra, bir SQL deyimi kullanmak için bir değişken de eklemelisiniz.
Örneğin:
PreparedStatement pst = connection.prepareStatement(sql);
int numRowsChanged = pst.executeUpdate(sql);