Commit a5311045 by Patryk Czarnik

parametry

parent 6b196884
package sklep.przyklady;
import java.math.BigDecimal;
import java.util.List;
import java.util.Scanner;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import sklep.model.Product;
public class Parametry {
public static void main(String[] args) {
EntityManagerFactory emf = null;
EntityManager em = null;
try {
Scanner sc = new Scanner(System.in);
emf = Persistence.createEntityManagerFactory("sklep");
em = emf.createEntityManager();
System.out.print("Podaj minimalną cenę: ");
BigDecimal min = sc.nextBigDecimal();
System.out.print("Podaj maksymalną cenę: ");
BigDecimal max = sc.nextBigDecimal();
TypedQuery<Product> query = em.createQuery(
"SELECT p FROM Product p WHERE p.price BETWEEN :minprice AND :maxprice",
Product.class);
query.setParameter("minprice", min);
query.setParameter("maxprice", max);
List<Product> products = query.getResultList();
System.out.println("Odczytano " + products.size() + " rekordów:");
for (Product product : products) {
System.out.println(" * " + product.getProductName() + " za " + product.getPrice());
}
} finally {
if(em != null)
em.close();
if(emf != null)
emf.close();
}
}
}
package sklep.przyklady;
import java.math.BigDecimal;
import java.util.List;
import java.util.Scanner;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import sklep.model.Product;
public class ParametryPozycyjne {
public static void main(String[] args) {
EntityManagerFactory emf = null;
EntityManager em = null;
try {
Scanner sc = new Scanner(System.in);
emf = Persistence.createEntityManagerFactory("sklep");
em = emf.createEntityManager();
System.out.print("Podaj minimalną cenę: ");
BigDecimal min = sc.nextBigDecimal();
System.out.print("Podaj maksymalną cenę: ");
BigDecimal max = sc.nextBigDecimal();
TypedQuery<Product> query = em.createQuery(
"SELECT p FROM Product p WHERE p.price BETWEEN ?1 AND ?2",
Product.class);
query.setParameter(1, min);
query.setParameter(2, max);
List<Product> products = query.getResultList();
System.out.println("Odczytano " + products.size() + " rekordów:");
for (Product product : products) {
System.out.println(" * " + product.getProductName() + " za " + product.getPrice());
}
} finally {
if(em != null)
em.close();
if(emf != null)
emf.close();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment