feat(postgres_java): added class JavaPostgreSqlRetrieve.java
This commit is contained in:
parent
589da9cd4b
commit
d22cd25e37
|
@ -38,7 +38,7 @@
|
|||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>1.6.0</version>
|
||||
<configuration>
|
||||
<mainClass>de.swingbe.postgres_java.JavaPostgreSqlPrepared
|
||||
<mainClass>de.swingbe.postgres_java.JavaPostgreSqlRetrieve
|
||||
</mainClass>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
@ -85,7 +85,7 @@
|
|||
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<manifestEntries>
|
||||
<Main-Class>
|
||||
de.swingbe.postgres_java.Main
|
||||
de.swingbe.postgres_java.JavaPostgreSqlRetrieve
|
||||
</Main-Class>
|
||||
</manifestEntries>
|
||||
</transformer>
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
package de.swingbe.postgres_java;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class JavaPostgreSqlRetrieve {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
String url = "jdbc:postgresql://localhost:5432/testdb";
|
||||
String user = "usr";
|
||||
String password = "#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(JavaPostgreSqlRetrieve.class.getName());
|
||||
lgr.log(Level.SEVERE, ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue