Commit e4f43ad9 by Patryk Czarnik

Projekt 22 BazyMaven

parent a8bf750e
/target/
/.settings/
/.classpath
/.project
/*.iml
/.idea/
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pl.alx.kjava</groupId>
<artifactId>PC22-BazyDanychMaven</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.6.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
package bazy.na_zywo;
import java.sql.*;
public class Odczyt1 {
// JDBC = Java Database Connectivity
public static void main(String[] args) {
try {
Connection c = DriverManager.getConnection("jdbc:postgresql://localhost/postgres",
"postgres", "abc123");
System.out.println("Mam połączenie: " + c);
PreparedStatement stmt = c.prepareStatement("SELECT * FROM osoby");
ResultSet rs = stmt.executeQuery();
while(rs.next()) {
System.out.println(rs.getString(2) + " " + rs.getString(3));
}
c.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
package bazy.na_zywo;
import java.sql.*;
public class Odczyt2 {
public static void main(String[] args) {
String url = "jdbc:postgresql://localhost:5432/postgres";
try (Connection c = DriverManager.getConnection(url, "postgres", "abc123");
PreparedStatement stmt = c.prepareStatement("SELECT * FROM osoby");
ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
System.out.printf("Osoba nr %d to jest %s %s, ur. %s, zarabia %s%n",
rs.getInt("id"),
rs.getString("imie"),
rs.getString("nazwisko"),
rs.getString("data_urodzenia"),
rs.getBigDecimal("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