feat(postgres_java): refactored class JavaPostgreSqlVersion

This commit is contained in:
dancingCycle 2022-04-14 11:09:41 +02:00
parent 2d44f9e343
commit 05d35609fd
3 changed files with 20 additions and 26 deletions

View File

@ -1,20 +1,5 @@
# Overview
This project shows how Postgres and Java interact.
# Notes
The instruction
```/etc/init.d/postgresql status```
is equivalent with
```systemctl status postgresql```
.
obx21
firstairport21
# Links
[source](https://zetcode.com/java/postgresql/)

View File

@ -33,12 +33,13 @@
<!--Set up the following plugin in order to run command line applications from Maven.
Run the app with mvn -q exec:java command.
The q runs mvn in quite mode.-->
<!-- https://mvnrepository.com/artifact/org.codehaus.mojo/exec-maven-plugin -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<version>3.0.0</version>
<configuration>
<mainClass>de.swingbe.postgres_java.JavaPostgreSqlProperties
<mainClass>de.swingbe.postgres_java.JavaPostgreSqlVersion
</mainClass>
</configuration>
</plugin>
@ -85,7 +86,7 @@
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>
de.swingbe.postgres_java.JavaPostgreSqlProperties
de.swingbe.postgres_java.JavaPostgreSqlVersion
</Main-Class>
</manifestEntries>
</transformer>
@ -98,11 +99,13 @@
</build>
<dependencies>
<!--Add the following dependency to include PostgreSQL Java driver.-->
<!--Add the following dependency to include PostgreSQL Java driver.-->
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.1</version>
<version>42.3.3</version>
</dependency>
</dependencies>
</project>

View File

@ -8,13 +8,19 @@ public class JavaPostgreSqlVersion {
public static void main(String[] args) {
String url = "jdbc:postgresql://localhost:5432/testdb";
String user = "usr";
String password = "#password";
//connection URL
String url = "jdbc:postgresql://192.168.178.25:5432/testdb";
String user = "user";
String key = "key";
try (Connection con = DriverManager.getConnection(url, user, password);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT VERSION()")) {
try (
//establish connection
Connection con = DriverManager.getConnection(url, user, key);
//create object for sending SQL statements
Statement st = con.createStatement();
//execute SQL statement
//rs is a table of data returned by a SQL statement
ResultSet rs = st.executeQuery("SELECT VERSION()")) {
while (rs.next()) {
System.out.println(rs.getString(1));