Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.sql.*;
- public class Lab1 {
- public static void main(String[] args) {
- Connection con;
- try{
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- con = DriverManager.getConnection("jdbc:sqlserver://"+"153.19.7.13:1401;databaseName=zgrabowska;"+
- "user=zgrabowska;password=238180;");
- System.out.println("Połączono z bazą danych");
- Statement statement = con.createStatement();
- exercise1(statement);
- statement = con.createStatement();
- exercise2(statement);
- statement = con.createStatement();
- exercise3(statement);
- statement = con.createStatement();
- exercise4(statement);
- con.close();
- }
- catch(SQLException error_polaczenie) {
- System.out.println("Błąd połączenia z bazą danych");}
- catch(ClassNotFoundException error_sterownik) {
- System.out.println("Brak sterownika");}
- }
- private static void exercise1(Statement statement) throws SQLException {
- String sql = "select * from klient k join adres a on k.id_klient = a.id_klient;";
- ResultSet wynik = statement.executeQuery(sql);
- while(wynik.next()){
- System.out.print(wynik.getInt(1) + "|");
- System.out.print(wynik.getString(2) + "|");
- System.out.print(wynik.getString(3) + "|");
- System.out.print(wynik.getDate(4) + "|");
- System.out.print(wynik.getInt(5) + "|");
- System.out.print(wynik.getInt(6) + "|");
- System.out.print(wynik.getString(7) + "|");
- System.out.println(wynik.getString(8));
- }
- System.out.println(" ");
- }
- private static void exercise2(Statement statement) throws SQLException {
- 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;";
- ResultSet wynik = statement.executeQuery(sql);
- while(wynik.next()){
- System.out.print(wynik.getString(1) + "|");
- System.out.print(wynik.getInt(2) + "|");
- System.out.println(wynik.getString(3));
- System.out.println();
- }
- }
- private static void exercise3(Statement statement) throws SQLException{
- String sql = "select top 1 nazwisko from klient order by data_ur asc";
- ResultSet wynik = statement.executeQuery(sql);
- while(wynik.next()){
- System.out.println(wynik.getString(1));
- System.out.println();
- }
- }
- private static void exercise4(Statement statement) throws SQLException {
- String sql = "select miasto, count(id_klient) as ilosc_mieszkancow from adres group by miasto";
- ResultSet wynik = statement.executeQuery(sql);
- while(wynik.next()){
- System.out.print(wynik.getString(1) + "|");
- System.out.println(wynik.getInt(2));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement