Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 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. package lab_jdbc;
  8.  
  9. import java.sql.*;
  10. import java.util.Properties;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. /**
  14. *
  15. * @author student
  16. */
  17. public class Lab_jdbc {
  18.  
  19. /**
  20. * @param args the command line arguments
  21. */
  22. public static void main(String[] args) {
  23. // TODO code application logic here
  24.  
  25.  
  26. try {
  27. Connection conn = createConnection();
  28. ex1(conn);
  29. ex2(conn);
  30. conn.rollback();
  31. conn.close();
  32. System.out.println("Rozłączono z bazą");
  33. } catch (SQLException ex) {
  34. Logger.getLogger(Lab_jdbc.class.getName()).log(Level.SEVERE, null, ex);
  35. }
  36.  
  37.  
  38. }
  39.  
  40. public static Connection createConnection() throws SQLException
  41. {
  42. Connection conn = null;
  43. Properties connectionProps = new Properties();
  44. connectionProps.put("user","inf132218");
  45. connectionProps.put("password","inf132218");
  46. try{
  47. conn = DriverManager.getConnection(""
  48. + "jdbc:oracle:thin:@//admlab2.cs.put.poznan.pl"
  49. + ":1521/dblab02_students.cs.put.poznan.pl", connectionProps);
  50. System.out.println("Połączono");
  51. }catch (SQLException ex)
  52. {
  53. Logger.getLogger(Lab_jdbc.class.getName()).log(Level.SEVERE,"nie udało się połączyć z bazą danych", ex);
  54. System.exit(-1);
  55. }
  56. conn.setAutoCommit(false);
  57. return conn;
  58. }
  59. public static void ex1(Connection conn) throws SQLException
  60. {
  61. Statement stmt = conn.createStatement();
  62. ResultSet rs = stmt.executeQuery("Select Count(*) from Pracownicy");
  63. rs.next();
  64. System.out.println("Zatrudniono "+rs.getInt(1)+" w tym:");
  65. rs.close();
  66. rs = stmt.executeQuery("Select Count(p.id_prac), z.NAZWA From Pracownicy p JOIN ZESPOLY z ON(z.id_zesp = p.id_zesp) GROUP BY z.NAZWA");
  67. while(rs.next())
  68. {
  69. System.out.println(rs.getInt(1)+" w zespole "+rs.getString(2));
  70.  
  71. }
  72. rs.close();
  73. stmt.close();
  74. }
  75. public static void test1(Connection conn) throws SQLException
  76. {
  77. Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  78.  
  79. ResultSet rs = stmt.executeQuery("Select Count(p.id_prac), z.NAZWA From Pracownicy p JOIN ZESPOLY z ON(z.id_zesp = p.id_zesp) GROUP BY z.NAZWA");
  80. rs.afterLast();
  81. while(rs.previous())
  82. {
  83. System.out.println(rs.getInt(1)+" w zespole "+rs.getString(2));
  84.  
  85. }
  86. rs.close();
  87. stmt.close();
  88. }
  89. public static void ex2(Connection conn) throws SQLException
  90. {
  91. Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  92. ResultSet rs = stmt.executeQuery("Select NAZWISKO FROM PRACOWNICY WHERE ETAT = 'ASYSTENT' ORDER BY PLACA_POD+NVL(PLACA_DOD,0) DESC");
  93.  
  94. rs.absolute(-1);
  95. System.out.println(rs.getString(1));
  96. rs.absolute(-2);
  97. System.out.println(rs.getString(1));
  98. rs.absolute(-3);
  99. System.out.println(rs.getString(1));
  100.  
  101. rs.close();
  102. stmt.close();
  103.  
  104. rs.close();
  105. stmt.close();
  106. }
  107.  
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement