Topic: Configure and run SQLJ in JBuilder9

  Print this page

1.Configure and run SQLJ in JBuilder9 Copy to clipboard
Posted by: javaever
Posted on: 2004-01-02 17:38

最近在学SQLJ, 公司用JDEVELOPER, 几乎不用什么配置. 个人却更喜欢JBUILDER, 从学校里一直用到现在. 但是配置就不不如JDEVELOPER方便, HELP FILE, JBUILDER DEVELOPER’S GUIDE也没能提供多大帮助. 但是从里面得到点思路, 试了几次, 也算成功了. 如果版主觉得好, 可不可以给一个积分. 有问题的话, 可以交流. 谢谢!

Assumed Environment:

JBuilder9 Enterprise in c:\JBuilder9
Oracle9.2 in c:\oracle\ora92

-1- Setup Oracle JDBCdriver
----------------------------------------------------------------------------------------
Since SQLJ is built on top of JDBC, we need to set up JDBC driver first.
Otherwise, you will get error messages like
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
"HelloWorld.sqlj": unexpected error occurred...
"HelloWorld.sqlj": java.lang.NoClassDefFoundError: oracle/jdbc/OraclePreparedStatement
"HelloWorld.sqlj": at java.lang.Class.forName0(Native Method)
"HelloWorld.sqlj": at java.lang.Class.forName(Class.java:140)
"HelloWorld.sqlj": at oracle.sqlj.checker.JdbcVersion.getSqljLibraryName(JdbcVersion.java:489)
"HelloWorld.sqlj": at sqlj.tools.Sqlj.checkConfiguration(Sqlj.java:713)
"HelloWorld.sqlj": at sqlj.tools.Sqlj.statusMain(Sqlj.java:284)
"HelloWorld.sqlj": at sqlj.tools.Sqlj.main(Sqlj.java:146)
"HelloWorld.sqlj": Error: Exception caught:
"HelloWorld.sqlj": Warning: Code generator "oracle" cannot be instantiated from class oracle.sqlj.codegen.CodeGenFactory: oracle.jdbc.OraclePreparedStatement.
"HelloWorld.sqlj": Total 1 error and 1 warning.
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

Follow the following steps:

Tools > Enterprise Setup > Database Drivers > Add...
You are prompted a {Select one or more libraries} dialog box, choose [New...]
The wizards appears, enter a valid name, say, OracleJDBC,
Location field can be any chooseable. Here, I chose [User Home]
In the [Libraries path] pane, click [Add...] button
Choose [classes12.jar] under [c:\oracle\ora92\jdbc\lib]
Click [OK] all the way to finish this Oracle JDBC driver setup.

You will be prompted to restart JBuilder to take effect the setting, so please do that.

-2- Setup Oracle SQLJ
----------------------------------------------------------------------------------------
Now, JBuilder is restarted, set the SQLJ.
Tools > Enterprise Setup > SQLJ > Oracle
For the SQLJ executable field, click the eclipse button
Choose [sqlj.exe] under [c:\oracle\ora92\bin]
Leave the [Additional options] field blank.
For the [Libraries] pane, click [Add..]
You are prompted {Select a library} dialog box, choose [New...]
Give it a valid name, say, OracleSQLJ, under Location, say, User Home
Fot the [Library paths],
choose [c:\oracle\ora92\sqlj\runtime12.jar], [c:\oracle\ora92\sqlj\translator.jar]
Click [OK]
Then choose both <Oracle JDBC> and <Oracle SQLJ> under <User Home>
Click [OK] to close the {Select a library} dialog box.
Click [OK] to close the {Enterprise Setup} dialog box.

-3- Test sample HelloWorld.sqlj
---------------------------------------------------------------------------------------
It is ready now for you to create and run your SQLJ program.
I attach a sample file extracted from <<Java Programming with Oracle SQLJ>> by J Price
Create a new project
File > New Project
{Project Wizard} dialog box appears (3 steps)
Give a project name and a directory, say, test.jpx under d:\sqljcode\
Click [Next] to proceed
click the [Required Libraries] tab, click [Add...]
Choose the <OracleJDBC> and <OracleSQLJ> under [User Home]
Click [Finish] to finish creating a project.
Alternatively, you can click [Next] to do additional setting. It is irrelevant here, so I ignore the last step.

Create the sqlj file now.
Right click the project you created just now
Choose [New] > [File...]
The {Create New File} dialog box appear
For this sample, name it HelloWorld (case-sensitive), choose <sqlj> for the [Type] drop-down list
Check the <Add saved file to the project> checkbox. click [OK] to dismiss the dialog box.

Copy and paste the code in <HelloWorld.sqlj> editor.

We are nearly there, HOWEVER, it is very important to set Oracle translator when you build the sqlj file.
Otherwise you cannot make the file.
Digressional word: you won't see <Make>, <Rebuild> menu item on the context menu of the project containing sqlj file. Try and see.
Project > Project Properties...
click [build] tab and then [General] tab
Choose [Oracle] for the [SQLJ translator] drop-down list
click [OK] to close the dialog box.

Now you can make the project by right click the project node, choose [Make].
The [HelloWorld.java] will be created under the [HelloWorld.sqlj] node.
Then you can run the generated Java file.

Right, here we are.

Final words: Step 1 and 2 can be done in one go without shut JBuilder down and turn it back on.
However, we still need to restart JBuilder once we do step 1 and 2 together.

If you need to code SQLJ a lot, you can set the
Project > Default Project Properties...
click the [Required libraries] tab and [Add...]
Add the <OracleSQLJ> and <OracleJDBc>
click [OK] to dismiss the window.

Should you have any questions on the setup, please feel free to ask.

THANK YOU!!!

--------------------------------------------------------------------------------------
Sample HelloWorld.sqlj code
--------------------------------------------------------------------------------------

<code>
/*
The program HelloWorld.sqlj illustrates how to connect to a
database, and display the words "Hello World" along with
the current date.
*/

// import required packages
import java.sql.Date;
import java.sql.SQLException;
import oracle.sqlj.runtime.Oracle;

public class HelloWorld {

public static void main(String [] args) {

java.sql.Date current_date;

try {

// connect to the database, PLEASE change as necessary
Oracle.connect(
"jdbc:oracle:thinAngrylocalhost:1521:homedb9i",
"scott",
"tiger"
);

// get the current date from the database
#sql { SELECT sysdate INTO :current_date FROM dual };

// display message
System.out.println("Hello World! The current date is " +
current_date);

} catch ( SQLException e ) {
System.err.println("SQLException " + e);
} finally {
try {

// disconnect from the database
Oracle.close();

} catch ( SQLException e ) {
System.err.println("SQLException " + e);
}
}

} // end of main()

}

</code>
-----------------------------------------------------------------------------------

2.Re:Configure and run SQLJ in JBuilder9 [Re: javaever] Copy to clipboard
Posted by: bluepure
Posted on: 2004-01-02 18:49

3ks!!!

学习ing...

现在在用jdeveloper,已经适应了。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923