Created
November 26, 2020 18:47
-
-
Save ahndmal/a23b6628ac320ef27b5b3cd99759757d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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