Hitachi

Hitachi Navigation Platform Development Guide


4.4.5 Implementing database connection processing

The following indicates the conditions for developing plugins to be connected to a database:

If you are developing plugins to be connected to a database, the following implementations are recommended:

Organization of this subsection

(1) Example of implementing plugin initialization processing

  // Store the data source in the static member variable so that the data source can be acquired from the inputFromNode method.
  private static DataSource mDs;
 
  @Override
  public void init() throws UCNPPluginUserException {
    try {
      // Data source lookup processing must be performed within the init method.
      InitialContext ic = new InitialContext();
      DataSource ds = (DataSource) ic.lookup(
          "java:comp/env/jdbc/TP_Connector_for_HiRDB_Type4"); 
      mDs = ds;
    } catch (NamingException e) {
      // If lookup processing fails, UCNPPluginUserException must be thrown.
      UCNPPluginUserException ue = new UCNPPluginUserException(
          "Lookup processing failed.", e);
      throw ue;
    }
  }
 
  public static DataSource getDataSource() {
    return mDs;
  }

(2) Example of implementing the outputToNode method

  public Map<String, Object> outputToNode(HttpSession session, Map<String, Object> param){
    Map<String, Object> map = new HashMap<String, Object>();
 
    DataSource ds = PluginInitializer.getDataSource(); // Acquire the instance
    Connection con = null;
    PreparedStatement statement = null;
    String name = null;
    try {
      // Establish a connection
      con = ds.getConnection();
 
      // Execute SQL
      // The outputToNode method must perform Reference processing
      statement = con.prepareStatement("SELECT NAME FROM TP_TBL");
      statement.setString(1, uid);
      ResultSet set = statement.executeQuery();
 
      // Obtain results
      if (set.next()) {
        name = set.getString(1);
      }
      Map<String, String> rtnParm = new HashMap<String, String>();
      rtnParm.put("outputParam1", name);
      map.put("ucnp.next.params.map ",rtnParm);
    } catch (SQLException e) {
       // Store the error information in response and interrupt transition.
       map.put("ucnp.error.message","An error occurred during DB access.");
       map.put("ucnp.error.type","NG");
    } finally {
      if (statement != null) {
        try {
          statement.close();
        } catch (SQLException e) {
        }
      }
      if (con != null) {
        try {
          con.close();
        } catch (SQLException e) {
        }
      }
    }
    return name;
  }