Java code database updating
You’re going to learn about annotations in this guide, but if you want to have a look at the legacy XML mappings, have a look at this link for example.
That let’s Hibernate know you have one User.class, which has annotations (most importantly, the @Entity annotation) that tell Hibernate HOW to map that class automatically to an appropriate database table.
Java developers are usually more comfortable with writing Java classes, than with writing SQL.
Hence many (greenfield) projects are written with a Java-First approach, which means that you create your Java class before you create the corresponding database table.
You have the full power of SQL at your hand, but you need to make sure to somehow convert back and forth between your Java Objects and the SQL code yourself.
Also, you need to make sure to properly maintain and close your database connections/statements.
Also experiment with closing the connection and statements.
If you are a bit lost, read through the corresponding Oracle Documentation.
Also, there’s two versions of the Criteria API (1 and 2) and a fair amount of developers find version 1 actually easier to use than version 2, though it is being deprecated and will likely be completely inaccessible in the future.
One very popular library that helps you do this, is Hibernate, which has been around since 2001.
Let’s dive right in (and in case you enjoy videos, have a look at these short and practical screencasts, which illustrate all following concepts in a real-life scenario): A Session Factory basically represents the mapping between your Java classes and your database tables (more on that in a second).
As you can see, starting with Java 7, the auto-closing of connections and statements makes cleanup a lot easier.
If you have the misfortune of being on Android or Java 6 or below, you can use Guava’s Closer to achieve code almost as clean with regard to resource management.