Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- JDBC 1
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package JDBC;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author student
- */
- public class JDBCTest1 {
- public static void main(String[] args) {
- nadjiProdukt();
- }
- private static void nadjiProdukt() {
- String url="jdbc:derby://localhost:1527/sample";
- String upit="select * from PRODUCT";
- try
- (Connection conn=DriverManager.getConnection(url,"app","app");
- Statement stat=conn.createStatement();
- ResultSet rs=stat.executeQuery(upit)) //executeQuery ---> select, executeUpdate ---> svi ostali
- {
- while (rs.next()) {
- int pId=rs.getInt(1); // moze i int pId=rs.getInt("PRODUCT_ID");
- int mId=rs.getInt(2);
- String pCode=rs.getString(3);
- System.out.println("pId :"+pId+
- " mId :"+mId+
- " pCode :"+pCode);
- }
- } catch (SQLException ex) {
- Logger.getLogger(JDBCTest1.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- JDBC 2
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package JDBC;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author student
- */
- public class JDBCTest2 {
- public static void main(String[] args) {
- dodajProduct(33,19985678,"SW",5.5,7,8.8,"DAA","NE");
- }
- private static void dodajProduct(int i, int i0, String sw, double d, int i2, double d0, String daa, String ne) {
- String url="jdbc:derby://localhost:1527/sample";
- String upit="insert into PRODUCT values (?,?,?,?,?,?,?,?)";
- try
- (Connection conn= DriverManager.getConnection(url, "app","app");
- PreparedStatement stat= conn.prepareStatement(upit))
- {
- conn.setAutoCommit(false);
- stat.setInt(1, i);
- stat.setInt(2, i0);
- stat.setString(3, sw);
- stat.setDouble(4, d);
- stat.setInt(5, i2);
- stat.setDouble(6, d0);
- stat.setString(7, daa);
- stat.setString(8, ne);
- int count=stat.executeUpdate();
- if (count>0){
- conn.commit();
- }else{
- conn.rollback();
- }
- } catch (SQLException ex) {
- Logger.getLogger(JDBCTest2.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- JPA 1
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package jpa;
- import domen.Product;
- import java.util.List;
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.Persistence;
- /**
- *
- * @author student
- */
- public class JPATest1 {
- public static void main(String[] args) {
- nadjiProduct();
- }
- private static void nadjiProduct() {
- EntityManagerFactory emf= Persistence.createEntityManagerFactory("JPA2PU");
- EntityManager em = emf.createEntityManager();
- em.getTransaction().begin();
- List<Product> proizvodi = em.createNamedQuery("Product.findAll").getResultList();
- for (Product p : proizvodi) {
- int pId = p.getProductId();
- int mId = p.getManufacturerId().getManufacturerId();
- String pCode = p.getProductCode().getProdCode();
- System.out.println("PID :"+pId+" MID :"+mId+" PCODE :"+pCode);
- }
- em.close();
- emf.close();
- }
- }
- JPA 2
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package jpa;
- import domen.Manufacturer;
- import domen.Product;
- import domen.ProductCode;
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.Persistence;
- /**
- *
- * @author student
- */
- public class JPATest2 {
- public static void main(String[] args) {
- dodajProduct(55,19985678,"SW",5.5,7,8.8,"DAA","NE");
- }
- private static void dodajProduct(int i, int i0, String sw, double d, int i1, double d0, String daa, String ne) {
- EntityManagerFactory emf= Persistence.createEntityManagerFactory("JPA2PU");
- EntityManager em = emf.createEntityManager();
- em.getTransaction().begin();
- Product proizvod = new Product();
- proizvod.setProductId(i);
- Manufacturer manufacturer = new Manufacturer(i0);
- proizvod.setManufacturerId(manufacturer);
- ProductCode pc = new ProductCode(sw);
- proizvod.setProductCode(pc);
- em.persist(proizvod);
- em.getTransaction().commit();
- em.close();
- emf.close();
- }
- }
- UPDATE
- package jpa;
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.Persistence;
- import jpa_entity.Odeljenje;
- public class JPATest3 {
- public static void main(String[] args) {
- azurirajOdeljenje(150, "Novo odeljenje", "Novi Sad");
- }
- public static void azurirajOdeljenje(int sifra, String naziv,
- String grad) {
- EntityManagerFactory emf =
- Persistence.createEntityManagerFactory("JPAPU");
- EntityManager em = emf.createEntityManager();
- Odeljenje o = new Odeljenje(sifra);
- o.setNazivodelj(naziv);
- o.setGrad(grad);
- em.getTransaction().begin();
- em.merge(o);
- em.getTransaction().commit();
- }
- }
- DELETE
- package jpa;
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.Persistence;
- import jpa_entity.Odeljenje;
- public class JPATest4 {
- public static void main(String[] args) {
- obrisiOdeljenje(150);
- }
- public static void obrisiOdeljenje(int sifra){
- EntityManagerFactory emf
- = Persistence.createEntityManagerFactory("JPAPU");
- EntityManager em = emf.createEntityManager();
- Odeljenje o = em.find(Odeljenje.class, sifra);
- em.getTransaction().begin();
- em.remove(o);
- em.getTransaction().commit();
- }
- }
Add Comment
Please, Sign In to add comment