com.noelios.restlet.ext.jdbc
Class JdbcClientHelper

java.lang.Object
  extended by org.restlet.util.Helper
      extended by com.noelios.restlet.ConnectorHelper
          extended by com.noelios.restlet.ClientHelper
              extended by com.noelios.restlet.ext.jdbc.JdbcClientHelper

public class JdbcClientHelper
extends ClientHelper

Client connector to a JDBC database.
To send a request to the server, create a new instance of a client supporting the JDBC Protocol and invoke the handle() method.
Alternatively, you can create a new Call with the JDBC URI as the resource reference and use an XML request as the entity.

Database connections are optionally pooled using Apache Commons DBCP. In this case, a different connection pool is created for each unique combination of JDBC URI and connection properties.

Do not forget to register your JDBC drivers before using this client. See JDBC DriverManager API for details

Sample XML request:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<request>
  <header>
    <connection>
      <usePooling>true</usePooling>
      <property name="user">scott</property >
      <property name="password">tiger</property >
      <property name="...">1234</property >
      <property name="...">true</property >
    </connection>
    <returnGeneratedKeys>true</returnGeneratedKeys>
  </header>
  <body>
    <statement>UPDATE myTable SET myField1="value1" </statement>
    <statement>SELECT msField1, myField2 FROM myTable</statement>
  </body>
</request>

Several SQL Statements can be specified.
A RowSetRepresentation of the last correctly executed SQL request is returned to the Client.

Author:
Jerome Louvel (contact@noelios.com), Thierry Boileau
See Also:
RowSetRepresentation

Constructor Summary
JdbcClientHelper(Client client)
          Constructor.
 
Method Summary
static Request create(String jdbcURI, Representation request)
          Creates an uniform call.
protected static org.apache.commons.pool.ObjectPool createConnectionPool(String uri, Properties properties)
          Creates a connection pool for a given connection configuration.
protected  Connection getConnection(String uri, Properties properties, boolean usePooling)
          Returns a JDBC connection.
 void handle(Request request, Response response)
          Handles a call.
static String sqlEncode(String query)
          Escapes quotes in a SQL query.
 
Methods inherited from class com.noelios.restlet.ClientHelper
getClient, getContext, getLogger, getParameters
 
Methods inherited from class com.noelios.restlet.ConnectorHelper
createContext, getProtocols, start, stop
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JdbcClientHelper

public JdbcClientHelper(Client client)
Constructor.

Parameters:
client - The client to help.
Method Detail

create

public static Request create(String jdbcURI,
                             Representation request)
Creates an uniform call.

Parameters:
jdbcURI - The database's JDBC URI (ex: jdbc:mysql://[hostname]/[database]).
request - The request to send (valid XML request).

handle

public void handle(Request request,
                   Response response)
Handles a call.

Overrides:
handle in class ConnectorHelper
Parameters:
request - The request to handle.
response - The response to update.

getConnection

protected Connection getConnection(String uri,
                                   Properties properties,
                                   boolean usePooling)
                            throws SQLException
Returns a JDBC connection.

Parameters:
uri - The connection URI.
properties - The connection properties.
usePooling - Indicates if the connection pooling should be used.
Returns:
The JDBC connection.
Throws:
SQLException

sqlEncode

public static String sqlEncode(String query)
Escapes quotes in a SQL query.

Parameters:
query - The SQL query to escape.
Returns:
The escaped SQL query.

createConnectionPool

protected static org.apache.commons.pool.ObjectPool createConnectionPool(String uri,
                                                                         Properties properties)
Creates a connection pool for a given connection configuration.

Parameters:
uri - The connection URI.
properties - The connection properties.
Returns:
The new connection pool.


Copyright © 2005-2007 Noelios Consulting.