Advertisement
Guest User

Untitled

a guest
May 28th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.59 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package main;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.Date;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.sql.Statement;
  15. import java.text.ParseException;
  16. import java.text.SimpleDateFormat;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import oracle.sql.DATE;
  20. //import java.util.Date;
  21. import java.text.SimpleDateFormat;
  22.  
  23.  
  24. /**
  25. *
  26. * @author FON
  27. */
  28. public class Main {
  29. private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
  30. private static String user = "student";
  31. private static String password = "student";
  32.  
  33. public static void main(String[] args) {
  34. /* System.out.println("Pre izmene:");
  35. vratiOdeljenja();
  36. // vratiPoSifri(20);
  37. /* izbrisi(777);
  38. System.out.println("Posle brisanja:");
  39. vratiOdeljenja();*/
  40.  
  41. /* izmeni(20);
  42. System.out.println("Posle izmene:");
  43. vratiOdeljenja();*/
  44.  
  45. /*izmeni2("Novi Sad");
  46. System.out.println("Posle izmene:");
  47. vratiOdeljenja();*/
  48.  
  49. System.out.println("Angazovanja pre ubacivanja: ");
  50. prikaziAngazovanje();
  51.  
  52.  
  53.  
  54. //ubaciAngazovanje(1, 666, '2010-05-08', '2011-08-09', 11);
  55.  
  56. }
  57.  
  58. public static void vratiOdeljenja(){
  59. String query = "select * from odeljenje";
  60.  
  61. Connection connection;
  62. try {
  63. connection = DriverManager.getConnection(url, user, password);
  64. Statement stmt = connection.createStatement();
  65. ResultSet rs = stmt.executeQuery(query);
  66.  
  67. while(rs.next()){
  68. int sifraodelj = rs.getInt("sifraodelj");
  69. String odelj = rs.getString("nazivodelj");
  70. String grad = rs.getString("grad");
  71.  
  72. System.out.println("Sifra: "+sifraodelj+" Odeljenje: "+odelj+" Grad: "+grad);
  73. }
  74.  
  75. rs.close();
  76. stmt.close();
  77. connection.close();
  78.  
  79.  
  80. } catch (SQLException ex) {
  81. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  82. }
  83.  
  84.  
  85. }
  86.  
  87. public static void ubaciOdeljenje(int sifra, String naziv, String grad){
  88. String query = "insert into odeljenje values(?,?,?)";
  89.  
  90. try {
  91. Connection connection = DriverManager.getConnection(url, user, password);
  92. connection.setAutoCommit(false);
  93. PreparedStatement ps = connection.prepareStatement(query);
  94.  
  95. ps.setInt(1, sifra);
  96. ps.setString(2, naziv);
  97. ps.setString(3, grad);
  98.  
  99. int br = ps.executeUpdate();
  100.  
  101. if(br == 1)
  102. connection.commit();
  103. else
  104. connection.rollback();
  105.  
  106. ps.close();
  107. connection.close();
  108.  
  109. } catch (SQLException ex) {
  110. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  111. }
  112. }
  113.  
  114. public static void vratiPoSifri(int sifra){
  115. String query = "select * from odeljenje where sifraodelj = ?";
  116.  
  117. try {
  118. Connection connection = DriverManager.getConnection(url, user, password);
  119. PreparedStatement ps = connection.prepareStatement(query);
  120.  
  121. ps.setInt(1, sifra);
  122.  
  123. ResultSet rs = ps.executeQuery();
  124. while(rs.next()){
  125. int sifrao = rs.getInt("sifraodelj");
  126. String naziv = rs.getString("nazivodelj");
  127. String grad = rs.getString("grad");
  128.  
  129. System.out.println("Sifra: "+sifrao+" Naziv: "+naziv+" Grad: "+grad);
  130. }
  131.  
  132. rs.close();
  133. ps.close();
  134. connection.close();
  135.  
  136. } catch (SQLException ex) {
  137. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  138. }
  139. }
  140.  
  141. public static void izbrisi(int sifra){
  142. String query = "delete from odeljenje where sifraodelj = ?";
  143.  
  144. try {
  145. Connection connection = DriverManager.getConnection(url, user, password);
  146. PreparedStatement ps = connection.prepareStatement(query);
  147.  
  148. ps.setInt(1, sifra);
  149.  
  150. int br = ps.executeUpdate();
  151.  
  152. if(br == 1)
  153. connection.commit();
  154. else
  155. connection.rollback();
  156.  
  157. ps.close();
  158. connection.close();
  159.  
  160. } catch (SQLException ex) {
  161. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  162. }
  163.  
  164. }
  165.  
  166. public static void izmeni(int sifra){
  167. String query = "update odeljenje set nazivodelj = 'Branko' where sifraodelj = ?";
  168.  
  169. try {
  170. Connection con = DriverManager.getConnection(url, user, password);
  171. PreparedStatement ps = con.prepareStatement(query);
  172.  
  173. ps.setInt(1, sifra);
  174. // ps.setString(1, naziv);
  175.  
  176. int br = ps.executeUpdate();
  177.  
  178. if(br == 1)
  179. con.commit();
  180. else con.rollback();
  181.  
  182. ps.close();
  183. con.close();
  184. } catch (SQLException ex) {
  185. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  186. }
  187. }
  188.  
  189. public static void izmeni2(String grad){
  190. String query = "update odeljenje set nazivodelj = 'teorijasistema' where grad = ?";
  191.  
  192. try {
  193. Connection con = DriverManager.getConnection(url, user, password);
  194. PreparedStatement ps = con.prepareStatement(query);
  195.  
  196. ps.setString(1, grad);
  197.  
  198. int br = ps.executeUpdate();
  199.  
  200. if(br >= 1)
  201. con.commit();
  202. else
  203. con.rollback();
  204.  
  205. ps.close();
  206. con.close();
  207.  
  208.  
  209. } catch (SQLException ex) {
  210. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  211. }
  212.  
  213. }
  214.  
  215. public static void prikaziAngazovanje(){
  216. String query = "select * from angazovanje";
  217.  
  218. try {
  219. Connection con = DriverManager.getConnection(url, user, password);
  220. Statement stmt = con.createStatement();
  221. ResultSet rs = stmt.executeQuery(query);
  222.  
  223. while(rs.next()){
  224. int sifrazap = rs.getInt("sifrazap");
  225. int sifrarm = rs.getInt("sifrarm");
  226. Date datod = rs.getDate("datod");
  227. Date datdo = rs.getDate("datdo");
  228. int sifrars = rs.getInt("sifrars");
  229.  
  230. System.out.println("Sifra zaposlenog: "+sifrazap+" Sifra radnog mesta: "+sifrarm+" Datum od: "+datod+" Datum do: "+datdo+" Sifra radnos statusa: "+sifrars);
  231.  
  232. }
  233.  
  234. rs.close();
  235. stmt.close();
  236. con.close();
  237. } catch (SQLException ex) {
  238. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  239. }
  240. }
  241.  
  242. public static void ubaciAngazovanje(int sifrazap, int sifrarm, Date datod, Date datdo, int sifrars){
  243. String query = "insert into angazovanje values(?,?,?,?,?)";
  244.  
  245. try {
  246. Connection con = DriverManager.getConnection(url, user, password);
  247. PreparedStatement ps = con.prepareStatement(query);
  248.  
  249. ps.setInt(1, sifrazap);
  250. ps.setInt(2, sifrarm);
  251. ps.setDate(3, datod);
  252. ps.setDate(4, datdo);
  253. ps.setInt(5, sifrars);
  254.  
  255. int br = ps.executeUpdate();
  256.  
  257. if(br == 1)
  258. con.commit();
  259. else
  260. con.rollback();
  261.  
  262. ps.close();
  263. con.close();
  264. } catch (SQLException ex) {
  265. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  266. }
  267. }
  268.  
  269.  
  270.  
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement