From f41dcbe4d810f0906f43d1b345cf506d72c792ac Mon Sep 17 00:00:00 2001 From: Peter Mount Date: Thu, 12 Oct 2000 08:55:28 +0000 Subject: Major update part I involving delayed patches, reworked Makefile, and how the versioning works. There's also a new utils directory used by Makefile --- src/interfaces/jdbc/example/psql.java | 423 +++++++++++++++++----------------- 1 file changed, 213 insertions(+), 210 deletions(-) (limited to 'src/interfaces/jdbc/example/psql.java') diff --git a/src/interfaces/jdbc/example/psql.java b/src/interfaces/jdbc/example/psql.java index 1493a3b8220..9e63ad8129e 100644 --- a/src/interfaces/jdbc/example/psql.java +++ b/src/interfaces/jdbc/example/psql.java @@ -1,210 +1,213 @@ -package example; - -import java.io.*; -import java.sql.*; -import java.text.*; - -/** - * This example application demonstrates some of the drivers other features - * by implementing a simple psql replacement in Java. - * - */ - -public class psql -{ - Connection db; // The connection to the database - Statement st; // Our statement to run queries with - DatabaseMetaData dbmd; // This defines the structure of the database - - public psql(String args[]) throws ClassNotFoundException, FileNotFoundException, IOException, SQLException - { - String url = args[0]; - String usr = args[1]; - String pwd = args[2]; - - // Load the driver - Class.forName("org.postgresql.Driver"); - - // Connect to database - System.out.println("Connecting to Database URL = " + url); - db = DriverManager.getConnection(url, usr, pwd); - - dbmd = db.getMetaData(); - st = db.createStatement(); - - // This prints the backend's version - System.out.println("Connected to "+dbmd.getDatabaseProductName()+" "+dbmd.getDatabaseProductVersion()); - - System.out.println(); - - // This provides us the means of reading from stdin - StreamTokenizer input = new StreamTokenizer(new InputStreamReader(System.in)); - input.resetSyntax(); - input.slashSlashComments(true); // allow // as a comment delimiter - input.eolIsSignificant(false); // treat eol's as spaces - input.wordChars(32,126); - input.whitespaceChars(59,59); - input.quoteChar(39); - - // Now the main loop. - int tt=0,lineno=1; - while(tt!=StreamTokenizer.TT_EOF) { - System.out.print("["+lineno+"] "); - System.out.flush(); - - // Here, we trap SQLException so they don't terminate the application - try { - if((tt=input.nextToken())==StreamTokenizer.TT_WORD) { - processLine(input.sval); - lineno++; - } - } catch(SQLException ex) { - System.out.println(ex.getMessage()); - } - } - - System.out.println("Now closing the connection"); - st.close(); - db.close(); - - } - - /** - * This processes a statement - */ - public void processLine(String line) throws SQLException - { - if(line.startsWith("\\")) { - processSlashCommand(line); - return; - } - - boolean type = st.execute(line); - boolean loop=true; - while(loop) { - if(type) { - // A ResultSet was returned - ResultSet rs=st.getResultSet(); - displayResult(rs); - } else { - int count = st.getUpdateCount(); - - if(count==-1) { - // This indicates nothing left - loop=false; - } else { - // An update count was returned - System.out.println("Updated "+st.getUpdateCount()+" rows"); - } - } - - if(loop) - type = st.getMoreResults(); - } - } - - /** - * This displays a result set. - * Note: it closes the result once complete. - */ - public void displayResult(ResultSet rs) throws SQLException - { - ResultSetMetaData rsmd = rs.getMetaData(); - - // Print the result column names - int cols = rsmd.getColumnCount(); - for(int i=1;i<=cols;i++) - System.out.print(rsmd.getColumnLabel(i)+(i3) - DriverManager.setLogStream(System.err); - - // Now run the tests - try { - psql test = new psql(args); - } catch(Exception ex) { - System.err.println("Exception caught.\n"+ex); - ex.printStackTrace(); - } - } -} +package example; + +import java.io.*; +import java.sql.*; +import java.text.*; + +/** + * This example application demonstrates some of the drivers other features + * by implementing a simple psql replacement in Java. + * + */ + +public class psql +{ + Connection db; // The connection to the database + Statement st; // Our statement to run queries with + DatabaseMetaData dbmd; // This defines the structure of the database + boolean done = false; // Added by CWJ to permit \q command + + public psql(String args[]) throws ClassNotFoundException, FileNotFoundException, IOException, SQLException + { + String url = args[0]; + String usr = args[1]; + String pwd = args[2]; + + // Load the driver + Class.forName("org.postgresql.Driver"); + + // Connect to database + System.out.println("Connecting to Database URL = " + url); + db = DriverManager.getConnection(url, usr, pwd); + + dbmd = db.getMetaData(); + st = db.createStatement(); + + // This prints the backend's version + System.out.println("Connected to "+dbmd.getDatabaseProductName()+" "+dbmd.getDatabaseProductVersion()); + + System.out.println(); + + // This provides us the means of reading from stdin + StreamTokenizer input = new StreamTokenizer(new InputStreamReader(System.in)); + input.resetSyntax(); + input.slashSlashComments(true); // allow // as a comment delimiter + input.eolIsSignificant(false); // treat eol's as spaces + input.wordChars(32,126); + input.whitespaceChars(59,59); + // input.quoteChar(39); *** CWJ: messes up literals in query string *** + + // Now the main loop. + int tt=0,lineno=1; + while(tt!=StreamTokenizer.TT_EOF && ! done) { // done added by CWJ to permit \q command + System.out.print("["+lineno+"] "); + System.out.flush(); + + // Here, we trap SQLException so they don't terminate the application + try { + if((tt=input.nextToken())==StreamTokenizer.TT_WORD) { + processLine(input.sval); + lineno++; + } + } catch(SQLException ex) { + System.out.println(ex.getMessage()); + } + } + + System.out.println("Now closing the connection"); + st.close(); + db.close(); + + } + + /** + * This processes a statement + */ + public void processLine(String line) throws SQLException + { + if(line.startsWith("\\")) { + processSlashCommand(line); + return; + } + + boolean type = st.execute(line); + boolean loop=true; + while(loop) { + if(type) { + // A ResultSet was returned + ResultSet rs=st.getResultSet(); + displayResult(rs); + } else { + int count = st.getUpdateCount(); + + if(count==-1) { + // This indicates nothing left + loop=false; + } else { + // An update count was returned + System.out.println("Updated "+st.getUpdateCount()+" rows"); + } + } + + if(loop) + type = st.getMoreResults(); + } + } + + /** + * This displays a result set. + * Note: it closes the result once complete. + */ + public void displayResult(ResultSet rs) throws SQLException + { + ResultSetMetaData rsmd = rs.getMetaData(); + + // Print the result column names + int cols = rsmd.getColumnCount(); + for(int i=1;i<=cols;i++) + System.out.print(rsmd.getColumnLabel(i)+(i3) + DriverManager.setLogStream(System.err); + + // Now run the tests + try { + psql test = new psql(args); + } catch(Exception ex) { + System.err.println("Exception caught.\n"+ex); + ex.printStackTrace(); + } + } +} -- cgit v1.2.3