feat(postgres_java): refactored class JavaPostgreSqlVersion
This commit is contained in:
parent
2d44f9e343
commit
05d35609fd
|
@ -1,20 +1,5 @@
|
||||||
# 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/)
|
||||||
|
|
|
@ -33,12 +33,13 @@
|
||||||
<!--Set up the following plugin in order to run command line applications from Maven.
|
<!--Set up the following plugin in order to run command line applications from Maven.
|
||||||
Run the app with mvn -q exec:java command.
|
Run the app with mvn -q exec:java command.
|
||||||
The q runs mvn in quite mode.-->
|
The q runs mvn in quite mode.-->
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.codehaus.mojo/exec-maven-plugin -->
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.codehaus.mojo</groupId>
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
<artifactId>exec-maven-plugin</artifactId>
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
<version>1.6.0</version>
|
<version>3.0.0</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<mainClass>de.swingbe.postgres_java.JavaPostgreSqlProperties
|
<mainClass>de.swingbe.postgres_java.JavaPostgreSqlVersion
|
||||||
</mainClass>
|
</mainClass>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
@ -85,7 +86,7 @@
|
||||||
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
<manifestEntries>
|
<manifestEntries>
|
||||||
<Main-Class>
|
<Main-Class>
|
||||||
de.swingbe.postgres_java.JavaPostgreSqlProperties
|
de.swingbe.postgres_java.JavaPostgreSqlVersion
|
||||||
</Main-Class>
|
</Main-Class>
|
||||||
</manifestEntries>
|
</manifestEntries>
|
||||||
</transformer>
|
</transformer>
|
||||||
|
@ -98,11 +99,13 @@
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<dependencies>
|
<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>
|
<dependency>
|
||||||
<groupId>org.postgresql</groupId>
|
<groupId>org.postgresql</groupId>
|
||||||
<artifactId>postgresql</artifactId>
|
<artifactId>postgresql</artifactId>
|
||||||
<version>42.3.1</version>
|
<version>42.3.3</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -8,13 +8,19 @@ public class JavaPostgreSqlVersion {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
String url = "jdbc:postgresql://localhost:5432/testdb";
|
//connection URL
|
||||||
String user = "usr";
|
String url = "jdbc:postgresql://192.168.178.25:5432/testdb";
|
||||||
String password = "#password";
|
String user = "user";
|
||||||
|
String key = "key";
|
||||||
|
|
||||||
try (Connection con = DriverManager.getConnection(url, user, password);
|
try (
|
||||||
Statement st = con.createStatement();
|
//establish connection
|
||||||
ResultSet rs = st.executeQuery("SELECT VERSION()")) {
|
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()) {
|
while (rs.next()) {
|
||||||
System.out.println(rs.getString(1));
|
System.out.println(rs.getString(1));
|
||||||
|
|
Loading…
Reference in New Issue