Commit c59e4e5c by Patryk Czarnik

Połączenie z HR

parent 37359109
package zajecia;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class OdczytajEmployees1 {
public static void main(String[] args) {
String url = "jdbc:postgresql://localhost/hr";
try {
Connection c = DriverManager.getConnection(url, "kurs", "abc123");
System.out.println("Udało się połączyć. c = " + c);
Statement stmt = c.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM employees");
while(rs.next()) {
String imie = rs.getString("first_name");
String nazwisko = rs.getString("last_name");
String job = rs.getString("job_id");
BigDecimal salary = rs.getBigDecimal("salary");
System.out.printf("%s %s (%s) zarabia %s\n", imie, nazwisko, job, salary);
}
rs.close();
stmt.close();
c.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package zajecia;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class OdczytajEmployees2 {
public static void main(String[] args) {
String url = "jdbc:postgresql://localhost/hr";
try(Connection c = DriverManager.getConnection(url, "kurs", "abc123");
PreparedStatement stmt = c.prepareStatement("SELECT * FROM employees");
ResultSet rs = stmt.executeQuery())
{
while(rs.next()) {
String imie = rs.getString("first_name");
String nazwisko = rs.getString("last_name");
String job = rs.getString("job_id");
BigDecimal salary = rs.getBigDecimal("salary");
System.out.printf("%s %s (%s) zarabia %s\n", imie, nazwisko, 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