Advertisement
Guest User

Untitled

a guest
Oct 9th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.61 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.  
  7.  
  8. package lab_jdbc;
  9.  
  10. import java.sql.*;
  11. import java.util.Properties;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14.  
  15. /**
  16. *
  17. * @author student
  18. */
  19. public class Lab_JDBC {
  20.  
  21. /**
  22. * @param args the command line arguments
  23. */
  24. public static void main(String[] args) {
  25. Connection conn = null;
  26. Properties connectionProps = new Properties();
  27. connectionProps.put("user", "inf132344");
  28. connectionProps.put("password", "inf132344");
  29. try {
  30. conn = DriverManager.getConnection(
  31. "jdbc:oracle:thin:@admlab2.cs.put.poznan.pl:1521/dblab02_students.cs.put.poznan.pl",
  32. connectionProps);
  33. System.out.println("Połączono z bazą danych");
  34. } catch (SQLException ex) {
  35. Logger.getLogger(Lab_JDBC.class.getName()).log(Level.SEVERE,
  36. "nie udało się połączyć z bazą danych", ex);
  37. System.exit(-1);
  38. }
  39.  
  40.  
  41.  
  42. Statement stmt = null;
  43. ResultSet rs = null;
  44. try {
  45. stmt = conn.createStatement();
  46. rs = stmt.executeQuery("select id_prac, nazwisko, placa_pod " +
  47. "from pracownicy");
  48. while (rs.next()) {
  49. System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " +
  50. rs.getFloat(3));
  51. }
  52. } catch (SQLException ex) {
  53. System.out.println("Bład wykonania polecenia" + ex.toString());
  54. } finally {
  55. if (rs != null) {
  56. try {
  57. rs.close();
  58. } catch (SQLException e) { /* kod obsługi */ }
  59. }
  60. if (stmt != null) {
  61. try {
  62. stmt.close();
  63. } catch (SQLException e) { /* kod obsługi */ }
  64. }
  65. }
  66. // zad 1
  67. stmt = null;
  68. rs = null;
  69. try {
  70. stmt = conn.createStatement();
  71. rs = stmt.executeQuery("select count(id_zesp), id_zesp from pracownicy group by id_zesp");
  72. while (rs.next()) {
  73. System.out.println(rs.getInt(1) + " pracownikow w zespole " + rs.getInt(2));
  74. }
  75. } catch (SQLException ex) {
  76. System.out.println("Bład wykonania polecenia" + ex.toString());
  77. } finally {
  78. if (rs != null) {
  79. try {
  80. rs.close();
  81. } catch (SQLException e) { /* kod obsługi */ }
  82. }
  83. if (stmt != null) {
  84. try {
  85. stmt.close();
  86. } catch (SQLException e) { /* kod obsługi */ }
  87. }
  88. }
  89.  
  90.  
  91. // zad 2
  92. stmt = null;
  93. rs = null;
  94. try {
  95. stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  96.  
  97. rs = stmt.executeQuery("select nazwisko, placa_pod from pracownicy where etat='ASYSTENT' order by placa_pod DESC");
  98.  
  99. rs.last();
  100. System.out.println("najmniej " + rs.getString(1) + " " + rs.getFloat(2));
  101. rs.absolute(3);
  102. System.out.println("trzeci " + rs.getString(1) + " " + rs.getFloat(2));
  103. rs.absolute(-2);
  104. System.out.println("przed ostatni " + rs.getString(1) + " " + rs.getFloat(2));
  105.  
  106. } catch (SQLException ex) {
  107. System.out.println("Bład wykonania polecenia" + ex.toString());
  108. } finally {
  109. if (rs != null) {
  110. try {
  111. rs.close();
  112. } catch (SQLException e) { /* kod obsługi */ }
  113. }
  114. if (stmt != null) {
  115. try {
  116. stmt.close();
  117. } catch (SQLException e) { /* kod obsługi */ }
  118. }
  119. }
  120.  
  121.  
  122. //zad3
  123. stmt = null;
  124. rs = null;
  125. int [] zwolnienia={150, 200, 230};
  126. String [] zatrudnienia={"Kandefer", "Rygiel", "Boczar"};
  127. try {
  128. int changes;
  129. stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY );
  130. for (int i = 0; i < zwolnienia.length; i++) {
  131. changes = stmt.executeUpdate("DELETE FROM pracownicy WHERE id_prac= " + zwolnienia[i]);
  132. }
  133. System.out.println("Usunieto");
  134.  
  135. } catch (SQLException ex) {
  136. System.out.println("Bład wykonania polecenia" + ex.toString());
  137. } finally {
  138. if (rs != null) {
  139. try {
  140. rs.close();
  141. } catch (SQLException e) { /* kod obsługi */ }
  142. }
  143. if (stmt != null) {
  144. try {
  145. stmt.close();
  146. } catch (SQLException e) { /* kod obsługi */ }
  147. }
  148. }
  149.  
  150.  
  151. //zad 3 czesc 2
  152.  
  153. try {
  154. int changes;
  155. stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  156. for (int i = 0; i < zatrudnienia.length; i++) {
  157. changes = stmt.executeUpdate("INSERT into pracownicy(id_prac,nazwisko)" + "VALUES(13*i + 103, zatrudnienia[i])");
  158. }
  159. System.out.println("Dodano");
  160.  
  161. } catch (SQLException ex) {
  162. System.out.println("Bład wykonania polecenia" + ex.toString());
  163. } finally {
  164. if (rs != null) {
  165. try {
  166. rs.close();
  167. } catch (SQLException e) { /* kod obsługi */ }
  168. }
  169. if (stmt != null) {
  170. try {
  171. stmt.close();
  172. } catch (SQLException e) { /* kod obsługi */ }
  173. }
  174. }
  175.  
  176. //zad 4
  177.  
  178. stmt = null;
  179. rs = null;
  180. try {
  181. conn.setAutoCommit(false);
  182. stmt = conn.createStatement();
  183. rs = stmt.executeQuery("select nazwa" +
  184. "from etaty");
  185. while (rs.next()) {
  186. System.out.println(rs.getString(1));
  187. }
  188. // dodawanie
  189. int changes;
  190. stmt = null;
  191. stmt = conn.createStatement();
  192. changes = stmt.executeUpdate("INSERT INTO etaty(nazwa) VALUES('Wozny')");
  193. // wyswietlenie
  194. stmt = null;
  195. stmt = conn.createStatement();
  196. rs = stmt.executeQuery("select nazwa" +
  197. "from etaty");
  198. while (rs.next()) {
  199. System.out.println(rs.getString(1));
  200. }
  201. //wycofanie
  202. conn.rollback();
  203.  
  204.  
  205. // wyswietlenie po wycofaniu
  206. stmt = null;
  207. stmt = conn.createStatement();
  208. rs = stmt.executeQuery("select nazwa" +
  209. "from etaty");
  210. while (rs.next()) {
  211. System.out.println(rs.getString(1));
  212. }
  213.  
  214. // dodawanie i zapisanie
  215. stmt = null;
  216. stmt = conn.createStatement();
  217. changes = stmt.executeUpdate("INSERT INTO etaty(nazwa) VALUES('Wozny')");
  218. // wyswietlenie
  219. stmt = null;
  220. stmt = conn.createStatement();
  221. rs = stmt.executeQuery("select nazwa" +
  222. "from etaty");
  223. while (rs.next()) {
  224. System.out.println(rs.getString(1));
  225. }
  226. conn.commit();
  227.  
  228. } catch (SQLException ex) {
  229. System.out.println("Bład wykonania polecenia" + ex.toString());
  230. } finally {
  231. if (rs != null) {
  232. try {
  233. rs.close();
  234. } catch (SQLException e) { /* kod obsługi */ }
  235. }
  236. if (stmt != null) {
  237. try {
  238. stmt.close();
  239. } catch (SQLException e) { /* kod obsługi */ }
  240. }
  241. }
  242.  
  243. //zad 5 polecenia przygotowane
  244. String [] nazwiska={"Woźniak", "Dąbrowski", "Kozłowski"};
  245. int [] place_2={1300, 1700, 1500};
  246. String []etaty={"ASYSTENT", "PROFESOR", "ADIUNKT"};
  247.  
  248. try {
  249. stmt = conn.prepareStatement("INSERT into pracownicy(nazwisko,placa_pod,etat) VALUES(?,?,?)");
  250. for (int i = 0; i < nazwiska.length; i++) {
  251. stmt.setFloat(1,nazwiska[i]);
  252. stmt.setString(2,place_2[i]);
  253. stmt.setInt(3,etaty[i]);
  254. int changes = stmt.executeUpdate();
  255. }
  256.  
  257. } catch (SQLException ex) {
  258. System.out.println("Bład wykonania polecenia" + ex.toString());
  259. } finally {
  260. if (rs != null) {
  261. try {
  262. rs.close();
  263. } catch (SQLException e) { /* kod obsługi */ }
  264. }
  265. if (stmt != null) {
  266. try {
  267. stmt.close();
  268. } catch (SQLException e) { /* kod obsługi */ }
  269. }
  270. }
  271.  
  272.  
  273.  
  274.  
  275.  
  276. try {
  277. conn.close();
  278. } catch (SQLException ex) {
  279. Logger.getLogger(Lab_JDBC.class.getName()).log(Level.SEVERE, null, ex);
  280. }
  281.  
  282. System.out.println("Odlaczenie od bazy danych");
  283. }
  284.  
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement