feat(postgres_java): added class JavaPostgreSqlVersion
This commit is contained in:
parent
977ab50dcd
commit
aa535c0e3f
|
@ -1,5 +1,20 @@
|
||||||
# Overview
|
# Overview
|
||||||
This project shows how Postgres and Java interact.
|
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
|
# Links
|
||||||
[source](https://zetcode.com/java/postgresql/)
|
[source](https://zetcode.com/java/postgresql/)
|
||||||
|
|
|
@ -30,6 +30,18 @@
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<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>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
@ -72,7 +84,8 @@
|
||||||
<transformer
|
<transformer
|
||||||
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
<manifestEntries>
|
<manifestEntries>
|
||||||
<Main-Class>de.swingbe.postgres_java.Main
|
<Main-Class>
|
||||||
|
de.swingbe.postgres_java.Main
|
||||||
</Main-Class>
|
</Main-Class>
|
||||||
</manifestEntries>
|
</manifestEntries>
|
||||||
</transformer>
|
</transformer>
|
||||||
|
@ -83,4 +96,13 @@
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</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>
|
</project>
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue