Question

In: Computer Science

Discuss the techniques for accessing databases from Java programs identical in web applications as in standalone...

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

Solutions

Expert Solution

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:

  1. 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.

  2. 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.

  3. 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:

  1. You mainly use jOOQ’s code generator to connect to your database and generate Java classes that certainly model your database tables and columns.

  2. Instead of writing SQL String statements with plain JDBC, you will use those generated Java classes to write your SQL queries.

  3. 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.


Related Solutions

Research information on how Python is used for Web applications and accessing resources on the Web?...
Research information on how Python is used for Web applications and accessing resources on the Web? Please help me for short discussion. Thanks in advance.
Discuss what web applications are and how they differ from other operating system based applications (include...
Discuss what web applications are and how they differ from other operating system based applications (include the role of IIS) Also, explore at least three vulnerabilities to utilizing web applications on a web server. 200 words or more, please.
JAVA PROGRAM (Make sure that programs are running. Please discuss, if there is any compilation or...
JAVA PROGRAM (Make sure that programs are running. Please discuss, if there is any compilation or run error.) Q. 1 Calculate the expression a)         x= 7.0 + (12 %3)*5 – 3;                    b)         x= 7 + (11 / 2)*5 + 3;            Q 2: Write a program that prompts the user to enter five test scores and then prints the average test score. (Assume that the test scores are decimal numbers.) Q 3. Write a program that prompts the capacity, in gallons,...
Discuss the applications of ANOVA (One-Way ANOVA, Two-Way ANOVA) and regression techniques in the context of...
Discuss the applications of ANOVA (One-Way ANOVA, Two-Way ANOVA) and regression techniques in the context of e-commerce firms like Amazon, Flipkart etc.
Discuss the five-factor model of personality. Aside from the obvious applications in the mental health industry,...
Discuss the five-factor model of personality. Aside from the obvious applications in the mental health industry, what other applications do you see for this model? Expand on your perspective.
Discuss at least three (3) of the capital budgeting techniques. Also, include the circumstance from which...
Discuss at least three (3) of the capital budgeting techniques. Also, include the circumstance from which its use is preferred.
Discuss with your classmates, the impact of Java from a personal, business, and societal perspective. How...
Discuss with your classmates, the impact of Java from a personal, business, and societal perspective. How much do you interact with Java on a daily basis?
The direct-manipulation systems have significantly evolved overtime, discuss some latest applications that benefit from using direct-manipulation...
The direct-manipulation systems have significantly evolved overtime, discuss some latest applications that benefit from using direct-manipulation interfaces. Support your answer with suitable examples.
The direct-manipulation systems have significantly evolved overtime, discuss some latest applications that benefit from using direct-manipulation...
The direct-manipulation systems have significantly evolved overtime, discuss some latest applications that benefit from using direct-manipulation interfaces. Support your answer with suitable examples. Post your responses, examples, ideas, and discussions on this topic
The direct-manipulation systems have significantly evolved overtime, discuss some latest applications that benefit from using direct-manipulation...
The direct-manipulation systems have significantly evolved overtime, discuss some latest applications that benefit from using direct-manipulation interfaces. Support your answer with suitable examples.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT