42 lines
1.6 KiB
Java
42 lines
1.6 KiB
Java
|
|
/*
|
||
|
|
Compile:
|
||
|
|
javac PostgreSqlExample.java
|
||
|
|
|
||
|
|
Run:
|
||
|
|
java -cp postgresql-42.5.4.jar;. PostgreSqlExample
|
||
|
|
|
||
|
|
*/
|
||
|
|
|
||
|
|
import java.sql.Connection;
|
||
|
|
import java.sql.DriverManager;
|
||
|
|
import java.sql.ResultSet;
|
||
|
|
import java.sql.SQLException;
|
||
|
|
import java.sql.Statement;
|
||
|
|
|
||
|
|
public class PostgreSqlExample {
|
||
|
|
public static void main(String[] args) {
|
||
|
|
try (Connection connection = DriverManager.getConnection("jdbc:postgresql://192.168.10.138:5432/example", "postgres", "postgres")) {
|
||
|
|
System.out.println("Java JDBC PostgreSQL Example");
|
||
|
|
|
||
|
|
System.out.println("Connected to PostgreSQL database!");
|
||
|
|
Statement statement = connection.createStatement();
|
||
|
|
|
||
|
|
// For huge resultset the following two lines do the trick:
|
||
|
|
statement.setFetchSize(100);
|
||
|
|
connection.setAutoCommit(false);
|
||
|
|
System.out.println("Reading records.");
|
||
|
|
|
||
|
|
//ResultSet resultSet = statement.executeQuery("SELECT * FROM test1 a, test1 b, test1 c, test1 d, test1 e, test1 f, test1 g, test1 h, test1 i, test1 j limit (10)");
|
||
|
|
// Produce HUGE resultest
|
||
|
|
ResultSet resultSet = statement.executeQuery("SELECT * FROM test1 a, test1 b, test1 c, test1 d, test1 e, test1 f, test1 g, test1 h, test1 i, test1 j");
|
||
|
|
while (resultSet.next()) {
|
||
|
|
System.out.print(resultSet.getInt(1));
|
||
|
|
System.out.println("-"+ resultSet.getString(2));
|
||
|
|
}
|
||
|
|
|
||
|
|
} catch (SQLException e) {
|
||
|
|
System.out.println("Connection failure.");
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|