Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;
  5. import java.sql.ResultSet;
  6. import java.lang.String;
  7. import java.sql.*;
  8.  
  9. public class Main {
  10.  
  11.  
  12. private final String url = "jdbc:postgresql://localhost/clinica?currentSchema=clinica";
  13. private final String user = "postgres";
  14. private final String password = "@Projeto";
  15.  
  16.  
  17. public static void primeiraConsulta(Connection conexao) throws SQLException{
  18. String sql = "SELECT (pnome || ' ' || snome) AS nome, cpf, data_nascimento, telefone_residencial " +
  19. "FROM cliente WHERE data_nascimento > '1980-01-01'";
  20.  
  21. PreparedStatement comando = conexao.prepareStatement(sql);
  22.  
  23. System.out.println("Executando consulta: " + sql);
  24. // comando.setString(1, DCOMP);
  25.  
  26. ResultSet resultado = comando.executeQuery();
  27.  
  28. System.out.printf("Código | Cpf | Data de Nascimento | Telefone \n");
  29. while(resultado.next()){
  30.  
  31. //Recupera valor referente ao nome da coluna
  32. String codigo = resultado.getString("nome");
  33. //Recupera o índice do campo referente ao campo nome
  34. // String nome = resultado.getString(2);
  35. long cpf = resultado.getLong("cpf");
  36. Date data_nascimento = resultado.getDate("data_nascimento");
  37. String telefone = resultado.getString("telefone_residencial");
  38. // Double orcamento = resultado.getDouble("orcamento")
  39. System.out.printf("%s10 | %s10 | %s10 | %s10 \n",codigo, cpf, data_nascimento, telefone);
  40.  
  41. }
  42.  
  43.  
  44. }
  45.  
  46. public Connection connect() {
  47. Connection conn = null;
  48. try {
  49. conn = DriverManager.getConnection(url, user, password);
  50. System.out.println("Connected to the PostgreSQL server successfully.");
  51. } catch (SQLException e) {
  52. System.out.println(e.getMessage());
  53. }
  54.  
  55. return conn;
  56. }
  57.  
  58. public static void main(String[] args) {
  59. Main app = new Main();
  60. Connection conn = app.connect();
  61.  
  62. try {
  63. primeiraConsulta(conn);
  64. } catch (SQLException e) {
  65. e.printStackTrace();
  66. }
  67.  
  68. // String sql = "SELECT * FROM departamento";
  69. //
  70. // try (Statement stmt = conn.createStatement()) {
  71. // ResultSet rs = stmt.executeQuery(sql);
  72. //
  73. // while(rs.next()){
  74. // //Recupera valor referente ao nome da coluna
  75. // String codigo = rs.getString("cod_depto");
  76. // //Recupera o índice do campo referente ao campo nome
  77. // String nome = rs.getString(2);
  78. // String chefe = rs.getString("chefe");
  79. // Double orcamento = rs.getDouble("orcamento");
  80. // System.out.printf("Código %s: %s - %s | Salário: %f \n",codigo, nome, chefe, orcamento);
  81. // }
  82. //
  83. //
  84. // } catch (SQLException e ) {
  85. // System.out.println(e.getMessage());
  86. // }
  87.  
  88.  
  89. System.out.println("Hello World!");
  90. }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement