开发者

SQLJ VS JAVA files

Can开发者_运维技巧 anyone expalain difference between .sqlj and .java files?


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜