Java Reference
In-Depth Information
apl.x = loc.getX();
apl.y = loc.getY();
apl.z = loc.getZ();
HashMap < String , Object > search = new HashMap < String , Object >();
search.put( "player_name" , player.getDisplayName());
try {
Database.get().update(apl, search);
} catch (DatabaseWriteException e) {
logger.error(e);
logger.info( "error" );
}
}
}
Here we run through the list of all online players. For each, we stick their
name and x-, y-, and z-coordinates into a new AllPlayerLocations object. That will
get saved into the database with the call to update() down at .
How does the database know what to update? You have to pass it a HashMap
that specifies the record you want to update (or create, if this is the first time).
So at , we'll build a quick hash that tells the database “update the record
where the field named player_name is equal to this Player's getDisplayName().”
Now we can call the database's update function, which looks like this:
try {
Database.get().update(apl, search);
} catch (DatabaseWriteException e) {
logger.error(e);
logger.info( "error" );
}
Now there's something we haven't seen before; what does this extra, mysteri-
ous code do?
Catch Exceptions in Java
See the code block bracketed by the try / catch keywords? The database functions
are declared to “throw exceptions” when something goes wrong (maybe there
was an error writing the file because the disk is full). What's an exception?
An exception interrupts the code that's currently being run, throws away the
rest of the function, and jumps straight to the catch block. It's sort of like if
the fire alarm rang right now, you'd jump up and run out, and not finish
reading the page.
If there's no error, then the catch block will never be run.
 
 
Search WWH ::




Custom Search