System.err.println("This example tests the binary large object api of the driver.\nBasically, it will allow you to store and view images held in the database.");
System.err.println("Note: If you are running this for the first time on a particular database,\nyou have to select \"Initialise\" in the \"PostgreSQL\" menu.\nThis will create a table used to store image names.");
System.out.println("Connecting to Database URL = "+url);
db=DriverManager.getConnection(url,usr,pwd);
System.out.println("Connected...Now creating a statement");
st=db.createStatement();
// Clean up the database (in case we failed earlier) then initialise
cleanup();
// Now run tests using JDBC methods
doexample();
// Clean up the database
cleanup();
// Finally close the database
System.out.println("Now closing the connection");
st.close();
db.close();
}
/**
* This drops the table (if it existed). No errors are reported.
*/
publicvoidcleanup()
{
try{
st.executeUpdate("drop table basic");
}catch(Exceptionex){
// We ignore any errors here
}
}
/**
* This performs the example
*/
publicvoiddoexample()throwsSQLException
{
System.out.println("\nRunning tests:");
// First we need a table to store data in
st.executeUpdate("create table basic (a int2, b int2)");
// Now insert some data, using the Statement
st.executeUpdate("insert into basic values (1,1)");
st.executeUpdate("insert into basic values (2,1)");
st.executeUpdate("insert into basic values (3,1)");
// For large inserts, a PreparedStatement is more efficient, because it
// supports the idea of precompiling the SQL statement, and to store
// directly, a Java object into any column. PostgreSQL doesnt support
// precompiling, but does support setting a column to the value of a
// Java object (like Date, String, etc).
//
// Also, this is the only way of writing dates in a datestyle independent
// manner. (DateStyles are PostgreSQL's way of handling different methods
// of representing dates in the Date data type.)
PreparedStatementps=db.prepareStatement("insert into basic values (?,?)");
for(inti=2;i<5;i++){
ps.setInt(1,4);// "column a" = 5
ps.setInt(2,i);// "column b" = i
ps.executeUpdate();// executeUpdate because insert returns no data
}
ps.close();// Always close when we are done with it
// Finally perform a query on the table
System.out.println("performing a query");
ResultSetrs=st.executeQuery("select a, b from basic");
if(rs!=null){
// Now we run through the result set, printing out the result.
// Note, we must call .next() before attempting to read any results
while(rs.next()){
inta=rs.getInt("a");// This shows how to get the value by name
intb=rs.getInt(2);// This shows how to get the value by column
System.out.println(" a="+a+" b="+b);
}
rs.close();// again, you must close the result when done
}
// Now run the query again, showing a more efficient way of getting the
// result if you don't know what column number a value is in
System.out.println("performing another query");
rs=st.executeQuery("select * from basic where b>1");
if(rs!=null){
// First find out the column numbers.
//
// It's best to do this here, as calling the methods with the column
// numbers actually performs this call each time they are called. This
// really speeds things up on large queries.
//
intcol_a=rs.findColumn("a");
intcol_b=rs.findColumn("b");
// Now we run through the result set, printing out the result.
// Again, we must call .next() before attempting to read any results
while(rs.next()){
inta=rs.getInt(col_a);// This shows how to get the value by name
intb=rs.getInt(col_b);// This shows how to get the value by column
System.out.println(" a="+a+" b="+b);
}
rs.close();// again, you must close the result when done
}
// The last thing to do is to drop the table. This is done in the
// cleanup() method.
}
/**
* Display some instructions on how to run the example
*/
publicstaticvoidinstructions()
{
System.out.println("\nThis example tests the basic components of the JDBC driver, demonstrating\nhow to build simple queries in java.\n");
System.out.println("Useage:\n java example.basic jdbc:postgresql:database user password [debug]\n\nThe debug field can be anything. It's presence will enable DriverManager's\ndebug trace. Unless you want to see screens of items, don't put anything in\nhere.");
System.exit(1);
}
/**
* This little lot starts the test
*/
publicstaticvoidmain(Stringargs[])
{
System.out.println("PostgreSQL basic test v6.3 rev 1\n");
if(args.length<3)
instructions();
// This line outputs debug information to stderr. To enable this, simply
System.err.println("java example.blobtest jdbc-url user password [debug]");
System.err.println("\nExamples:\n");
System.err.println("java -Djdbc.driver=postgresql.Driver example.blobtest jdbc:postgresql:test postgres password\nThis will run the tests on the database test on the local host.\n");
System.err.println("java -Djdbc.driver=postgresql.Driver example.blobtest jdbc:postgresql:test postgres password debug\nThis is the same as above, but will output debug information.\n");
System.err.println("This example tests the binary large object api of the driver.\nThis allows images or java objects to be stored in the database, and retrieved\nusing both postgresql's own api, and the standard JDBC api.");
// Now because the driver needs to know what the current style is,
// we have to run the following:
st.executeUpdate("show datestyle");
// This is a limitation, but there is no real way around this.
// Now we query the table.
ResultSetrs=st.executeQuery("select dt from datestyle");
// Throw an exception if there is no result (if the table is empty
// there should still be a result).
if(rs==null)
thrownewSQLException("The test query returned no data");
while(rs.next()){
// The JDBC spec states we should only read each column once.
// In the current implementation of the driver, this is not necessary.
// Here we use this fact to see what the query really returned.
if(standard.equals(rs.getDate(1)))
System.out.println(" passed, returned "+rs.getString(1));
else
System.out.println(" failed, returned "+rs.getString(1));
}
rs.close();
}
}
/**
* Display some instructions on how to run the example
*/
publicstaticvoidinstructions()
{
System.out.println("\nThis example tests the drivers ability to handle dates correctly if the\nbackend is running any of the various date styles that it supports.\nIdealy this should work fine. If it doesn't, then there is something wrong\npossibly in postgresql.Connection or in the backend itself. If this does occur\nthen please email a bug report.\n");
System.out.println("Useage:\n java example.datestyle jdbc:postgresql:database user password [debug]\n\nThe debug field can be anything. It's presence will enable DriverManager's\ndebug trace. Unless you want to see screens of items, don't put anything in\nhere.");
System.exit(1);
}
/**
* This little lot starts the test
*/
publicstaticvoidmain(Stringargs[])
{
System.out.println("PostgreSQL datestyle test v6.3 rev 1\n");
if(args.length<3)
instructions();
// This line outputs debug information to stderr. To enable this, simply
* Display some instructions on how to run the example
*/
publicstaticvoidinstructions()
{
System.out.println("\nThis example shows how some of the other JDBC features work within the\ndriver. It does this by implementing a very simple psql equivalent in java.\nNot everything that psql does is implemented.\n");
System.out.println("Useage:\n java example.psql jdbc:postgresql:database user password [debug]\n\nThe debug field can be anything. It's presence will enable DriverManager's\ndebug trace. Unless you want to see screens of items, don't put anything in\nhere.");
System.exit(1);
}
/**
* This little lot starts the test
*/
publicstaticvoidmain(Stringargs[])
{
System.out.println("PostgreSQL psql example v6.3 rev 1\n");
if(args.length<3)
instructions();
// This line outputs debug information to stderr. To enable this, simply