/* * TestPostgress.java * * Created on 20 March 2002, 16:28 */ package uk.ac.leeds.ccg.andyt.tests; import java.sql.*; // import javax.sql.*; import org.postgresql.*; //import org.postgresql.core.*; //import org.postgresql.fastpath.*; //import org.postgresql.geometric.*; import org.postgresql.jdbc2.*; //import org.postgresql.largeobject.*; //import org.postgresql.util.*; /** * * @author andyt */ public class TestPostgress { /** Creates a new instance of TestPostgress */ public TestPostgress() { } /** * @param args the command line arguments */ public static void main(String args[]) { String url; // url is either: // jdbc:postgresql:database // jdbc:postgresql://host/database // jdbc:postgresql://host:port/database // where: // host is the host name of the server, which defaults to localhost. // port is the port number the server is listening on, which defaults to the PostgreSQL standard port number (5432). // database is yhe database name. String myLogin; String myPassword; String query1; /* The following was to try the other driver and connection options. url = "jdbc:postgresql://feathers.leeds.ac.uk/stats19"; myLogin = "andyt"; myPassword = "andyt"; query1 = "SELECT * FROM acc92 LIMIT 10;"; // Test java.sql.Connection using org.postgresql.Driver System.out.println("Test using java.sql.Connection and org.postgresql.Driver"); try { // Load driver //Class.forName("java.sql.Driver"); // This driver fails. Class.forName("org.postgresql.Driver"); try { // Establish connection java.sql.Connection con = DriverManager.getConnection(url, myLogin, myPassword); // Test SQL java.sql.Statement s = con.createStatement(); // Open statement java.sql.ResultSet rs = s.executeQuery(query1); java.sql.ResultSetMetaData rsmd = rs.getMetaData(); // Print some results set meta data int cols = rsmd.getColumnCount(); System.out.println("Column Count " + cols); System.out.println("Did not find a way of counting rows!"); for (int i = 1; i <= cols; i ++) { // System.out.print(rsmd.getColumnName(i) + ","); } System.out.println(rsmd.getColumnName(cols)); // Print results set while (rs.next()) { for (int i = 1; i <= cols; i ++) { // System.out.print(rs.getString(i) + ","); } System.out.println(rs.getString(cols)); } s.close(); // Close statement // Close connection con.close(); } catch (java.sql.SQLException e) {System.out.println(e);} } catch (ClassNotFoundException e) {System.out.println(e);System.exit(0);} // Test org.postgresql.Connection using org.postgresql.Driver System.out.println("Testing org.postgresql.Connection using org.postgresql.Driver"); try { Class.forName("org.postgresql.Driver"); // Load driver try { org.postgresql.Connection con = (org.postgresql.Connection) DriverManager.getConnection(url, myLogin, myPassword); // Establish connection org.postgresql.ResultSet rs = (org.postgresql.ResultSet) con.ExecSQL(query1,con.createStatement()); // Execute statement // Print some results set meta data int cols = rs.getColumnCount(); System.out.println("Column Count " + cols); System.out.println("Tuple Count " + rs.getTupleCount()); // Print results set values System.out.println("No way of printing column names from this results set?"); while (rs.next()) { for (int i = 1; i <= cols; i ++) { System.out.print(rs.getString(i) + ","); } System.out.println(rs.getString(cols)); } con.close(); // Close connection } catch (java.sql.SQLException e) {System.out.println(e);System.exit(0);} } catch (ClassNotFoundException e) {System.out.println(e);System.exit(0);} */ // Test org.postgresql.jdbc2.Connection using org.postgresql.Driver System.out.println("Testing org.postgresql.jdbc2.Connection using org.postgresql.Driver"); try { url = "jdbc:postgresql://feathers.leeds.ac.uk/stats19"; myLogin = "andyt"; myPassword = "andyt"; query1 = "SELECT * FROM acc92 LIMIT 10;"; Class.forName("org.postgresql.Driver"); // Load dirver org.postgresql.jdbc2.Connection con = (org.postgresql.jdbc2.Connection) DriverManager.getConnection(url, myLogin, myPassword); // Open connection org.postgresql.jdbc2.Statement s = (org.postgresql.jdbc2.Statement) con.createStatement(org.postgresql.jdbc2.ResultSet.TYPE_SCROLL_SENSITIVE, org.postgresql.jdbc2.ResultSet.CONCUR_READ_ONLY); // Open statement org.postgresql.jdbc2.ResultSet rs = (org.postgresql.jdbc2.ResultSet) s.executeQuery(query1); // Execute statement // Print some results set meta data org.postgresql.jdbc2.ResultSetMetaData rsmd = (org.postgresql.jdbc2.ResultSetMetaData) rs.getMetaData(); int cols = rsmd.getColumnCount(); System.out.println("Column Count " + cols); System.out.println("Tuple Count " + rs.getTupleCount()); for (int i = 1; i <= cols; i ++) { // System.out.print(rsmd.getColumnName(i) + ","); } System.out.println(rsmd.getColumnName(cols)); // Print results set values while (rs.next()) { for (int i = 1; i <= cols; i ++) { // System.out.print(rs.getString(i) + ","); } System.out.println(rs.getString(cols)); } s.close(); // Close statement con.close(); // Close connection } catch (java.sql.SQLException e) {System.out.println(e);System.exit(0);} catch (ClassNotFoundException e) {System.out.println(e);System.exit(0);} } }