Advertisement
Guest User

Untitled

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