Databases with JAVA instead of SQL!

toyo2020 Posted messages 75 Status Membre -  
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   -

Hello, what is the advantage of managing databases with JAVA rather than with SQL?

With JAVA, a prerequisite is necessary (registering a database in ODBC & explicitly loading the driver via Class.forName ...) and then you need much longer code for the same result:

 requete = "INSERT INTO client VALUES ('value 1', 'value 2', ...)"; try { Statement stmt = con.createStatement(); ResultSet results = stmt.executeQuery(requete); } catch (SQLException e) { e.printStackTrace(); }

In the same example of inserting values with SQL, one line is enough:

INSERT INTO table VALUES ('value 1', 'value 2', ...)

My friend who was a student with me is a JAVA fan and codes in JAVA at his job; none of his arguments convinced me, SQL is better for me than JAVA when it comes to querying databases.

Maybe among the many registered on commentcamarche, there are some offering serious arguments for JAVA. I'm curious to read their arguments.


8 réponses

yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   Ambassadeur 1 587
 

Hello,

I'm curious to read how your SQL query will be executed.

I'm also wondering, out of curiosity, what you have studied.

1
KX Posted messages 19031 Status Modérateur 3 020
 

Hello,

SQL is a wheel, a very good wheel, that rolls very well, there's nothing to say about it, but it's just a wheel.

Java is a car, not really a racing car, but rather a family car that everyone can afford; it drives well for everyday trips and is more than sufficient for most users.

A car without a wheel isn't worth much, which is why Java knows how to use SQL databases.

It's worth noting that a car has 4 wheels, just as a Java program can handle multiple databases.

But Java is very vast, it could be a car, but also a motorcycle or a truck.

On the other hand, a wheel by itself, you can quickly get tired of it...


1
jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention   4 830
 

Hello,

How can we compare the two ??

Java is a programming language ..... SQL .. a querying language !!

Anyway .. if Java wants to manipulate a MySQL database .. it will necessarily have to use SQL.

On the contrary .. if you need to develop software .... SQL won't do anything for you.

So well .. unless you need to occasionally manipulate your database (to manually add data for testing purposes, for example).. you can do it directly in SQL. If, however, you need to automate things (or do them repetitively ...) going through a programming language (Java or another...) becomes obvious.

.

By the way .. your SQL query won't execute on its own (as Ygbe pointed out)...

To "launch" it.. you will need to either use the command line (and then you have to open a connection to the database ... then write your query to execute it.. or use software (phpMyAdmin, HeidiSQL, DBeaver ....) which (you can't see it.. does the same thing as your buddy in different programming languages depending on the chosen software...).

In short... there's no comparison to be made ... SQL is a "tool" that can be used by programming languages...


.
Best regards,
Jordane

0
toyo2020 Posted messages 75 Status Membre
 

I agree with both of you KX and jordane45 that SQL doesn't allow you to create software and is more like a wheel than a car since I have done SQL queries with Heidisql, and I have also integrated them into VisualBasic.

However, I don’t know JAVA, and I have the impression that JAVA code is very heavy; the registration of a database or loading drivers... seems to take longer than with other languages like VisualBasic or DELPHI.

What is the reality, does the complexity of JAVA code allow for more than with VisualBasic or DELPHI code? I’m not asking this question in general (at least not in this discussion) but regarding a specific topic, namely its relationship with databases.

0
jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention   4 830
 

Java does not allow anything more or less than other languages (in its relationship to databases)


.
Best regards,
Jordane

0
KX Posted messages 19031 Status Modérateur 3 020
 

Hello,

I feel like JAVA code is very heavy, the database recording or the driver loading

First of all, loading the driver (Class.forName) is no longer necessary since Java 6 (released in 2006), so unless you have a very old driver, loading is automatic when the driver is added to the classpath.

Therefore, creating a connection boils down to a single line (example with MySql)

Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "password");

It's hard to make it simpler... Even though, to keep it clean (and Java is quite strict about this), you will also need code to close the connection, handle error cases, etc.


0
toyo2020 Posted messages 75 Status Membre
 

Nice KX this code is lightweight!

0
mariam-j Posted messages 44 Registration date   Status Membre Last intervention   39
 

Hello,
I would like someone to explain to me the necessity and relevance of databases.
It seems to me that by putting the data into a simple file and loading the file, we can do whatever we want with the data, and that with any language.
Thank you for shedding some light.

0
jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention   4 830
 

A simple text file could be considered as a database....

Now, let's talk more about DBMS like mySQL, SQL Server, Oracle, PostgreSQL, Access... etc.....

It’s more than just data storage... it’s also about sorting, searching, filtering methods... functions that allow you to link/cross multiple data together, to search them easily and especially quickly... using a query language...

PS: And can you explain what your question is doing here when it has nothing to do with the initial question?

0
mariam-j Posted messages 44 Registration date   Status Membre Last intervention   39 > jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention  
 

Initial question: Database with Java vs SQL.
My question: Why "database"?
Isn't there a little connection?
Sorting methods, etc... can also be done with code.
If we throw random data into a database, it won't organize itself, will it?

0
jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention   4 830 > mariam-j Posted messages 44 Registration date   Status Membre Last intervention  
 

Initial question: Database with Java vs SQL.
My question: Why "database"?
Isn’t there a little connection?

It is customary to open your own questions ... and not to pollute those of others...

And then.. starting from there ... you could have put it in any programming-related question... since ultimately... everything is programming, right?!

Joking aside,

Sorting methods, etc... can also be done with code.

Yes, but I specified easily and especially quickly... just like you can get to Japan by walking and then swimming ... or use a more suitable means of transportation like a plane...

If we dump data randomly into a database, it won't structure itself, will it?

Well no... that's why you need to think a bit about the design of your database ... and to stay in the absurd ... I’ll turn the question back to you: If you throw random bits of code together .. does that magically turn into software??

Honestly, I don’t know what you’re doing in the "programming" forums... other than trolling...

0
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   1 587
 

A database is software written in code; it's a tool.

For some projects, this tool is neither necessary nor useful.

For some projects, this tool is useful; it makes the work easier.

For some projects, this tool is essential; the project would not be feasible, or would not be completed on time, or would exceed the budget without this tool.

0