Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 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 oraclejdbc;
  7.  
  8. import java.io.DataInputStream;
  9. import java.io.IOException;
  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.logging.Level;
  16. import java.util.logging.Logger;
  17.  
  18. /**
  19. *
  20. * @author student
  21. */
  22. public class OracleJDBC {
  23.  
  24. /**
  25. * @param args the command line arguments
  26. */
  27. public static void main(String[] args) throws IOException {
  28. // TODO code application logic here
  29. System.out.println("-------Polaczenie z baza danych Oracle-------");
  30. try {
  31. Class.forName("oracle.jdbc.driver.OracleDriver");
  32. } catch (ClassNotFoundException e) {
  33. System.out.println("Blad sciezki sterwonika Oracle JDBC?");
  34. e.printStackTrace();
  35. return;
  36. }
  37.  
  38. System.out.println("Rejsetracja sterownika Oracle JDBC!");
  39. Connection connection = null;
  40. try {
  41.  
  42. DataInputStream d = new DataInputStream(System.in);
  43. System.out.println("Podaj nazwe uzytkownika");
  44. String user = d.readLine();
  45. System.out.println("Podaj haslo");
  46. String pass = d.readLine();
  47.  
  48. connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.56.101:1521:xe", user, pass);
  49. Statement stmt = connection.createStatement();
  50.  
  51. System.out.println("Wybierz operacje:");
  52. System.out.println("a) Pracownik - ID:");
  53. System.out.println("b) Pracownik na stanowisku:");
  54. System.out.println("c) Lista 3");
  55. String zmienna = d.readLine();
  56. switch(zmienna){
  57. case "a":
  58. System.out.println("Podaj ID pracownika");
  59. String ID = d.readLine();
  60. ResultSet rs = stmt.executeQuery("select e.first_name, e.last_name, d.department_name from employees e, "
  61. + "departments d where d.department_id=e.department_id AND e.employee_id="+ID);
  62. while(rs.next()){
  63. System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
  64. }
  65. break;
  66. case "b":
  67. ResultSet rs2 = stmt.executeQuery("select e.first_name, e.last_name, e.email, e.phone_number, e.hire_date,"
  68. + "e.salary, j.job_title from employees e, jobs j where e.job_id=j.job_id AND e.job_id = 'IT_PROG'");
  69. while(rs2.next()){
  70. System.out.println(rs2.getString(1)+" "+rs2.getString(2)+" "+rs2.getString(3)+" "+rs2.getString(4)+" "+rs2.getDate(5)+" "+rs2.getInt(6));
  71. }
  72. break;
  73.  
  74. case "c":
  75. System.out.println("Podaj id wykluczonego departamentu");
  76. String dep_id = d.readLine();
  77. ResultSet rs3 = stmt.executeQuery("select e.first_name, e.last_name, e.salary from employees e where e.salary>5000 AND e.department_id!="+dep_id);
  78. while(rs3.next()){
  79. System.out.println(rs3.getString(1)+" "+rs3.getString(2)+" "+rs3.getInt(3));
  80. }
  81. break;
  82. default: System.out.println("Wybrales zla opcje");
  83. }
  84.  
  85.  
  86. } catch (SQLException e) {
  87. System.out.println("Blad polaczenia. Sprawdz konsole");
  88. e.printStackTrace();
  89. return;
  90. }
  91. if (connection != null) {
  92. System.out.println("Polaczyles sie z baza danych oracle!");
  93. } else {
  94. System.out.println("Blad polaczenia!");
  95. }
  96.  
  97. try {
  98. connection.close();
  99. System.out.println("Połaczenie z baza zakończone");
  100. } catch (SQLException e) {
  101. System.out.println("Blad zakonczenia połaczenia");
  102. e.printStackTrace();
  103. return;
  104. }
  105.  
  106. }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement