Commit b047b539 by Patryk Czarnik

pierwszy program postgresql

parent 437b5282
......@@ -6,11 +6,21 @@
<groupId>org.example</groupId>
<artifactId>SzkolenieJavaXTB</artifactId>
<version>1.0</version>
<version>1.1-SNAPSHOT</version>
<properties>
<maven.compiler.release>21</maven.compiler.release>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.5</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package bazy.zajecia;
import java.sql.*;
public class OdczytajPostgreSQL1 {
public static void main(String[] args) {
// JDBC - Java Database Connectivity
String url = "jdbc:postgresql://localhost:5432/hr";
try(Connection c = DriverManager.getConnection(url, "alx", "abc123")) {
System.out.println("Udało się połączyć: " + c);
// Gdyby zależność była w zakresie compile, a nie runtime, moglibyśmy skorzystać z klas zawartych w bibliotece...
// org.postgresql.jdbc.PgConnection pgConnection = (org.postgresql.jdbc.PgConnection)c;
// int pid = pgConnection.getBackendPID();
// System.out.println(pid);
try(PreparedStatement stmt = c.prepareStatement("SELECT * FROM employees ORDER BY employee_id");
ResultSet rs = stmt.executeQuery()) {
System.out.println("ResultSet: " + rs);
while(rs.next()) {
System.out.printf("Pracownik nr %d: %s %s (%s), pensja %s%n",
rs.getInt(1),
rs.getString("first_name"),
rs.getString("last_name"),
rs.getString("job_id"),
rs.getBigDecimal("salary")
);
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
......@@ -25,6 +25,11 @@ public class C13_MultiMap {
}
})
.forEach(x -> System.out.print(x + " | "));
System.out.println("=================");
// przykład mapowania 1→1 za pomocą mapMulti (wiadomo, że dałoby się zwykłym map)
Stream.of(1, 3, 0, 5)
.mapMulti((n, c) -> c.accept(n*n))
.forEach(x -> System.out.print(x + " , "));
}
}
......@@ -5,7 +5,6 @@ import java.time.LocalTime;
public class ProstyCzas {
public static void main(String[] args) {
LocalTime czas1 = LocalTime.now(); // ustawia z dokladnoscia do milisekundy
System.out.println(czas1);
......@@ -15,7 +14,12 @@ public class ProstyCzas {
LocalTime czas3 = LocalTime.of(12, 15, 33);
System.out.println(czas3);
// nie ma setterów
// czas3.setHour(14);
// ale można zmienić wartość pola i pobrać NOWY obiekt
czas3 = czas3.withHour(14);
System.out.println(czas3);
// uwaga: nanosekumdy podajemy jako liczbę całkowitą (int)
LocalTime czas4 = LocalTime.of(12, 15, 33, 333222111);
System.out.println(czas4);
......
......@@ -38,7 +38,6 @@ public class Kopiuj1 {
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
kopiuj("pliki/pan_tadeusz.txt", "kopia1.txt");
long koniec = System.currentTimeMillis();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment