Get Data Moving 1
Results 1 to 1 of 1

Thread: Working with JDBC and WebSphere

  1. #1
    Costi Zaboura is offline World Wide Support Manager
    Join Date
    Aug 2006
    Posts
    300
    Rep Power
    14

    Working with JDBC and WebSphere

    DESCRIPTION
    Following is a step by step tutorial on how to work with Attunity's JDBC driver and WebSphere.

    STEP 1 - Create a new J2EE Project
    1.1. Create a new J2EE Project as follows:
    • Go to the menu File\New\Project.
    • Select J2EE/Enterprise Application Project, and click Next.
    • Select the default option “Create J2EE 1.3 Enterprise Application project”, and click Next.
    • Enter the project name, e.g: “AttunityJdbcDemo”.
    1.2. Create deafult modules
    • Click on “New Module...“.
    • Make sure the “Create default module projects” is checked.
    • Make sure to check the “Web projects”, and rest of the options to be unchecked, for this demo.
    • Click Finish, to create the template project.
    1.3. EAR Module Projects
    • Make sure the module created in the previous step is checked (AttunityJdbcDemoWeb).
    • And click Finish, to complete the project creation.
    STEP 2 - Write servlet code

    2.1. Switch to the Web Perspective by clicking on the “Open Perspective“, OR from the menu Windows/Open Perspective. And clicking on “Web“.
    Note: If the option “Web“ is not displayed, click on “Other...“, choose “Web“ and click “OK“.
    2.2. Creating a new Serlvet:
    • Expand the “AttunityJdbcDemoWeb“ project.
    • Right-Click on “Java Resources“.
    • Click on New, and choose “Servlet“.
    • Enter the class name, for example: “AttuJdbcDemo“.
    • Click Finish to create the Servlet (AttuJdbcDemo.java).
    Note: Alternatively, you can click Next, and specify otehr advanced options.
    2.3. The example's code:
    • Copy and paste the following code to the newly created servlet:
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;

    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.servlet.Servlet;
    import javax.servlet.ServletException;

    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.sql.DataSource;

    /**
    * @version 1.0
    * @author
    */
    public class AttuJdbcDemo extends HttpServlet implements Servlet
    {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
    {
    doPost( req, resp);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
    {
    Connection conn = null;
    try{
    //JNDI
    Context aContext = new InitialContext();
    DataSource aDataSource = (DataSource)aContext.lookup("jdbc/attunityDemo");
    conn = (Connection)(aDataSource.getConnection());
    //JNDI end

    Statement stmt = conn.createStatement();
    String sSQL = "select * from disam:nv_dept;";

    System.out.println("Execute Query...");
    ResultSet result = stmt.executeQuery(sSQL);

    while (result.next()) {
    String name_str = result.getString(2);
    System.out.println("name = " + name_str);
    }

    if(conn != null)
    {
    conn.close();
    System.out.println("Successfully closed");
    }
    }
    catch (Exception e)
    {
    System.out.println(e);
    }

    }
    }

    STEP 3 - Configure the Server and Deployment
    3.1. Creating a Test Server
    Now, we have to create a test server to run the sample application:
    • Switch to the Server perspective.
    • Right-Click on the “Servers“ entry.
    • Click on New, and choose “Server and Server Configuration“.
    • Enter a name for the Test server, for example: “AttunityDemoServer“.
    • Select “WebSphere version 5.1” and “Test Environment“.
    • Click Finish.
    3.2. Add the Web Project to the Server
    • Right-Click on the server you have just created “AttunityDemoServer”.
    • Choose “Add and remove project“.
    • Select the J2EE project you created “AttunityJdbcDemo”.
    • Click “Add”, to add it to the server.
    • Press Finish.
    3.3. Add the Attunity JDBC Provider
    • Double click on the new server “AttunityDemoServer” to open Server Configuration view.
    • Click on the DataSource tab.
    • Click “Add” in the “JDBC provider list“, to create a new JDBC Provider.
    • Choose “User-defined“ as the Database type, and “User-defined JDBC provider” as JDBC provider type. And click Next.
    • Enter a name for your JDBC provider, for example: “AttunityJdbcDriver”.
    • Enter a description, e.g: “Attunity JDBC Provider“.
    • Enter the implementation class name, e.g: “com.attunity.jdbc.NvConnectionPoolDataSource “.
    • Click “Add External JARs” to add your JDBC driver JAR files.“/nvjdbc2.jar, /nvapispy2.jar, /nvlog2.jar”.
    • Click Finish.
    3.4. Create the DataSource
    • Click “Add” in the Data sources table.
    • In the "Create a Data Source" dialog, select “User-defined JDBC provider" and "Version 5.0 data source", and click Next.
    • Enter a name for the data source, e.g: “AttuJdbcDS“.
    • Enter a JNDI name, e.g: “jdbc/attunityDemoDS”.
    • Choose “com.ibm.websphere.rsadapter.ConnectJDBCDataStoreH elper “ for the Data Source helper class name.
    • Accept the defaults for the other configurations values, click Next and Finish.
    3.5. Create JDBC properties for the DataSource
    • Click “Add” next to the Resource properties defined in the data source above.
    • Add the properties needed to connect to the defined data source:

    “serverName” (type: java.lang.String, value: localhost)

    “portNumber”, (type: java.lang.Integer, value: 2551)

    • Click on “Save”, to save the server configuration.
    • Close the Server Configuration editor.
    3.6. Define Resource References
    At deployment time, each component must define in its deployment descriptor all the resource names it expects to find in the local JNDI namespace: EJB homes, data sources, mail providers, Java Messaging Service queues and more. Data sources and other J2EE resources need to be defined by what are called resource references.
    • Go to the Web perspective.
    • Expand the “AttunityJdbcDemoWeb” and Double-Click the Web Deployment Descriptor (to open its editor).
    • Select the References tab at the bottom of this editor.
    • And then select the Resource tab at the top.
    • Click Add, define the JNDI name you are using (e.g: “jdbc/attuJndi”).
    • Click on Browse for the Type, and specify javax.sql.DataSource and click OK.
    • Choose “Application” for the authentication mode.
    • Choose the Isolation level.
    • At the Websphere bindings, choose the JNDI name for this resource (in our case, we are testing the local JNDI name, meaning it is the same JNDI name we gave before, “jdbc/attuJndi”).
    • Save the changes.
    3.7. Start the server
    • Click on the “Server perspective“.
    • Right-Click the Server name.
    • Click on Start.
    STEP 4 - Run the test
    • Right click on AttunityJdbcDemo.java.
    • Select the "Run on Server option.".
    • Choose “Use an exsisting server” and the server you created, e.g: “AttunityDemoServer”.
    • Click Finish to run the test.
    NOTE
    • Use the Universal Test Client to check your defined JNDI name. Open a Web browser and type http://localhost:9080/UTC/ . Click on JNDI Explorer, and expand the JDBC tree to see the names defined in the test environment.
    • For this tutorial to work, you should use at least p003 of the ThinClient.
    JNDI Tutorial
    http://java.sun.com/products/jndi/tutorial/index.html
    Last edited by Costi Zaboura; 11-02-2006 at 06:38 AM.
    To Find Out more About Attunity Technology:
    Attunity
    or:
    Contact Us

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •