Java Reference
In-Depth Information
"+sqlex.getMessage());
System.err.println("SQL
state
:
"+sqlex.getSQLState());
System.err.println("Error
code:
"+sqlex.getErrorCode());
System.err.println("Cause:
"+sqlex.getCause());
sqlex = sqlex.getNextException();
}
}
}
}
Aftersuccessfullyestablishingaconnectiontothe
employee
datasource,
main()
createsastatementandusesittoexecuteSQLstatementsforcreating,insertingvalues
into, querying, and dropping an
EMPLOYEES
table.
The
executeQuery()
methodreturnsa
ResultSet
objectthatprovidesaccess
toaquery'stabularresults.Eachresultsetisassociatedwitha
cursor
thatprovidesac-
cesstoaspecificrowofdata.Thecursorinitiallypointsbeforethefirstrow;call
Res-
ultSet
's
boolean next()
methodtoadvancethecursortothenextrow.Aslong
as there's a next row, this method returns true; it returns false when there are no more
rows to examine.
ResultSet
also declares various methods for returning the current row's column
valuesbasedontheirtypes.Forexample,
int getInt(String columnLabel)
returns the integer value corresponding to the
INTEGER
-based column identified by
columnLabel
. Similarly,
String getString(String columnLabel)
re-
turns the string value corresponding to the
VARCHAR
-based column identified by
columnLabel
.
Tip
If you don't have column names but have zero-based column indexes, call
ResultSet
methodssuchas
int getInt(int columnIndex)
and
String
getString(int columnIndex)
. However, best practice is to call
int
getInt(String columnLabel)
.
(
java EmployeeDB
). You should observe the following output: