Skip to content

Instantly share code, notes, and snippets.

@callicles
Last active July 3, 2017 16:46
Show Gist options
  • Save callicles/8008c95142ffb0960b3d207bde519900 to your computer and use it in GitHub Desktop.
Save callicles/8008c95142ffb0960b3d207bde519900 to your computer and use it in GitHub Desktop.
Comdb2/postgres jdbc
import java.sql.*;
import java.util.*;
public class Hello {
public static void main(String[] args) throws Exception {
Connection conn = null;
Statement stmt = null;
PreparedStatement ps = null;
ResultSet rset = null;
ResultSetMetaData md = null;
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://localhost/pg_demo");
// conn.setAutoCommit(false);
// stmt = conn.createStatement();
// stmt.setFetchSize(1); // <----- Turn on stream ResultSet
// rset = stmt.executeQuery("SELECT * from users;");
rset = conn.getMetaData().getColumns(null, "public", "users", null);
md = rset.getMetaData();
// Close the connection
conn.close();
// Let's say I don't know what's in the database but I want to retrieve its content
// Here there is no exception
ArrayList<ArrayList<Object>> results = new ArrayList<>();
while(rset.next()) {
ArrayList<Object> row = new ArrayList<>();
for (int i = 1; i <= md.getColumnCount(); i++){
row.add(rset.getObject(i));
}
results.add(row);
}
// // Close the connection
// conn.close();
System.out.println(String.format("-> Result Size: %s", results.size()));
// for Comdb2 -> Result Size: 0
// for Postgres -> Result Size: 10
// Print the results correctly for postgres but don't display anything for comdb2 (nothing in the list)
for (int rowIndex = 0; rowIndex < results.size(); rowIndex++){
ArrayList<Object> row = results.get(rowIndex);
for (int columnIndex = 0; columnIndex < row.size(); columnIndex++){
Object value = row.get(columnIndex);
if (value != null)
System.out.println(String.format("(Row %s, Column %s): %s", rowIndex, columnIndex, row.get(columnIndex).toString()));
else
System.out.println(String.format("(Row %s, Column %s): null", rowIndex, columnIndex));
}
}
}
}
import java.sql.*;
import java.util.*;
public class Hello {
public static void main(String[] args) throws Exception {
Connection conn = null;
Statement stmt = null;
PreparedStatement ps = null;
ResultSet rset = null;
ResultSetMetaData md = null;
Class.forName("com.bloomberg.comdb2.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:comdb2://localhost/dauphine");
conn.setAutoCommit(false);
stmt = conn.createStatement();
stmt.setFetchSize(1); // <----- Turn on stream ResultSet
rset = stmt.executeQuery("SELECT * from users;");
// rset = conn.getMetaData().getColumns(null, "public", "users", null);
md = rset.getMetaData();
// // Close the connection
// conn.close();
// Let's say I don't know what's in the database but I want to retrieve its content
ArrayList<ArrayList<Object>> results = new ArrayList<>();
while(rset.next()) {
ArrayList<Object> row = new ArrayList<>();
for (int i = 1; i <= md.getColumnCount(); i++){
row.add(rset.getObject(i));
}
results.add(row);
}
// Close the connection
conn.close();
System.out.println(String.format("-> Result Size: %s", results.size()));
// Result size is valid
// Prints the all the results correctly for both postgres and comdb2
for (int rowIndex = 0; rowIndex < results.size(); rowIndex++){
ArrayList<Object> row = results.get(rowIndex);
for (int columnIndex = 0; columnIndex < row.size(); columnIndex++){
Object value = row.get(columnIndex);
if (value != null)
System.out.println(String.format("(Row %s, Column %s): %s", rowIndex, columnIndex, row.get(columnIndex).toString()));
else
System.out.println(String.format("(Row %s, Column %s): null", rowIndex, columnIndex));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment