Commit 56930a03 by Patryk Czarnik

uzupełnienia JDBC

parent d80d6a45
...@@ -6,3 +6,4 @@ ...@@ -6,3 +6,4 @@
/*.iml /*.iml
/.idea/ /.idea/
/hr.db
...@@ -18,5 +18,10 @@ ...@@ -18,5 +18,10 @@
<artifactId>postgresql</artifactId> <artifactId>postgresql</artifactId>
<version>42.7.5</version> <version>42.7.5</version>
</dependency> </dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.49.1.0</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
...@@ -14,7 +14,8 @@ import java.sql.Statement; ...@@ -14,7 +14,8 @@ import java.sql.Statement;
public class Oracle { public class Oracle {
public static void main(String[] args) { public static void main(String[] args) {
final String url = "jdbc:oracle:thin:@//10.0.15.13:1521/xe"; // final String url = "jdbc:oracle:thin:@//10.0.15.13:1521/xe";
final String url = "jdbc:oracle:thin:@//10.0.15.13:1521/xepdb1";
try(Connection con = DriverManager.getConnection(url, "HR", "abc123")) { try(Connection con = DriverManager.getConnection(url, "HR", "abc123")) {
try(Statement st = con.createStatement()) { try(Statement st = con.createStatement()) {
final String sql = "SELECT * FROM countries"; final String sql = "SELECT * FROM countries";
......
...@@ -25,7 +25,7 @@ public class P07_GenerowaneId { ...@@ -25,7 +25,7 @@ public class P07_GenerowaneId {
String[] polaGenerowane = {"location_id", "state_province"}; String[] polaGenerowane = {"location_id", "state_province"};
String sql = "INSERT INTO locations(street_address, postal_code, city, country_id) VALUES(?, ?, ?, ?)"; String sql = "INSERT INTO locations(street_address, postal_code, city, country_id) VALUES(?, ?, ?, ?)";
try(Connection c = DriverManager.getConnection(Ustawienia.URL, Ustawienia.USER, Ustawienia.PASSWD); try(Connection c = DriverManager.getConnection(Ustawienia.URL, Ustawienia.USER, Ustawienia.PASSWD);
PreparedStatement stmt = c.prepareStatement(sql, polaGenerowane)) { PreparedStatement stmt = c.prepareStatement(sql, polaGenerowane)) {
stmt.setString(1, "Nowa z " + LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:SS"))); stmt.setString(1, "Nowa z " + LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:SS")));
...@@ -36,7 +36,7 @@ public class P07_GenerowaneId { ...@@ -36,7 +36,7 @@ public class P07_GenerowaneId {
int ile = stmt.executeUpdate(); int ile = stmt.executeUpdate();
System.out.println("Wstawiono " + ile + " rekord"); // powinno być 1 System.out.println("Wstawiono " + ile + " rekord"); // powinno być 1
// odcyt wartości wygenerowanych pól // odczyt wartości wygenerowanych pól
try(ResultSet rsGen = stmt.getGeneratedKeys()) { try(ResultSet rsGen = stmt.getGeneratedKeys()) {
if(rsGen.next()) { if(rsGen.next()) {
int id = rsGen.getInt(1); int id = rsGen.getInt(1);
......
...@@ -68,6 +68,11 @@ public class P09_DowolnaKolejnoscResultSet { ...@@ -68,6 +68,11 @@ public class P09_DowolnaKolejnoscResultSet {
System.out.printf("\npoz %3d - nie ma takiej pozycji\n", poz); System.out.printf("\npoz %3d - nie ma takiej pozycji\n", poz);
} }
} }
if(rs.last()) {
System.out.println("\nJestem w ostatnim rekordzie");
int nr = rs.getRow();
System.out.println("Numer pozycji " + nr + ", nazwisko pracownika: " + rs.getString("last_name"));
}
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
......
...@@ -18,7 +18,7 @@ public class P10_Batch { ...@@ -18,7 +18,7 @@ public class P10_Batch {
PreparedStatement stmt = c.prepareStatement(sql)) { PreparedStatement stmt = c.prepareStatement(sql)) {
c.setAutoCommit(false); // wyłączamy automatyczne commitowanie - przechodzimy w tryb traksakcji c.setAutoCommit(false); // wyłączamy automatyczne commitowanie - przechodzimy w tryb traksakcji
System.out.println("start"); System.out.println("start");
long poczatek = System.currentTimeMillis(); long poczatek = System.currentTimeMillis();
......
...@@ -19,6 +19,7 @@ public class P11_Metadane { ...@@ -19,6 +19,7 @@ public class P11_Metadane {
System.out.println(dbMetaData.getDatabaseProductName()); System.out.println(dbMetaData.getDatabaseProductName());
System.out.println(dbMetaData.getDatabaseMajorVersion() System.out.println(dbMetaData.getDatabaseMajorVersion()
+ "." + dbMetaData.getDatabaseMinorVersion()); + "." + dbMetaData.getDatabaseMinorVersion());
System.out.printf("maxStatements: " + dbMetaData.getMaxStatements());
System.out.println(); System.out.println();
try(ResultSet tables = dbMetaData.getTables(null, "public", null, null)) { try(ResultSet tables = dbMetaData.getTables(null, "public", null, null)) {
......
...@@ -16,7 +16,7 @@ public class P14c_Procedura_OdczytWartosciSkalarnej { ...@@ -16,7 +16,7 @@ public class P14c_Procedura_OdczytWartosciSkalarnej {
int nr = sc.nextInt(); int nr = sc.nextInt();
try(Connection c = DriverManager.getConnection(Ustawienia.URL, Ustawienia.USER, Ustawienia.PASSWD); try(Connection c = DriverManager.getConnection(Ustawienia.URL, Ustawienia.USER, Ustawienia.PASSWD);
CallableStatement stmt = c.prepareCall("{?= call nazwisko_szefa(?)}")) { CallableStatement stmt = c.prepareCall("{? = call nazwisko_szefa(?)}")) {
stmt.setInt(2, nr); stmt.setInt(2, nr);
stmt.registerOutParameter(1, Types.VARCHAR); stmt.registerOutParameter(1, Types.VARCHAR);
System.out.println(stmt); System.out.println(stmt);
......
url=jdbc:postgresql://localhost/hr
user=alx
password=abc123
tcpKeepAlive=true
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