Advertisement
Guest User

Untitled

a guest
May 16th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. JDBC 1
  2.  
  3. /*
  4. * To change this license header, choose License Headers in Project Properties.
  5. * To change this template file, choose Tools | Templates
  6. * and open the template in the editor.
  7. */
  8. package JDBC;
  9.  
  10. import java.sql.Connection;
  11. import java.sql.DriverManager;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.sql.Statement;
  15. import java.util.logging.Level;
  16. import java.util.logging.Logger;
  17.  
  18. /**
  19. *
  20. * @author student
  21. */
  22. public class JDBCTest1 {
  23. public static void main(String[] args) {
  24.  
  25. nadjiProdukt();
  26. }
  27.  
  28. private static void nadjiProdukt() {
  29.  
  30. String url="jdbc:derby://localhost:1527/sample";
  31. String upit="select * from PRODUCT";
  32.  
  33. try
  34. (Connection conn=DriverManager.getConnection(url,"app","app");
  35. Statement stat=conn.createStatement();
  36. ResultSet rs=stat.executeQuery(upit)) //executeQuery ---> select, executeUpdate ---> svi ostali
  37. {
  38. while (rs.next()) {
  39. int pId=rs.getInt(1); // moze i int pId=rs.getInt("PRODUCT_ID");
  40. int mId=rs.getInt(2);
  41. String pCode=rs.getString(3);
  42.  
  43. System.out.println("pId :"+pId+
  44. " mId :"+mId+
  45. " pCode :"+pCode);
  46.  
  47. }
  48.  
  49.  
  50. } catch (SQLException ex) {
  51. Logger.getLogger(JDBCTest1.class.getName()).log(Level.SEVERE, null, ex);
  52. }
  53.  
  54. }
  55. }
  56.  
  57.  
  58. JDBC 2
  59.  
  60. /*
  61. * To change this license header, choose License Headers in Project Properties.
  62. * To change this template file, choose Tools | Templates
  63. * and open the template in the editor.
  64. */
  65. package JDBC;
  66.  
  67. import java.sql.Connection;
  68. import java.sql.DriverManager;
  69. import java.sql.PreparedStatement;
  70. import java.sql.SQLException;
  71. import java.util.logging.Level;
  72. import java.util.logging.Logger;
  73.  
  74. /**
  75. *
  76. * @author student
  77. */
  78. public class JDBCTest2 {
  79. public static void main(String[] args) {
  80.  
  81. dodajProduct(33,19985678,"SW",5.5,7,8.8,"DAA","NE");
  82. }
  83.  
  84. private static void dodajProduct(int i, int i0, String sw, double d, int i2, double d0, String daa, String ne) {
  85. String url="jdbc:derby://localhost:1527/sample";
  86. String upit="insert into PRODUCT values (?,?,?,?,?,?,?,?)";
  87.  
  88. try
  89. (Connection conn= DriverManager.getConnection(url, "app","app");
  90. PreparedStatement stat= conn.prepareStatement(upit))
  91.  
  92.  
  93. {
  94.  
  95. conn.setAutoCommit(false);
  96.  
  97. stat.setInt(1, i);
  98. stat.setInt(2, i0);
  99. stat.setString(3, sw);
  100. stat.setDouble(4, d);
  101. stat.setInt(5, i2);
  102. stat.setDouble(6, d0);
  103. stat.setString(7, daa);
  104. stat.setString(8, ne);
  105.  
  106. int count=stat.executeUpdate();
  107.  
  108. if (count>0){
  109. conn.commit();
  110. }else{
  111. conn.rollback();
  112. }
  113. } catch (SQLException ex) {
  114. Logger.getLogger(JDBCTest2.class.getName()).log(Level.SEVERE, null, ex);
  115. }
  116. }
  117. }
  118.  
  119.  
  120. JPA 1
  121.  
  122. /*
  123. * To change this license header, choose License Headers in Project Properties.
  124. * To change this template file, choose Tools | Templates
  125. * and open the template in the editor.
  126. */
  127. package jpa;
  128.  
  129. import domen.Product;
  130. import java.util.List;
  131. import javax.persistence.EntityManager;
  132. import javax.persistence.EntityManagerFactory;
  133. import javax.persistence.Persistence;
  134.  
  135. /**
  136. *
  137. * @author student
  138. */
  139. public class JPATest1 {
  140. public static void main(String[] args) {
  141. nadjiProduct();
  142. }
  143.  
  144. private static void nadjiProduct() {
  145. EntityManagerFactory emf= Persistence.createEntityManagerFactory("JPA2PU");
  146. EntityManager em = emf.createEntityManager();
  147. em.getTransaction().begin();
  148. List<Product> proizvodi = em.createNamedQuery("Product.findAll").getResultList();
  149.  
  150. for (Product p : proizvodi) {
  151. int pId = p.getProductId();
  152. int mId = p.getManufacturerId().getManufacturerId();
  153. String pCode = p.getProductCode().getProdCode();
  154.  
  155. System.out.println("PID :"+pId+" MID :"+mId+" PCODE :"+pCode);
  156. }
  157.  
  158. em.close();
  159. emf.close();
  160.  
  161.  
  162. }
  163. }
  164.  
  165.  
  166. JPA 2
  167.  
  168. /*
  169. * To change this license header, choose License Headers in Project Properties.
  170. * To change this template file, choose Tools | Templates
  171. * and open the template in the editor.
  172. */
  173. package jpa;
  174.  
  175. import domen.Manufacturer;
  176. import domen.Product;
  177. import domen.ProductCode;
  178. import javax.persistence.EntityManager;
  179. import javax.persistence.EntityManagerFactory;
  180. import javax.persistence.Persistence;
  181.  
  182. /**
  183. *
  184. * @author student
  185. */
  186. public class JPATest2 {
  187.  
  188. public static void main(String[] args) {
  189. dodajProduct(55,19985678,"SW",5.5,7,8.8,"DAA","NE");
  190. }
  191.  
  192. private static void dodajProduct(int i, int i0, String sw, double d, int i1, double d0, String daa, String ne) {
  193. EntityManagerFactory emf= Persistence.createEntityManagerFactory("JPA2PU");
  194. EntityManager em = emf.createEntityManager();
  195.  
  196. em.getTransaction().begin();
  197.  
  198. Product proizvod = new Product();
  199. proizvod.setProductId(i);
  200. Manufacturer manufacturer = new Manufacturer(i0);
  201. proizvod.setManufacturerId(manufacturer);
  202. ProductCode pc = new ProductCode(sw);
  203. proizvod.setProductCode(pc);
  204.  
  205. em.persist(proizvod);
  206.  
  207. em.getTransaction().commit();
  208. em.close();
  209. emf.close();
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement