SqlUtil class
The SqlUtil class contains helper methods for two main purposes:
At present, this class contains only one helper method, loadQuery( ). This method has six syntax variants.
Although anyone developing a NAS application can use SqlUtil, this class is typically used in components generated by Netscape Application Builder.
Package
com.kivasoft.util
Variables
Constructor
SqlUtil extends the Object class and should never be instantiated. The SqlUtil constructor is as follows:
public SqlUtil()
Methods
Examples
Example 1
The following code shows how you might use a SELECT query:
try
{
int colIndex;
int x;
long y;
NASRowSet rowSet = new com.netscape.server.jdbc.NASRowSet();
// Initialize connection info
rowSet.setDataSourceName("jdbc/nsample");
// permanent parameter substitutions in the SQL statement.
IValList param = GX.CreateValList();
param.setValString("REGION", "WEST");
param.setValInt("POPULATION", 100000);
SqlUtil.loadQuery(rowSet, "state.gxq", "STATES_select1", param);
// temporary parameter substitutions.
rowSet.setString(param.getValInt("STATE"), "CA");
rowSet.execute();
// get the return values.
colIndex = 1;
x = rowSet.getInt(colIndex++);
y = rowSet.getLong(colIndex++);
rowSet.clearParameters();
// temporary parameter substitutions.
rowSet.setString(param.getValInt("STATE"), "TX");
rowSet.execute();
// get the return values.
colIndex = 1;
x = rowSet.getInt(colIndex++);
y = rowSet.getLong(colIndex++);
rowSet.close();
}
catch (SQLException err)
{
}
Example 2
The following code shows how you might use an UPDATE query:
try
{
String url = "jdbc:netscape:ODBC:nsample"
java.util.Properties props = new java.util.Properties();
props.put("DRIVER", "ODBC");
props.put("DSN", "nsample");
props.put("DB", "nsample");
props.put("user", "netscape");
props.put("password", "netscape");
Connection conn = DriverManager.getDriver(url).connect(url, props);
// permanent parameter substitutions in the SQL statement.
IValList param = GX.CreateValList();
param.setValString("REGION", "WEST");
param.setValInt("POPULATION", 100000);
PreparedStatement prestate =
SqlUtil.loadQuery(conn, "state.gxq", "STATES_update1", param);
// temporary parameter substitutions.
prestate.setString(param.getValInt("STATE"), "CA");
prestate.setInt(param.getValInt("NEW_POP"), 10000);
prestate.execute();
prestate.clearParameters();
// temporary parameter substitutions.
prestate.setString(param.getValInt("STATE"), "TX");
prestate.setInt(param.getValInt("NEW_POP"), 10000);
prestate.execute();
conn.close();
}
catch (SQLException err)
{
}
Related Topics
com.netscape.server.jdbc.NASRowSet class, java.sql.Connection interface, java.sql.PreparedStatement interface, javax.sql.RowSet interface
Using JDBC for Database Access
loadQuery( )
Loads a query.
Syntax 1
Use any of the following three variants to initialize a RowSet, given a SELECT query.
static public boolean loadQuery(
RowSet rs,
String filepath,
String queryname,
IValList params)
Syntax 2
static public boolean loadQuery(
RowSet rs,
String query,
IValList params)
Syntax 3
static public boolean loadQuery(
RowSet rs,
Reader r,
String queryname,
IValList params)
Syntax 4
Use any of the following three variants to generate a PreparedStatement, given
an INSERT, UPDATE, or DELETE query.
static public PreparedStatement loadQuery(
Connection conn,
String filepath,
String queryname,
IValList params)
Syntax 5
static public PreparedStatement loadQuery(
Connection conn,
String query,
IValList params)
Syntax 6
static public PreparedStatement loadQuery(
Connection conn,
Reader r,
String queryname,
IValList params)
rs.
Specifies the rowset on which the query will act. The rowset handed in must have its Connection initialized; otherwise the loadQuery( ) call will fail.
filepath.
The full pathname of the query file (.gxq file).
queryname.
For Syntax 3 (using a RowSet), the name of the SELECT query in the query file. For Syntax 6 (using a Connection), the name of the INSERT, UPDATE, or DELETE query in the query file.
query.
For Syntax 2 (using a RowSet), the SELECT query. For Syntax 5 (using a Connection), the INSERT, UPDATE, or DELETE query.
params.
A list of parameter-value pairs that will be permanently replaced in the SELECT query.
r.
A reader that points to the contents of a query file (.gxq file).
conn.
Connection information needed to generate a PreparedStatement.
Usage
Use loadQuery( ) either to initialize a RowSet given a SELECT query, or to generate a PreparedStatement given an INSERT, UPDATE, or DELETE query. This method performs the following:
Return Value
The boolean syntax returns true if no errors were encountered or returns false for failure. The PreparedStatement syntax has the following return values: if no errors are encountered, loadQuery( ) returns the PreparedStatement object that references the query; otherwise, the method returns null for failure.
|