jcs.util
Class ExecSql

java.lang.Object
  |
  +--jcs.util.ExecSql

public class ExecSql
extends java.lang.Object

A class with miscellaneous static methods to execute SQL statements and scripts.

  connectTo(server)  -   Return a connection to the given server.
                         Specify an empty server when you invoke
                         this method in the server.

  statement(sql, server) - Get a connection and execute the given SQL statement.

  query(sql, server) - Get a connection, execute the given SQL query, and return
       the ResultSet.
  querySingleString(sql, server) - Get a connection, execute the given query,
       which must return a single column/row that is a string, and return that string.  

  querySingleInt(sql, server) - Get a connection, execute the given query,
       which must return a single column/row that is an int, and return that int.  
  


Method Summary
static java.sql.Connection connectTo(java.lang.String server)
          Connects to the given SQL server.
static java.sql.ResultSet query(java.lang.String query, java.lang.String server)
          Connects to the given server, and executes the given SQL query.
static int querySingleInt(java.lang.String query, java.lang.String server)
          Connects to the given server, executes the given single-valued SQL query, and returns that single (int) value.
static java.lang.String querySingleString(java.lang.String query, java.lang.String server)
          Connects to the given server, executes the given single-valued SQL query, and returns that single (String) value.
static void statement(java.lang.String sql, java.lang.String server)
          Connects to the given server, and executes the given SQL statement.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

connectTo

public static java.sql.Connection connectTo(java.lang.String server)
                                     throws java.lang.Exception
Connects to the given SQL server.

This method is intended for routines that will run in either a client or server environment.

Parameters:
server - a string that identifies the server. See above.
Returns:
a java.sql.Connection object for the server.
Throws:
java.lang.Exception - Thrown by java.io, java.sql, or java.util

statement

public static void statement(java.lang.String sql,
                             java.lang.String server)
                      throws java.lang.Exception
Connects to the given server, and executes the given SQL statement. This is a convenience method for non-parameterized statements.

Example Java code:

   ExecSql.statement("delete from emps", "");
   String stmt = 
          "set quoted_identifier on  "
         + "insert into emps('emp id', name) " +
         + "values('12345', 'John Doe')";
   ExecSql.statement(stmt, "some-server:1000?user=sa");
   
Parameters:
sql - a string with the SQL statement to be executed. This statement must have no question-mark parameters, and must return no result set.
server - a string that identifies the server in which to execute the statement.
Throws:
java.lang.Exception - Thrown by java.io, java.sql, or java.util
See Also:
connectTo(java.lang.String)

query

public static java.sql.ResultSet query(java.lang.String query,
                                       java.lang.String server)
                                throws java.lang.Exception
Connects to the given server, and executes the given SQL query. This is a convenience method for non-parametrized queries.

Example Java code:

   java.sql.ResultSet rs 
            = ExecSql.query("select * from systypes", "");
   
Parameters:
sql - a string with the SQL statement to be executed. This statement must have no question-mark parameters, and must return a result set.
server - a string that identifies the server in which to execute the statement.
Throws:
java.lang.Exception - Thrown by java.io, java.sql, or java.util
See Also:
connectTo(java.lang.String)

querySingleString

public static java.lang.String querySingleString(java.lang.String query,
                                                 java.lang.String server)
                                          throws java.lang.Exception
Connects to the given server, executes the given single-valued SQL query, and returns that single (String) value.

This is a convenience method for non-parametrized queries that return a single row with a single string column.

Example Java code:

   String s = ExecSql.querySingleString("select max(name) from systypes",  
                                         "antibes:4000?user=sa");
   
Parameters:
sql - a string with the SQL statement to be executed. This statement must have no question-mark parameters, and must return a result set with a single row and a single column that is a character string or String.
server - a string that identifies the server in which to execute the statement.
Throws:
java.lang.Exception - Thrown by java.io, java.sql, or java.util
See Also:
connectTo(java.lang.String)

querySingleInt

public static int querySingleInt(java.lang.String query,
                                 java.lang.String server)
                          throws java.lang.Exception
Connects to the given server, executes the given single-valued SQL query, and returns that single (int) value.

This is a convenience method for non-parametrized queries that return a single row with a single column.

Example Java code:

   String s = query("select count(*) from systypes",  
                                         "antibes:4000?user=sa");
   
Parameters:
sql - a string with the SQL statement to be executed. This statement must have no question-mark parameters, and must return a result set with a single row and a single column that is an integer.
server - a string that identifies the server in which to execute the statement.
Throws:
java.lang.Exception - Thrown by java.io, java.sql, or java.util
See Also:
connectTo(java.lang.String)