Commit 00274ce9 by Patryk Czarnik

pierwsze przykłady JDBC

parent 7d0ac1a1
......@@ -11,4 +11,12 @@
<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>
</dependency>
</dependencies>
</project>
package zajecia;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class OdczytajEmployees {
public static void main(String[] args) {
String url = "jdbc:postgresql://localhost:5432/hr";
try(Connection c = DriverManager.getConnection(url, "alx", "abc123")) {
System.out.println("mam połączenie: " + c);
try(PreparedStatement stmt = c.prepareStatement("SELECT * FROM employees ORDER BY employee_id");
ResultSet rs = stmt.executeQuery()) {
System.out.println("mam ResultSet: " + rs);
while(rs.next()) {
System.out.printf("%s %s (%s), pensja %s\n",
rs.getString("first_name"), rs.getString("last_name"),
rs.getString("job_id"), rs.getBigDecimal("salary"));
}
}
} catch (SQLException e) {
e.printStackTrace();
};
}
}
package zajecia;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class OdczytajOsoby {
public static void main(String[] args) {
String url = "jdbc:postgresql://localhost:5432/test";
try(Connection c = DriverManager.getConnection(url, "alx", "abc123")) {
System.out.println("mam połączenie: " + c);
try(PreparedStatement stmt = c.prepareStatement("SELECT * FROM osoby");
ResultSet rs = stmt.executeQuery()) {
System.out.println("mam ResultSet: " + rs);
while(rs.next()) {
System.out.printf("%s %s, ur. %s, pensja %s\n",
rs.getString("imie"), rs.getString("nazwisko"),
rs.getDate("data_ur"), rs.getBigDecimal("zarobki"));
}
}
} catch (SQLException e) {
e.printStackTrace();
};
}
}
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