Commit a6ac07cb by Patryk Czarnik

Pierwszy odczyt bazy danych

parent 80c0f21c
/target/
/.settings/
/.classpath
/.project
/*.iml
/.idea/
/hr.db
/employees.xml
/eksport.csv
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>PC22-BazyDanych</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<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.3</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package na_zajeciach.podstawy;
import java.math.BigDecimal;
import java.sql.*;
// JDBC Java Database Connectivity
public class OdczytajOsoby {
public static void main(String[] args) {
try(Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/alx",
"alx", "abc123")) {
System.out.println("Mam połączenie: " + c);
try(PreparedStatement stmt = c.prepareStatement("SELECT * FROM osoby");
ResultSet rs = stmt.executeQuery()) {
while(rs.next()) {
int id = rs.getInt(1); // albo rs.getInt("id");
String imie = rs.getString("imie");
String nazwisko = rs.getString("nazwisko");
Date dataUr = rs.getDate("data_ur");
BigDecimal pensja = rs.getBigDecimal("pensja");
System.out.printf("%d: %s %s, ur. %s, pensja: %s%n", id, imie, nazwisko, dataUr, pensja);
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
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