return to first page linux journal archive
keywordscontents
import java.io.*;


public class QueryTest extends Object 
{
  
public static void main(String[] args)
  {
    QueryTest dummy = new QueryTest();
  }
public QueryTest()
  {
    DataInputStream in = new DataInputStream(System.in);
    boolean done = false;
    String query;
    try {
      PGConnection conn = new PGConnection("localhost", "5432", "",
					   "", "foo");
      System.out.println("Connected to database " + conn.db() +
			 " on " + conn.host() + " port " +
conn.port()
			 + " tty " + conn.tty() + ".");
      while (!done)
	{
	  System.out.println("Enter a Query (q to quit):");
	  query = in.readLine();
	  if (query.equalsIgnoreCase("q"))
	    {
	      done = true;
	    }
	  else
	    {
	      try {
		PGResult res = conn.exec(query);
		if (res.resultStatus() == PGResult.PGRES_TUPLES_OK)
		  System.out.println("\n" +
				     res.formatTuples(true, " ",
true,
						      false));
		else
		  System.out.println("\nExec Returned Status: "
+
				     res.resultStatus());
		res.resClear();
	      }
	      catch (PostgresException p)
		{
		  System.out.println(p);
		}
	    }
	}
    }
    catch (Exception e)
      {
	//	System.err.println(e);
	e.printStackTrace();
      }
  }

}


Back to article