Advertisement
BenjaminWade

jdbc

May 23rd, 2022
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class App {
  4. private Connection c;
  5.  
  6. public App() {
  7. try {
  8. Class.forName("org.postgresql.Driver");
  9. System.out.println("carregou");
  10. } catch (ClassNotFoundException ex) {
  11. ex.printStackTrace();
  12. }
  13. }
  14.  
  15. public void setConnection() throws SQLException {
  16. String host = "https://database-1.cceebyyef1n1.us-east-1.rds.amazonaws.com/";
  17. String db = "bd_aula";
  18. String url = "jdbc:postgresql://" + host + "/" + db;
  19. String user = "aluno";
  20. String senha = "aluno";
  21. c = DriverManager.getConnection(url, user, senha);
  22. }
  23.  
  24. public void executarConsulta() throws SQLException {
  25. Statement st = c.createStatement();
  26. String sql = "SELECT * FROM universidade.professor";
  27. ResultSet rs = st.executeQuery(sql);
  28. while (rs.next()) {
  29. System.out.println(rs.getRow() + " ");
  30. String col1 = rs.getString(1);
  31. String col2 = rs.getString(2);
  32. String data = rs.getString("data_nascimento");
  33. System.out.println(col1 + "\t" + col2 + "\t" + data);
  34.  
  35. }
  36. }
  37.  
  38. public static void main(String[] args) {
  39. App t = new App();
  40. try {
  41. t.setConnection();
  42. System.out.println("conexão ok...");
  43. t.executarConsulta();
  44. } catch (SQLException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement