feat(postgres_java): added class JavaPostgreSqlVersion

This commit is contained in:
dancingCycle 2021-12-09 10:22:54 -05:00
parent 977ab50dcd
commit aa535c0e3f
3 changed files with 67 additions and 1 deletions

View File

@ -1,5 +1,20 @@
# 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

@ -30,6 +30,18 @@
<build>
<plugins>
<!--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.-->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>de.swingbe.postgres_java.JavaPostgreSqlVersion
</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
@ -72,7 +84,8 @@
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>de.swingbe.postgres_java.Main
<Main-Class>
de.swingbe.postgres_java.Main
</Main-Class>
</manifestEntries>
</transformer>
@ -83,4 +96,13 @@
</plugin>
</plugins>
</build>
<dependencies>
<!--Add the following dependency to include PostgreSQL Java driver.-->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.1</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,29 @@
package de.swingbe.postgres_java;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class JavaPostgreSqlVersion {
public static void main(String[] args) {
String url = "jdbc:postgresql://localhost:5432/testdb";
String user = "usr";
String password = "#password";
try (Connection con = DriverManager.getConnection(url, user, password);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT VERSION()")) {
if (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(JavaPostgreSqlVersion.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
}
}
}