Skip to content

Instantly share code, notes, and snippets.

@ahndmal
Created November 26, 2020 18:47
Show Gist options
  • Save ahndmal/a23b6628ac320ef27b5b3cd99759757d to your computer and use it in GitHub Desktop.
Save ahndmal/a23b6628ac320ef27b5b3cd99759757d to your computer and use it in GitHub Desktop.
public static Connection getConnection() throws SQLException, ClassNotFoundException {
Class.forName("org.postgresql.Driver");
return DriverManager.getConnection(PropertiesConfig.getProperty(PropertiesConfig.DB_URL),
PropertiesConfig.getProperty(PropertiesConfig.DB_LOGIN),
PropertiesConfig.getProperty(PropertiesConfig.DB_PASSWORD));
};
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("org.postgresql.Driver");
// Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost/blogs-2", "user", "password");
Connection connection = getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("select * from blogs");
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
System.out.println(resultSet.getInt("blog_id"));
System.out.println(resultSet.getString("body"));
System.out.println(resultSet.getString("title"));
System.out.println(resultSet.getTimestamp("created_at"));
}
connection.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment