Java Reference
In-Depth Information
The basic purpose of each of these methods is fairly self-evident, but the
distinction between the last two is worth clarifying. Method getColumnType returns
the selected fi eld's SQL type as an integer matching one of the named constants in
class java.sql.Types , while method getColumnTypeName returns the string holding
the database-specifi c type name for the selected fi eld. Now for the example…
Example
This example uses the Accounts table in our Finances database to retrieve all data
relating to account number 12345. It then uses the above methods to display the name
of each fi eld, its database-specifi c type name and the value held (after ascertaining
the fi eld's data type, so that the appropriate Java getXXX method can be called).
import java.sql.*;
public class JDBCMetaData
{
private static Connection connection;
private static Statement statement;
private static ResultSet results;
public static void main(String[] args)
{
try
{
//Step 1…
connection = DriverManager.getConnection(
"jdbc:odbc:Finances","","");
}
catch(ClassNotFoundException cnfEx)
{
System.out.println(
"* Unable to load driver! *");
System.exit(1);
}
catch(SQLException sqlEx)
{
System.out.println(
"* Cannot connect to database! *");
System.exit(1);
}
try
{
//Step 2…
statement = connection.createStatement();
Search WWH ::




Custom Search