By using the DB access class wizard in the standard workbench, DB access processing using JDBC can be generated automatically as a DB access class.
DB access classes deploy search, insertion, deletion, and update processes as methods based on the information specified using the wizard, and deploy the processing target database columns as class fields. Thus, when a DB access class is used, DB access processes are performed by invoking methods while setting values in and fetching values from class fields.
Creating a DB access class
From the [New] wizard, select [Java] > [Source] > [DB Access Class] and use the wizard to create the DB access class. See below for the wizard settings.
Source folder
Specify the folder in which the DB access class source is stored.
Package
Specify the name of the DB access class package.
Name
Specify the DB access class name.
Generate methods for manual commit
Specify whether or not to generate a method for manual commit for the DB access class. If selected, a commit method and rollback method are generated for controlling transactions.
Catch the exception thrown by the DB access
Specify whether or not to catch exceptions (SQLException and similar) within DB access methods.
Connection list box
A list of already created DB connections is displayed. Select one from the list.
Add a connection
This opens a new DB connection wizard.
Connect
Connects to the DB selected in the connection list box.
[Information DB] page
Use the [Add], [Edit], and [Delete] buttons to specify information related to the DB access method definitions.
[DB Access Method] dialog box
Specify the DB access method name, return value type, arguments, and so on.
A list of schemas, tables, and column names in the connected database is displayed at [Database objects]. Specify the processing target table and column.
At [Access type], select the type of database access performed by the method.
At [Condition], specify the conditions to be used when accessing the database. For the conditions, specify the character string after the SQL statement WHERE clause as in the example shown below. Instead of the "?" used to dynamically change a condition, specify the DB access method parameter name with "?" at the start and the end.
(Example) WHERE NAME = ?param1? AND ID = ?param2?
[Field information definition] page
Confirm or change the contents of the DB access method definition.
The [Type] and [Field Name] can be changed.
[Getter] and [Setter] are enabled if [Generate getter/setter methods] was selected.
Information
If the JDBC driver 2.x is being used, the connect method of the automatically generated class must be replaced with the following comment content that is coded in the same source:
// This is the connect method for JDBC2.x connections. // Substitute this for the existing connect method and enter the required items. This enables access to be executed from a data source. //public void connect(java.lang.String userName, java.lang.String passWord) throws javax.naming.NamingException, java.sql.SQLException { // //Connect to the database. // java.util.Hashtable env = new java.util.Hashtable(); // env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "{ initial context factory}"); // env.put(javax.naming.Context.PROVIDER_URL, "{provider URL}"); // javax.naming.InitialContext ctx = new javax.naming.InitialContext(env); // javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup("jdbc/{ data source name}"); // con = ds.getConnection(userName, passWord); // con.setAutoCommit(true); //}
When replacing the comment contents, also correct the coding for the database connection items below.
Item name | Coding example |
---|---|
Initial context factory | com.fujitsu.symfoware.jdbc2.jndisp.SYMContextFactory (Register the JDBC driver in the project build path in advance.) |
Provider URL | SYM://<connection destination address>:<port number> |
Data source name | JDBC/<name registered by data source registration tool> |