Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.sql.ResultSet;
  5. import java.sql.Statement;
  6.  
  7.  
  8. import java.sql.*;
  9.  
  10. public class Lab1 {
  11.  
  12. public static void main(String[] args) {
  13.  
  14. Connection con;
  15. try{
  16. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  17. con = DriverManager.getConnection("jdbc:sqlserver://"+"153.19.7.13:1401;databaseName=zgrabowska;"+
  18. "user=zgrabowska;password=238180;");
  19. System.out.println("Połączono z bazą danych");
  20.  
  21. Statement statement = con.createStatement();
  22. exercise1(statement);
  23.  
  24. statement = con.createStatement();
  25. exercise2(statement);
  26.  
  27. statement = con.createStatement();
  28. exercise3(statement);
  29.  
  30. statement = con.createStatement();
  31. exercise4(statement);
  32.  
  33.  
  34. con.close();
  35. }
  36. catch(SQLException error_polaczenie) {
  37. System.out.println("Błąd połączenia z bazą danych");}
  38. catch(ClassNotFoundException error_sterownik) {
  39. System.out.println("Brak sterownika");}
  40.  
  41. }
  42.  
  43. private static void exercise1(Statement statement) throws SQLException {
  44. String sql = "select * from klient k join adres a on k.id_klient = a.id_klient;";
  45. ResultSet wynik = statement.executeQuery(sql);
  46. while(wynik.next()){
  47. System.out.print(wynik.getInt(1) + "|");
  48. System.out.print(wynik.getString(2) + "|");
  49. System.out.print(wynik.getString(3) + "|");
  50. System.out.print(wynik.getDate(4) + "|");
  51. System.out.print(wynik.getInt(5) + "|");
  52. System.out.print(wynik.getInt(6) + "|");
  53. System.out.print(wynik.getString(7) + "|");
  54. System.out.println(wynik.getString(8));
  55. }
  56. System.out.println(" ");
  57. }
  58.  
  59. private static void exercise2(Statement statement) throws SQLException {
  60. String sql = "select k.nazwisko, datediff(year, k.data_ur, getdate()) as wiek, a.miasto from klient k join adres a on k.id_klient = a.id_klient;";
  61. ResultSet wynik = statement.executeQuery(sql);
  62. while(wynik.next()){
  63. System.out.print(wynik.getString(1) + "|");
  64. System.out.print(wynik.getInt(2) + "|");
  65. System.out.println(wynik.getString(3));
  66. System.out.println();
  67. }
  68. }
  69.  
  70. private static void exercise3(Statement statement) throws SQLException{
  71. String sql = "select top 1 nazwisko from klient order by data_ur asc";
  72. ResultSet wynik = statement.executeQuery(sql);
  73. while(wynik.next()){
  74. System.out.println(wynik.getString(1));
  75. System.out.println();
  76. }
  77. }
  78.  
  79. private static void exercise4(Statement statement) throws SQLException {
  80. String sql = "select miasto, count(id_klient) as ilosc_mieszkancow from adres group by miasto";
  81. ResultSet wynik = statement.executeQuery(sql);
  82. while(wynik.next()){
  83. System.out.print(wynik.getString(1) + "|");
  84. System.out.println(wynik.getInt(2));
  85. }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement