Database Reference
In-Depth Information
// Harness.java: test harness for Cookbook library class
import
java.sql.*
;
import
com.kitebird.mcb.Cookbook
;
public
class
Harness
{
public
static
void
main
(
String
[]
args
)
{
Connection
conn
=
null
;
try
{
conn
=
Cookbook
.
connect
();
System
.
out
.
println
(
"Connected"
);
}
catch
(
Exception
e
)
{
Cookbook
.
printErrorMessage
(
e
);
System
.
exit
(
1
);
}
finally
{
if
(
conn
!=
null
)
{
try
{
conn
.
close
();
System
.
out
.
println
(
"Disconnected"
);
}
catch
(
Exception
e
)
{
String
err
=
Cookbook
.
getErrorMessage
(
e
);
System
.
out
.
println
(
err
);
}
}
}
}
}
Harness.java
also shows how to use the error message utility methods from the
Cook
book
class when a MySQL-related exception occurs:
•
printErrorMessage()
takes the exception object and uses it to print an error mes‐
sage to
System.err
.
•
getErrorMessage()
returns the error message as a string. You can display the
message yourself, write it to a logfile, or whatever.