Skip to Content
avatar image
Former Member

Tutorials on access the Database for data

Hello,

I wanted few good tutorials, on how do retrieve data from the database or info cubes of BI using the webdynpro technology.

Thanks,

karthik

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Jan 15, 2007 at 04:34 PM

    Sample of connection to a DB

    		public static Connection startDBCon(String datasource_name){
    		
    		 DataSource ds;
    		 InitialContext ctx;
    		 Properties properties;
    		 
    	  try {
    		  datasource_name = "jdbc/" + datasource_name; // + "DB";
    		  String contextUrl = "";
    		  String contextFactory = "com.sap.engine.services.jndi.InitialContextFactoryImpl";
    		  properties = new Properties();
    		  properties.put("java.naming.factory.initial", contextFactory);
    
    		  ctx = new InitialContext(properties);
    		  ds = (DataSource)ctx.lookup(datasource_name);
    		  return ds.getConnection();
    		
    	  } catch (Exception e) {
    //		  e.printStackTrace();
    	  }
    	  
    	  return null;
      		
    	}
    

    usually datasource_name is equal to:

    			  datasource_name = 
    						  "SAP" + 
    						  (String) System.getProperties().get("SAPSYSTEMNAME")
    								  + "DB";

    or

    datasource_name = "SAPSR3DB";

    by the way have to call you system administrator and ask, this datasourcename is configurated under Visual Adminstrator.

    Here some SQL call after got connection:

    	public Rows[] findByDynamicSelect(String sql, Object[] sqlParams) throws YourException
    	{
    		// declare variables
    		final boolean isConnSupplied = (userConn != null);
    		Connection conn = null;
    		PreparedStatement stmt = null;
    		ResultSet rs = null;
    		
    		try {
    			// get the user-specified connection or get a connection from the ResourceManager
    			conn = isConnSupplied ? userConn : ResourceManager.getConnection();
    		
    			// construct the SQL statement
    			final String SQL = sql;
    		
    		
    			// prepare statement
    			stmt = conn.prepareStatement( SQL );
    
    			// bind parameters
    			for (int i=0; sqlParams!=null && i<sqlParams.length; i++ ) {
    				stmt.setObject( i+1, sqlParams<i> );
    			}
    		
    		
    			rs = stmt.executeQuery();
    		
    			// fetch the results
    			return fetchMultiResults(rs);
    		}
    		catch (SQLException _e) {
    			throw new YourException"SQLException: " + _e.getMessage(), _e );
    		}
    		catch (Exception _e) {
    			throw new YourException"Exception: " + _e.getMessage(), _e );
    		}
    		finally {
    			ResourceManager.close(rs);
    			ResourceManager.close(stmt);
    			if (!isConnSupplied) {
    				ResourceManager.close(conn);
    			}
    		
    		}
    		
    	}
    

    example how to call:

    	public Row findByPrimaryKey(String Par1, int Par2, int Par3) throws YourException
    	{
    		Row ret[] = findByDynamicSelect( SQL_SELECT + " WHERE Par1 = ? AND Par2 = ? AND Par3 = ? ", new Object[] { Par1, new Integer(Par2),  new Integer(Par3) } );
    		return ret.length==0 ? null : ret[0];
    	}
    

    after for Row (Resultset) there are method like:

    Row.getInt( COLUMN_FIELD )

    Row.getString( COLUMN_FIELD )

    Row.getDouble( COLUMN_FIELD )

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jan 16, 2007 at 02:31 PM

    Hi,

    Thanks Roberto Tagliento for the code what you had send. I have also award points for it. But id you can suggest me tutorial examples, where i can get from SDN. tut on picking up data from infocubes BI concept . mostly with DB and other good examples.

    Thanks,

    karthik

    Add comment
    10|10000 characters needed characters exceeded