Advertisement
Guest User

Untitled

a guest
May 10th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. package database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9.  
  10. public class DataGet {
  11.  
  12. static final String DB_URL = "jdbc:postgresql://localhost:5432/javaexample";
  13.  
  14. // Database credentials
  15. static final String USER = "postgres";
  16. static final String PASS = "SQLMaster5";
  17.  
  18.  
  19. public static void main(String[] args) {
  20. // TODO Auto-generated method stub
  21. try {
  22. Connection con = DriverManager.getConnection(DB_URL, USER, PASS);
  23. if (con != null) {
  24. System.out.println("Heureka");
  25. con.createStatement();
  26.  
  27. Statement stmt = null;
  28. String query = "select * from public.users";
  29. try {
  30. stmt = con.createStatement();
  31. ResultSet rs = stmt.executeQuery(query);
  32. while (rs.next()) {
  33. String lastname = rs.getString("password");
  34.  
  35. System.out.println(lastname);
  36. }
  37. } catch (SQLException e ) {
  38. System.out.println(e.getMessage());
  39. } finally {
  40. if (stmt != null) { stmt.close(); }
  41. }
  42.  
  43.  
  44. }
  45.  
  46.  
  47. } catch (SQLException e) {
  48. // TODO Auto-generated catch block
  49. e.printStackTrace();
  50. }
  51.  
  52. }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement