In: Computer Science
Discuss the techniques for accessing databases from Java programs identical in web applications as in standalone Java programs. Need 2 references and 300 plus words
So Student as you mentioned I get it you want to know the techniques involved in java and database.So lets start
I would like to tell whenever all of us want to access a database with our server or our desktop with our own Java application these questions arises and mostly pop up in our mind :
Are we approaching our application from a Java-first or database-first perspective?
Do we want to write Java classes or SQL statements first?
Do we need to integrate with an existing database?
How do we execute our SQL statements? From small CRUD operations (select from, insert into, update where) to more complex SQL related queries for the (analytics functions)?
How easily can we do object-relational mapping? Which means mapping between Java objects and database tables and rows?
To understand the concept of object-relational mapping we first have to imagine a Java class like this:\
public class student{
private Integer Id;
private string firstname;
private string lastname;
//these are constructors getters ,setters//
}
id |
first_name |
last_name |
1 |
Rahul |
Amar |
2 |
max |
mun |
3 |
donald |
trump |
This is the users database table.
After mentioning this table the question which might be arising in your mind is how do we map java class and that Table?
Here comes our main points
1.JDBC,this is the low level choice
2.The most convenient lightweight SQL frameworks like jOOQ or Spring’s JDBC abstraction.
3.The third and another most is full-blown ORMs like Hibernate or any other JPA implementation.
We will start Discussing JDBC,so what is JDBC?
As mentioned the most low-level way to accessing databases in Java, is through the JDBC API which stands for Java Database Connectivity.
Every framework that we encounter in this discussion uses JDBC under the hood. But you can, of course, choose to use it directly to execute your SQL queries.
The advantage of using JDBC libraries is that we don’t need any 3rd-party libraries as it comes with every JDK/JRE. You only need an appropriate JDBC driver for your specific database.
When you are using JDBC you have the full power and speed of SQL and JDBC at your hand, but you need to make sure to somehow convert back and forth between your Java Objects and the SQL code yourself.
You also have to make sure to become a good developer and open/close database connections yourself.
Java ORM Frameworks: Hibernate, JPA.
we know that Java developers are usually more comfortable with writing Java classes, than with writing SQL statements.Hence many projects are java first approach first the java classes are written then the SQL table is made.
This process naturally leads to the object-relational mapping question:
How do you map from your newly written Java class to your yet to be created database table?
And could you even generate your database from those Java classes?
This is where and how our process full-blown ORMs, like Hibernate or any other JPA implementation come into play.
What is Hibernate?
t Hibernate does well:
It lets you to easily convert between database tables and java classes, without you having to go through to much apart from an initial mapping.
It allows you to not have to write any SQL code for basic operations. Consider the basic e.g:-creating a user, deleting a user, updating a user.
It offers a couple query mechanisms (HQL, Criteria API) on top of SQL to query your databases.
Thisis just the basic idea to be ideal and short in many ways and is much more in deep.
What is the Java Persistence API (JPA)?
JPA is a specification, not an implementation or library. JPA defines a standard what features a library must support to be JPA part . And there’s a couple of libraries, like Hibernate tthat all implement the JPA specification.
In much simpler explanation: If our library supports for example saving objects to the database in a certain way, supports mapping and querying capabilities then you can call it fully JPA compliant.
So, instead of writing Hibernate specific code you write JPA specific code. And then it is just a matter of adding some libraries (Hibernate) and configuration file to your JPA project, and you can access your database.
Java SQL Libraries: A lightweight approach
JOOQ
jOOQ is a popular library fand it is very well maintained.
Essentially working with jOOQ brings down to the following points:
You mainly use jOOQ’s code generator to connect to your database and generate Java classes that certainly model your database tables and columns.
Instead of writing SQL String statements with plain JDBC, you will use those generated Java classes to write your SQL queries.
jOOQ will conveniently turn these Java classes and queries into real SQL, execute them against the database and map the results back to Java code.
I certainly hope that I have provided you with all the necessary information for your approach.