Commit dfd7e0b0 by Patryk Czarnik

SQLite

parent f3f8474c
...@@ -6,6 +6,7 @@ import java.sql.*; ...@@ -6,6 +6,7 @@ import java.sql.*;
public class PolaczPostgres1 { public class PolaczPostgres1 {
public static void main(String[] args) throws SQLException { public static void main(String[] args) throws SQLException {
Connection c = DriverManager.getConnection("jdbc:postgresql://localhost/hr", "kurs", "abc123"); Connection c = DriverManager.getConnection("jdbc:postgresql://localhost/hr", "kurs", "abc123");
System.out.println("Mam połączenie: " + c); System.out.println("Mam połączenie: " + c);
...@@ -25,11 +26,10 @@ public class PolaczPostgres1 { ...@@ -25,11 +26,10 @@ public class PolaczPostgres1 {
System.out.printf("%d: %s %s (%s) zarabia %s\n", id, firstName, lastName, job, salary); System.out.printf("%d: %s %s (%s) zarabia %s\n", id, firstName, lastName, job, salary);
// dobrą praktyką jest zamykanie zasobów ResultSet i Statement - bo serwer SQL może trzymać dla nas jakieś zasoby
rs.close();
stmt.close();
c.close();
} }
// dobrą praktyką jest zamykanie zasobów ResultSet i Statement - bo serwer SQL może trzymać dla nas jakieś zasoby
rs.close();
stmt.close();
c.close();
} }
} }
package bazy.a_poczatek;
import java.math.BigDecimal;
import java.sql.*;
public class PolaczSQLite {
public static void main(String[] args) {
try (Connection c = DriverManager.getConnection("jdbc:sqlite:hr.db");
PreparedStatement stmt = c.prepareStatement("SELECT * FROM employees");
ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
// odczyt wg numerów kolumn, numeracja od 1
int id = rs.getInt(1);
String firstName = rs.getString(2);
String lastName = rs.getString(3);
// odczyt wg nazw kolumn:
String job = rs.getString("job_id");
BigDecimal salary = rs.getBigDecimal("salary");
System.out.printf("%d: %s %s (%s) zarabia %s\n", id, firstName, lastName, job, salary);
}
} 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