/*
* This sample shows how to use the "define" extensions.
* The define extensions allow the user to specify the types
* under which to retrieve column data in a query.
*
* This saves round-trips to the database (otherwise necessary to
* gather information regarding the types in the select-list) and
* conversions from native types to the types under which the user
* will get the data.
*
* This can also be used to avoid streaming of long columns, by defining
* them as CHAR or VARCHAR types.
*/
// You need to import the java.sql package to use JDBC
import java.sql.*;
// You need to import oracle.jdbc.driver.* in order to use the
// API extensions.
import oracle.jdbc.driver.*;
class DefineColumnType
{
public static void main (String args [])
throws SQLException
{
// Load the Oracle JDBC driver
DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver());
// Connect to the database
// You can put a database name after the @ sign in the connection URL.
Connection conn =
DriverManager.getConnection ("jdbc:oracle:oci7:@", "scott", "tiger");
Statement stmt = conn.createStatement ();
// Call DefineColumnType to specify that the column will be
// retrieved as a String to avoid conversion from NUMBER to String
// on the client side. This also avoids a round-trip to the
// database to get the column type.
//
// There are 2 defineColumnType API. We use the one with 3 arguments.
// The 3rd argument allows us to specify the maximum length
// of the String. The values obtained for this column will
// not exceed this length.
((OracleStatement)stmt).defineColumnType (1, Types.VARCHAR, 2);
ResultSet rset = stmt.executeQuery ("select empno from emp");
while (rset.next ())
{
System.out.println (rset.getString (1));
}
stmt.close ();
}
}
|