feat(postgres_java): added class JavaPostgreSqlProperties.java
This commit is contained in:
parent
d22cd25e37
commit
15866d920d
|
@ -38,7 +38,7 @@
|
||||||
<artifactId>exec-maven-plugin</artifactId>
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
<version>1.6.0</version>
|
<version>1.6.0</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<mainClass>de.swingbe.postgres_java.JavaPostgreSqlRetrieve
|
<mainClass>de.swingbe.postgres_java.JavaPostgreSqlProperties
|
||||||
</mainClass>
|
</mainClass>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
@ -85,7 +85,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.JavaPostgreSqlRetrieve
|
de.swingbe.postgres_java.JavaPostgreSqlProperties
|
||||||
</Main-Class>
|
</Main-Class>
|
||||||
</manifestEntries>
|
</manifestEntries>
|
||||||
</transformer>
|
</transformer>
|
||||||
|
|
|
@ -0,0 +1,61 @@
|
||||||
|
package de.swingbe.postgres_java;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class JavaPostgreSqlProperties {
|
||||||
|
|
||||||
|
public static Properties readProperties() {
|
||||||
|
|
||||||
|
Properties props = new Properties();
|
||||||
|
Path path = Paths.get("src/main/resources/database.properties");
|
||||||
|
|
||||||
|
try {
|
||||||
|
BufferedReader br = Files.newBufferedReader(path, StandardCharsets.UTF_8);
|
||||||
|
props.load(br);
|
||||||
|
} catch (IOException e) {
|
||||||
|
Logger.getLogger(JavaPostgreSqlProperties.class.getName()).log(
|
||||||
|
Level.SEVERE, null, e);
|
||||||
|
}
|
||||||
|
return props;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
Properties props = readProperties();
|
||||||
|
|
||||||
|
String url = props.getProperty("db.url");
|
||||||
|
String user = props.getProperty("db.user");
|
||||||
|
String password = props.getProperty("db.password");
|
||||||
|
|
||||||
|
//get all columns from a table
|
||||||
|
String query = "SELECT * FROM authors";
|
||||||
|
|
||||||
|
//create prepared statement using placeholders instead of directly writing values
|
||||||
|
try (Connection con = DriverManager.getConnection(url, user, password);
|
||||||
|
PreparedStatement pst = con.prepareStatement(query);
|
||||||
|
ResultSet rs = pst.executeQuery()) {
|
||||||
|
|
||||||
|
//advance cursor to the next record
|
||||||
|
//return false if there are no more records in the result set
|
||||||
|
while (rs.next()) {
|
||||||
|
System.out.print(rs.getInt(1));
|
||||||
|
System.out.print(" | ");
|
||||||
|
System.out.println(rs.getString(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
|
||||||
|
Logger lgr = Logger.getLogger(JavaPostgreSqlProperties.class.getName());
|
||||||
|
lgr.log(Level.SEVERE, ex.getMessage(), ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
db.url=jdbc:postgresql://localhost:5432/testdb
|
||||||
|
db.user=usr
|
||||||
|
db.passwd=password
|
Loading…
Reference in New Issue