Java Reference
In-Depth Information
String select = "SELECT * FROM Accounts"
+ " WHERE acctNum = 123456";
//Step 3…
results = statement.executeQuery(select);
//Start of step 4…
ResultSetMetaData metaData =
results.getMetaData();
int numFields = metaData.getColumnCount();
//Check that record has been found…
boolean found = results.next();
if (!found)
{
//No point in continuing…
System.out.println("\nNot found!");
connection.close();
return;
}
//Cycle through the database fi elds, displaying
//meta data about each one…
for (int i=1; i<=numFields; i++)
//N.B. Remember that count must start at 1!
{
System.out.println("\nField name: "
+ metaData.getColumnName(i));
System.out.println("Field type: "
+ metaData.getColumnTypeName(i));
int colType = metaData.getColumnType(i);
System.out.print("Value: ");
//Select the appropriate getXXX method,
//according to the SQL type of the fi eld…
switch (colType)
{
case Types.INTEGER:
System.out.println(
results.getInt(i));
break;
case Types.VARCHAR:
System.out.println(
results.getString(i));
break;
case Types.NUMERIC:
Search WWH ::




Custom Search