Advertisement
Guest User

Untitled

a guest
Nov 27th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 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 lab_jdbc;
  7. import java.sql.*;
  8. import java.util.Properties;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11.  
  12. /**
  13. *
  14. * @author student
  15. */
  16. public class Lab_JDBC {
  17.  
  18. /**
  19. * @param args the command line arguments
  20. */
  21. public static void main(String[] args) {
  22.  
  23. Connection conn = null;
  24. Properties connectionProps = new
  25. Properties();
  26. connectionProps.put("user", "inf127300");
  27. connectionProps.put("password", "inf127300");
  28. try {
  29. conn = DriverManager.getConnection("jdbc:oracle:thin:@//admlab2.cs.put.poznan.pl:1521/dblab02_students.cs.put.poznan.pl", connectionProps);
  30. System.out.println("Połączono z bazą danych");
  31. }
  32. catch
  33. (SQLException ex) {
  34. Logger.getLogger(Lab_JDBC.class.getName()).log(Level.SEVERE,
  35. "nie udało się połączyć z bazą danych", ex);
  36. System.exit(-1);
  37. }
  38. Statement stmt =
  39. null
  40. ;
  41. ResultSet rs =
  42. null
  43. ;
  44. try
  45. {
  46. stmt = conn.createStatement();
  47. rs = stmt.executeQuery(
  48. "select id_prac, nazwisko, placa_pod " +
  49. "from pracownicy"
  50. );
  51. while
  52. (rs.next()) {
  53. System.out.println(rs.getInt(1) +
  54. " "
  55. + rs.getString(2) +
  56. " "
  57. +
  58. rs.getFloat(3));
  59. }
  60. }
  61. catch
  62. (SQLException ex) {
  63. System.out.println(
  64. "Bład wykonania polecenia"
  65. + ex.toString());
  66. }
  67. finally
  68. {
  69. if
  70. (rs !=
  71. null
  72. ) {
  73. try
  74. {
  75. rs.close();
  76. }
  77. catch
  78. (SQLException e) {
  79. /* kod obsługi */
  80. }
  81. }
  82. if
  83. (stmt !=
  84. null
  85. ) {
  86. try
  87. {
  88. stmt.close();
  89. }
  90. catch
  91. (SQLException e) {
  92. /* kod obsługi */
  93. }
  94. }
  95. }
  96. try {
  97. conn.close();
  98. } catch (SQLException ex) {
  99. Logger.getLogger(Lab_JDBC.class.getName()).log(Level.SEVERE, null, ex);
  100. }
  101.  
  102. System.out.println("Rozłączono");
  103. }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement