Why and when to perform this task
This article describes how JDBC applications, comprised of bean-managed persistence (BMP) entity beans, session beans, or servlets, are converted to SQLJ applications. It also describes how the SQLJ applications are then deployed in WebSphere Application Server.Follow these steps precisely and in the right order to ensure a correct conversion:
Steps for this task
Now when you run the sqlj tool in the next step, the .java file that it creates will have the same name as your old .java file, providing you with a seamless transition to the SQLJ technology.
For example, convert the following JDBC operation:
Connection con = dataSource.getConnection(); Statement stmt = con.createStatement(); stmt.execute("INSERT INTO users VALUES (1, 'user1')"); con.commit();to the following SQLJ:
// At the top of the file and just below the import statements, define Connection_Context #sql context Connection_context; . . Connection con = dataSource.getConnection(); . . Connection_context ctx1 = new Connection_context(con); . . #sql [ctx1] {INSERT INTO users VALUES (1, 'user1')}; . . con.commit(); ctx1.close();
sqlj -db2optimize SQLJTransactionTest.sqlj db2sqljcustomize -url jdbc:db2://localhost:50000/jtest -user dbuser1 -password dbpwd1 SQLJTransactionTest_SJProfile0.ser
utx.begin(); cons =ds.getConnection( request.getParameter("db.user"), request.getParameter("db.password")); cmctx1 = new CM_context(cons); #sql [cmctx1] {DELETE FROM cmtest WHERE id=1}; utx.commit(); //The next statement verifies the result: #sql [cmctx1] cursor1 = {SELECT id, name FROM cmtest WHERE id=1};In this case, the Select statement elicits an object closed exception. To prevent the exception from occurring, close the connection before committing the transaction. Then get a new connection and a new context before running the Select statement.
Related concepts
Embedded Structured Query language in Java (SQLJ) support
Tips for developing entity beans
Exceptions pertaining to data access
Related tasks
Developing data access applications
Related reference
Data access bean types