Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.ResultSet;
- import java.sql.Statement;
- public class Main {
- private final String url = "jdbc:postgresql://localhost/test2";
- private final String user = "postgres";
- private final String password = "admin";
- /**
- * Connect to the PostgreSQL database
- *
- * @return a Connection object
- */
- public Connection connect() {
- Connection conn = null;
- try {
- conn = DriverManager.getConnection(url, user, password);
- System.out.println("Connected to the PostgreSQL server successfully.");
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- return conn;
- }
- public void getActors() {
- String SQL = "SELECT * from names";
- try (Connection conn = connect();
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(SQL)) {
- // display actor information
- displayActor(rs);
- } catch (SQLException ex) {
- System.out.println(ex.getMessage());
- }
- }
- private void displayActor(ResultSet rs) throws SQLException {
- while (rs.next()) {
- System.out.println(rs.getString("name") );
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Main main =new Main();
- main.getActors();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement