Advertisement
Guest User

Untitled

a guest
Apr 15th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.sql.ResultSet;
  5. import java.sql.Statement;
  6.  
  7. public class Main {
  8.  
  9.  
  10.     private final String url = "jdbc:postgresql://localhost/test2";
  11.     private final String user = "postgres";
  12.     private final String password = "admin";
  13.  
  14.     /**
  15.      * Connect to the PostgreSQL database
  16.      *
  17.      * @return a Connection object
  18.      */
  19.     public Connection connect() {
  20.         Connection conn = null;
  21.         try {
  22.             conn = DriverManager.getConnection(url, user, password);
  23.             System.out.println("Connected to the PostgreSQL server successfully.");
  24.         } catch (SQLException e) {
  25.             System.out.println(e.getMessage());
  26.         }
  27.  
  28.         return conn;
  29.     }
  30.  
  31.     public void getActors() {
  32.  
  33.         String SQL = "SELECT * from names";
  34.  
  35.         try (Connection conn = connect();
  36.              Statement stmt = conn.createStatement();
  37.              ResultSet rs = stmt.executeQuery(SQL)) {
  38.             // display actor information
  39.             displayActor(rs);
  40.         } catch (SQLException ex) {
  41.             System.out.println(ex.getMessage());
  42.         }
  43.     }
  44.  
  45.     private void displayActor(ResultSet rs) throws SQLException {
  46.         while (rs.next()) {
  47.             System.out.println(rs.getString("name") );
  48.  
  49.         }
  50.     }
  51.     /**
  52.      * @param args the command line arguments
  53.      */
  54.     public static void main(String[] args) {
  55.         Main main =new Main();
  56.         main.getActors();
  57.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement