Guest User

Untitled

a guest
Oct 9th, 2018
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.66 KB | None | 0 0
  1. ZADANIE 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 lab01;
  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.Properties;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18.  
  19. /**
  20. *
  21. * @author student
  22. */
  23. public class Lab01 {
  24.  
  25. /**
  26. * @param args the command line arguments
  27. */
  28. public static void main(String[] args) {
  29.  
  30. Connection conn = null;
  31. Properties connectionProps = new Properties();
  32. connectionProps.put("user", "inf132310");
  33. connectionProps.put("password", "inf132310");
  34.  
  35. try
  36. {
  37. conn = DriverManager.getConnection("jdbc:oracle:thin:@//admlab2.cs.put.poznan.pl:1521/dblab02_students.cs.put.poznan.pl", connectionProps);
  38. System.out.println("Połączono z bazą danych");
  39. Statement stmt1 = null;
  40. Statement stmt2 = null;
  41. ResultSet rs1 = null;
  42. ResultSet rs2 = null;
  43. try
  44. {
  45. stmt1 = conn.createStatement();
  46. stmt2 = conn.createStatement();
  47. rs1 = stmt1.executeQuery("select count(*) as lprac_total from pracownicy");
  48. rs2 = stmt2.executeQuery("select count(*) as lprac, id_zesp from pracownicy group by id_zesp");
  49. rs1.next();
  50. System.out.println("Zatrudniono " + rs1.getInt("lprac_total") + " pracowników, w tym:");
  51. while (rs2.next()) {
  52. System.out.println(rs2.getInt("lprac") + " w zespole " + rs2.getInt("id_zesp") + ",");
  53. }
  54. }
  55. catch (SQLException ex) {
  56. System.out.println("Bład wykonania polecenia" + ex.toString());
  57. }
  58. finally {
  59. if (rs1 != null) {
  60. try
  61. {
  62. rs1.close();
  63. }
  64. catch (SQLException e) {
  65. /* kod obsługi */
  66. }
  67. }
  68. if (stmt1 != null) {
  69. try {
  70. stmt1.close();
  71. }
  72. catch (SQLException e) {
  73. /* kod obsługi */
  74. }
  75. }
  76. }
  77. }
  78. catch (SQLException ex) {
  79. Logger.getLogger(Lab_JDBC.class.getName()).log(Level.SEVERE, "nie udało się połączyć z bazą danych", ex);
  80. System.exit(-1);
  81. }
  82. try {
  83. conn.close();
  84. // TODO code application logic here
  85. } catch (SQLException ex) {
  86. Logger.getLogger(Lab01.class.getName()).log(Level.SEVERE, null, ex);
  87. }
  88. System.out.println("exit db");
  89. }
  90.  
  91. }
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98. ZADANIE 2:
  99.  
  100. /*
  101. * To change this license header, choose License Headers in Project Properties.
  102. * To change this template file, choose Tools | Templates
  103. * and open the template in the editor.
  104. */
  105. package lab01;
  106.  
  107. import java.sql.Connection;
  108. import java.sql.DriverManager;
  109. import java.sql.ResultSet;
  110. import java.sql.SQLException;
  111. import java.sql.Statement;
  112. import java.util.Properties;
  113. import java.util.logging.Level;
  114. import java.util.logging.Logger;
  115.  
  116. /**
  117. *
  118. * @author student
  119. */
  120. public class Lab01 {
  121.  
  122. /**
  123. * @param args the command line arguments
  124. */
  125. public static void main(String[] args) {
  126.  
  127. Connection conn = null;
  128. Properties connectionProps = new Properties();
  129. connectionProps.put("user", "inf132310");
  130. connectionProps.put("password", "inf132310");
  131.  
  132. try
  133. {
  134. conn = DriverManager.getConnection("jdbc:oracle:thin:@//admlab2.cs.put.poznan.pl:1521/dblab02_students.cs.put.poznan.pl", connectionProps);
  135. System.out.println("Połączono z bazą danych");
  136. Statement stmt1 = null;
  137. Statement stmt2 = null;
  138. ResultSet rs1 = null;
  139. ResultSet rs2 = null;
  140. try
  141. {
  142. stmt1 = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  143. rs1 = stmt1.executeQuery("select nazwisko from pracownicy where etat='ASYSTENT' order by placa_pod desc");
  144. rs1.last();
  145. System.out.println(rs1.getString("nazwisko"));
  146. rs1.absolute(-3);
  147. System.out.println(rs1.getString("nazwisko"));
  148. rs1.absolute(-2);
  149. System.out.println(rs1.getString("nazwisko"));
  150. }
  151. catch (SQLException ex) {
  152. System.out.println("Bład wykonania polecenia" + ex.toString());
  153. }
  154. finally {
  155. if (rs1 != null) {
  156. try
  157. {
  158. rs1.close();
  159. }
  160. catch (SQLException e) {
  161. /* kod obsługi */
  162. }
  163. }
  164. if (stmt1 != null) {
  165. try {
  166. stmt1.close();
  167. }
  168. catch (SQLException e) {
  169. /* kod obsługi */
  170. }
  171. }
  172. }
  173. }
  174. catch (SQLException ex) {
  175. Logger.getLogger(Lab_JDBC.class.getName()).log(Level.SEVERE, "nie udało się połączyć z bazą danych", ex);
  176. System.exit(-1);
  177. }
  178. try {
  179. conn.close();
  180. // TODO code application logic here
  181. } catch (SQLException ex) {
  182. Logger.getLogger(Lab01.class.getName()).log(Level.SEVERE, null, ex);
  183. }
  184. System.out.println("exit db");
  185. }
  186.  
  187. }
  188.  
  189.  
  190. ZADANIE 3:
  191.  
  192. /*
  193. * To change this license header, choose License Headers in Project Properties.
  194. * To change this template file, choose Tools | Templates
  195. * and open the template in the editor.
  196. */
  197. package lab01;
  198.  
  199. import java.sql.Connection;
  200. import java.sql.DriverManager;
  201. import java.sql.ResultSet;
  202. import java.sql.SQLException;
  203. import java.sql.Statement;
  204. import java.util.Properties;
  205. import java.util.logging.Level;
  206. import java.util.logging.Logger;
  207.  
  208. /**
  209. *
  210. * @author student
  211. */
  212. public class Lab01 {
  213.  
  214. /**
  215. * @param args the command line arguments
  216. */
  217. public static void main(String[] args) {
  218.  
  219. Connection conn = null;
  220. Properties connectionProps = new Properties();
  221. connectionProps.put("user", "inf132310");
  222. connectionProps.put("password", "inf132310");
  223.  
  224. try
  225. {
  226. conn = DriverManager.getConnection("jdbc:oracle:thin:@//admlab2.cs.put.poznan.pl:1521/dblab02_students.cs.put.poznan.pl", connectionProps);
  227. System.out.println("Połączono z bazą danych");
  228. Statement stmt1 = null;
  229. Statement stmt2 = null;
  230. ResultSet rs1 = null;
  231. ResultSet rs2 = null;
  232. try
  233. {
  234. int [] zwolnienia={150, 200, 230};
  235. String [] zatrudnienia={"Kandefer", "Rygiel", "Boczar"};
  236. int [] zatrudnienia_id_prac={240,250,260};
  237. stmt1 = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
  238. for(int i=0;i<3;i++) {
  239. stmt1.executeUpdate("delete from pracownicy where id_prac=" + zwolnienia[i]);
  240. stmt1.executeUpdate("insert into pracownicy(id_prac, nazwisko) values('" + zatrudnienia_id_prac[i] + "', '" + zatrudnienia[i] + "')");
  241. }
  242. stmt1.close();
  243. }
  244. catch (SQLException ex) {
  245. System.out.println("Bład wykonania polecenia " + ex.toString());
  246. }
  247. finally {
  248. if (rs1 != null) {
  249. try
  250. {
  251. rs1.close();
  252. }
  253. catch (SQLException e) {
  254. /* kod obsługi */
  255. }
  256. }
  257. if (stmt1 != null) {
  258. try {
  259. stmt1.close();
  260. }
  261. catch (SQLException e) {
  262. /* kod obsługi */
  263. }
  264. }
  265. }
  266. }
  267. catch (SQLException ex) {
  268. Logger.getLogger(Lab_JDBC.class.getName()).log(Level.SEVERE, "nie udało się połączyć z bazą danych", ex);
  269. System.exit(-1);
  270. }
  271. try {
  272. conn.close();
  273. // TODO code application logic here
  274. } catch (SQLException ex) {
  275. Logger.getLogger(Lab01.class.getName()).log(Level.SEVERE, null, ex);
  276. }
  277. System.out.println("exit db");
  278. }
  279.  
  280. }
  281.  
  282.  
  283.  
  284. ZADANIE 4:
Add Comment
Please, Sign In to add comment