0

Can anyone expalain difference between .sqlj and .java files?

Andrew Kozak
  • 1,631
  • 2
  • 22
  • 35
Debuger
  • 9
  • 2

3 Answers3

1

From the wikipedia article:

Whereas JDBC provides an API, SQLJ consists of a language extension. Thus programs containing SQLJ must be run through a preprocessor (the SQLJ translator) before they can be compiled.

aioobe
  • 413,195
  • 112
  • 811
  • 826
0

Wikipedia explains this.

A .java file is a normal Java source code file. An .sqlj file is a Java source code file with embedded SQL statements. You have to pass .sqlj source files through a preprocessor to make normal .java files from them, which you can then compile with a standard Java compiler.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • but if I use Select statement in .java file then i also use SQL statements in .java. Why I need .sqlj? – Debuger May 18 '10 at 08:49
  • You cannot add a `SELECT` statement directly in a Java source file. You'd have to make a `Statement` object, pass the query as a string etc. SQLJ makes it so that you can put SQL statements directly in your source file, without a lot of boilerplate code. The preprocessor will add the boilerplate code for you. Look at the Wikipedia page, it shows an example. – Jesper May 18 '10 at 11:50
  • Can You please say, where in real life we use SQLJ classes and why? In real applicationsor so on? Best regards, Kristaps – Debuger May 18 '10 at 13:47
  • I don't think that in practice there are many people who use SQLJ; I've never encountered it myself in more than 10 years of programming in Java. It's not a widely used way to use SQL from Java. There are other technologies, such as plain JDBC, or ORM frameworks such as Hibernate, which are much more common. – Jesper May 19 '10 at 06:55
  • We had to use it specifically when a database entry was modified and many servers had to be advised of the row change. We used it to send MQ messages through topics. Apart from that, I don't suggest using this strategy. – Pat B Oct 11 '11 at 18:31
0

In normal Java files you use JDBC to interact with the database. Each time you execute a DML (select, insert, delete,...) the sentence should be optimized, analyzed to create the access plan and then, perform the action. When you have to do this many times, it could be expensive to do the same process everytime.

Instead, SQLJ is a combination between Java and SQL. You write your sentences as you do in a database viewer, and at precompile time, the sentences will be analyzed. This means, that the sentences will be static, and a package in the database will be created and will contain the access plan. This also means, that the query is analyzed just once, and compile time, and execute many times.

JDBC is more for dynamic queries, like select that are built given a set of parameters, and each time they are different. SQLJ is for static queries, that has the same structure.

Developing in SQLJ is very easy, and finally, at precompile time, the processor will extract the SQL sentences, create a profile file to bind in the database, and also it will create a set of java files, that will contain JDBC "optimized"

You could mix SQLJ and JDBC in a file, taking the best from each world. For example, declaring a select for update, and then iterate and modify a row is very concise in SQLJ.

It is true that SQLJ is not very popular, but that is the problem when developers only think in their program, without analyzing how to access the data, the developers only write code, and when the programs are released in production, the performance problems are always the same. SQLJ provides a more flexible data access that DBA appreciate a lot.

AngocA
  • 7,655
  • 6
  • 39
  • 55
  • 2
    "not very popular"? SQLJ is completely dead. – skaffman Feb 08 '12 at 14:14
  • It is not dead, the last version of DB2 still use it. I do not know about Oracle, but I am pretty sure it still exist. The problem is that Java developers do not know that, and they think it is obsolete, but it is the only way Java has to create static SQL, or do you know another way? – AngocA Feb 08 '12 at 14:20