How to access  database connection from datatsource in a standalone java ap

My java standalone application wants connection to database with lookup to datasource name which is configured in the oc4j app server.
Please guide me with some ideas to acheieve this.

user510497,
Following sample program requires access to classes in "oc4j.jar" file.
Test environment:
* Server:
+ OC4J stand-alone 10.1.2.0.2
+ Red Hat Enterprise Linux AS release 3
+ Java HotSpot(TM) Client VM (build 1.4.2_08-b03, mixed mode)
+ Oracle Database 10g Release 10.1.0.4.0 - 64bit Production
* Client
+ Microsoft Windows XP Professional Version 2002 Service Pack 2
+ Java HotSpot(TM) Client VM (build 1.5.0_08-b03, mixed mode, sharing)
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DataSrc2 {
  public static void main(String[] args) {
    Connection conn = null;
    Properties l_props = new Properties();
    l_props.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.evermind.server.rmi.RMIInitialContextFactory");
    l_props.put(Context.PROVIDER_URL, "ormi://your_OC4J_host/");
    l_props.put(Context.SECURITY_PRINCIPAL, "your_OC4J_user");
    l_props.put(Context.SECURITY_CREDENTIALS, "your_OC4J_password");
    Context l_jndiContext = null;
    try {
      l_jndiContext = new InitialContext(l_props);
      Object l_obj = l_jndiContext.lookup("default-data-source");
      DataSource ds = (DataSource) l_obj;
      conn = ds.getConnection("scott","tiger");
      DatabaseMetaData dbmd = conn.getMetaData();
      System.out.println(dbmd.getDatabaseProductVersion());
    catch (NamingException x_naming) {
      x_naming.printStackTrace();
    catch (SQLException xSql) {
      xSql.printStackTrace();
    finally {
      if (l_jndiContext != null) {
        try {
          l_jndiContext.close();
        catch (NamingException x_naming) {
          System.err.println("Failed to close JNDI context.");
          x_naming.printStackTrace();
      if (conn != null) {
        try {
          conn.close();
        catch (SQLException xSql) {
          System.err.println("Failed to close [database] connection.");
          xSql.printStackTrace();
}NOTES:
1. Datasource lookup name is value of "default-data-source" attribute of "orion-application" element in file:
<OC4J_HOME>/j2ee/home/config/application.xmlGood Luck,
Avi.

Similar Messages

  • How  to access database in Sql Server 7.0 using Java

    I am trying to access a database in SQL Server 7.0 using JDBC.But it is giving me errors.I have used following statements to connect the database
    private String URL = "jdbc:odbc:DatabaseName";
    private String userName="user";
    private String passWord="password";
    Class.forName( "sun.jdbc.odbc.SQLServerDriver" );
    connection = DriverManager.getConnection( URL,userName,"passWord" );
    where "username" and "password" are user Name and password respectively to access the database in SQL Server 7.0.

    I am sorry but I have written like;
    private String URL = "jdbc:odbc:DatabaseName";
    private String userName="user";
    private String passWord="password";
    Class.forName( "sun.jdbc.odbc.jdbcOdbcDriver" );
    connection = DriverManager.getConnection( URL,userName,"passWord" );
    where "username" and "password" are user Name and password respectively to access the database in SQL Server 7.0. And I have used the same name in "ODBC data source" as that of the database.I am trying some examples from book and these examples just work fine with MS Access( I created those databases in Access and tried the examples) but not with SQL Server 7.0. Do I need a separate driver or something?

  • How to get total number of Database connections from Java code

    Hi,
    I am using Myql4.1.12.
    I am conncting to mysql through Java.
    I want to know how many active database connections were running at a specific time. Is it possible?
    Can you please help me.
    Thanks in advance.

    Seems like this would be a question for MySql, not for Java. And if it's possible you'd run some MySql utility to do that, not code it in a Java app.

  • Error While creating new database connection from HFM workspace

    Hi,
    We have recently installed HFM and was trying to create database connection from database connection manager in HFM workspace.
    I am getting below error:
    "Error connecting to database connection : no HssJNIDriver950 in java.library.path" initially and later
    "Error connecting to database connection : com/hyperion/ap/adm/HssConn".
    Any information in this direction would helpful.
    Thanks,
    Bhargav
    Edited by: bhargavr on Jan 24, 2011 6:55 PM

    Hi bhargavr,
    We came accross the same issue. Try the following solution below for this 'KNOWN ISSUE' and let me know how you get on. Drop me a note if you have any questions regarding this post.
    Error: *"Error connecting to database connection: com/hyperion/ap/adm/HssConn" While Creating a Financial Management Database Connection* [ID 1102697.1]
    Modified 04-JAN-2011 Type PROBLEM Status PUBLISHED
    Applies to:
    Hyperion BI+ - Version: 11.1.2.0.00 and later [Release: 11.1 and later ]
    Microsoft Windows x64 (64-bit)
    Symptoms:
    You are running the Financial Reporting Web Server on a 64-bit server.
    When you try to create a new Financial Management (HFM) database connection using the Database Connection Manager in Workspace you receive the following message:
    “Error connecting to database connection <connection_name>: com/hyperion/ap/adm/HssConn”
    You can create a database connection in Financial Reporting Studio. When you refresh a report with a Financial Management database connection, you receive one of the following messages:
    “1001: Error connecting to database connection <DATABASE_CONNECTION_NAME>: com/hyperion/ap/adm/HssConn”
    or
    “1001: Error connecting to database connection <DATABASE_CONNECTION_NAME>: no HssJNIDriver950 in java.library.path”
    Cause:
    The issue is documented in unpublished bug 9537050, and in Known Issues in the 11.1.2 Reporting and Analysis Readme. Cross-reference unpublished bug 9707353.
    Solution:
    In the Windows registry, take the following steps:
    Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\HyperionSolutions\FinancialReporting0\HyS9FRReports.
    For each of the following entries, prefix the contents with text in the blue box: Env2, JVMOption13, and JVMOption17 (note that the JVMOption numbers might be slightly different in your environment).
    C:\Oracle\Middleware\EPMSystem11R1\common\ADM\11.1.2.0\bin-64
    If your EPM_ORACLE_HOME is not located at C:\Oracle\Middleware\EPMSystem11R1, replace that location with your location.
    Restart the server.
    G'Luck,
    -David

  • When and How to close database connection in JSP?

    Hi there,
    I am using MySQL and JDBC 3.0, in my system, When and How to close database connection in JSP?
    Thanks in advance.
    Lonely Wolf
    <%@ page session="true" language="java" %>
    <jsp:include page="checkauthorization.jsp" />
    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
    <%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
    <%--
    Execute query, with wildcard characters added to the
    parameter values used in the search criteria
    --%>
    <sql:query var="availablecomputerList" dataSource="jdbc/Bookingcomputer" scope="request">
    SELECT * FROM computer where status=0
    order by s_code
    </sql:query>
    <html>
    <head>
    <title>Search Result</title>
    </head>
    <body bgcolor="white">
    <center>
    <form action="checkin.jsp" method="post">
    <input type="submit" value="Back to Check-in Page">
    </form>
    <c:choose>
    <c:when test="${availablecomputerList.rowCount == 0}">
    Sorry, no available computer found.
    </c:when>
    <c:otherwise>
    The following available computers were found:
    <table border="1">
    <th>Computer</th>
    <th>Description</th>
    <th>Status</th>
    <c:forEach items="${availablecomputerList.rows}" var="row">
    <tr>
    <td><c:out value="${row.s_code}" /></td>
    <td><c:out value="${row.description}" /></td>
    <td><c:out value="${row.status}" /></td>
    </tr>
    </c:forEach>
    </table>
    </c:otherwise>
    </c:choose>
    </center>
    </body>
    </html>

    when should you close the connection? when you're done with it.
    how should you close the connection? like this: conn.close();
    that said, doing this in a JSP page is bad form and not recommended
    JSP's typically don't contain ANY business or data logic

  • Please Help me in Labview and MS Access Database Connective

    Please Help me in LabVIEW and MS Access Database Connective through an example . I am new to LabVIEW .I need simple example that take a data from a table and display it on front panel .
    thanks in advance .

    duplicate post

  • How to get database connection in applet

    Hi,
    I am trying to prepare database connection in applet. After preparing connection with database it'll read same values from table.
    At the time of development it works fine. I have used esclipse IDE for coding and testing.
    But when I try to call that applet from browser. It is giving ClassNotFound exception.
    Does anybody know How to get database connection in applet ?
    Please help me if anybody know solution for this.
    Thanks,
    Rajesh

    As per my knowledge is conserned
    1 u can get the database connection in a jsp page and u send the result set as param to the applet and u can use retrieved values as if they were of the same applet if u r interested i can send the db connetion coding for jsp my id [email protected]

  • How to access the Oracle from Unix when SID is not provided?

    How to access the Oracle from Unix when SID is not provided.
    User Name : xxx
    Password : yyy
    SID : SID
    I use to access it using below command from Unix prompt:
    sqlplus xxx/yyy@SIDNow since I dont have SID, how to access the Oracle DB from Unix Box?
    User Name : xxx
    Password : yyy
    Service Name : zzz
    Regards,
    Sunny.

    Hi Ajay
    Thanks for the suggesstion :)..It worked as well.
    The Actual problem was in the "tnsnames.ora" file where in I need to make entry for the new SERVICE name.
    Now I am able to connect it properly.
    Regards,
    Sunny.

  • Problem in getting the database connection from a connection pool

    Hai All,
    I am facing a problem in getting the database connection from a connection pool created on weblogic server 8.1.
    I am using the Oracle database 8.1.7.
    I have configured my connection pool, datasource and JNDI in weblogic.
    In my java program i have the following code to retrieve the connection.
    import java.sql.*;    
    import java.util.Hashtable;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    class jdbcshp1 {
        public static void main(String[] args) {
         Connection connection = null;
         try {
               Hashtable ht = new Hashtable();
               ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");  // Wanna get rid of this.
               ht.put(Context.PROVIDER_URL,"t3://localhost:7001"); // wanna get rid of this.
               // Get a context for the JNDI look up
               Context ctx = new InitialContext(ht);
            javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup ("myjndi1");
              //Create a connection object
              connection = ds.getConnection();
         The above code is working fine but, the two ht.put statements are creating problem.
    The problem is, after converting the application into WAR file it can be deployed
    on any machine or different port on same machine. My application fails if its deployed on
    weglogicserver which is at different port.
    Is there any way that i can get rid of those ht.put statements or any other way to solve the problem.
    any help is appreciated.
    Thanks in advance
    Pooja.

    Hai All,
    Firstly, thanks for ur reply.
    Even i have seen some code which uses context constructor with out any parameter and works fine.
    i dont understand why its not working for my code.
    When i remove those ht.put code and use context constructor with out any parameter, it giving an error.
    Context ctx = new InitialContext();
    javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup ("ocjndi");
    connection = ds.getConnection();The error is as follows:
    javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
    the above error is forcing me to include those code but if the port number is changed the code will not work. Plz let meknow if some setting have to be made.
    I appreciate all ur valuable help.
    Thanks once again.
    Pooja.

  • How to Access Maintenance View from a program

    Hi Experts,
    I have created a maintenance view for a table. i want to insert data's into the table through the maintenance view. I don't know how to access the view, how to insert data into the Maintenance view. Please suggest me how to access the view from the program.
    with regards,
    James...
    Valuable answers will be rewarded...

    Hi,
    What is the need to search for a program.
    Use transaction SM30 or SE16 to create or maintain entries in the table.
    e.g. if the Maintenance view is created on table A, then Goto transaction SM30 or SE16 and give the table name as A and proceed. This will automatically take u to the maintenance view on tha table A.
    Regards,
    Himanshu

  • How to create database link from oracle to sql server

    Please help with how to create database link from oracle to sql server
    Best regards,
    Vishal

    Please help with how to create database link from oracle to sql server
    Best regards,
    Vishal
    Hi Vishal,
    I found a lof of information regarding how to create a database link from Oracle to SQL Server, please see:
    https://www.google.co.in/?gws_rd=cr&ei=vd3XUvGFO8TgkAXqlYCADg#q=how+to+create+database+link+from+oracle+to+sql+server
    We discuss SQL Server related issue in this forum. If you have any more question regarding Oracle, please post it in Oracle communities forum for better support.
    Regards,
    Elvis Long
    TechNet Community Support

  • How to access Oracle 8g from SQL Server 2005 SP3 EE 64 Bits ?

    Dear All,
    How to access Oralce 8g from SQL Server 2005 SP3 EE 64 Bits (liked server).
    Best regards,
    Miguel Gavinhos

    Hi!
    For the Oracle version is 8.1.6. what should I install, to access Oracle trought a linked server.
    Best regards
    Miguel Gavinhos

  • How to configure BT connection from PDA?

    Hello :)
    PDA cannot find the ActiveSync service, though Windows Mobile Device Center 6.1 is running, and COM port is configured appropriately.
    PDA HTC P3300 Artemis-WinMob 5.0, USB Bluettoth adapter, last version 7.10.10
    in IVT BlueSoleil works fine
    Manual *"How to configure a connection from the PDA (Windows Mobile 6) to the Windows Mobile Device Center"* not help
    Windows 7 64bit

    My HTC Windows Mobile 6.1 phone (not your model) in combination with Windows7 x64 and Windows Mobile Device Center works just fine for me with Bluetooth stack v7.10.10 (T). Ensure that you use the suitable x64 bit version of the Windows Mobile Device Center.
    Here are my links:
    http://www.microsoft.com/windowsmobile/en-us/downloads/microsoft/device-center-download.mspx
    http://www.microsoft.com/downloads/details.aspx?FamilyId=4F68EB56-7825-43B2-AC89-2030ED98ED95&displaylang=en

  • How to move database objects from one user to another user

    Could someone help how to move database objects from one user to another user
    Thanks in advance

    Thanks a lot for the reply.
    Actually I wanted to know whether there is any command to change the owner ship of the table.
    For ex table1 belongs to user1. can the owner of table1 to be changed to owner2.
    directly using an oracle command. I knew that it can be done using import and export, since the table was 200 million thought the rename command would be faster instead of import and export.
    Thanks Billy for letting me know that it is not possible using oracle query.

  • How to access the cloud from my pc?

    how to access the cloud from my pc?

    Go to Verizonwireless.com, log in to your account (or register your phone number if you haven't done so).  Once logged in, Go to My Verizon, My Device, Backup my contacts.  That should bring you to your Cloud "dashboard" - where you can view Contacts, Photos, Videos, Music,Documents, and anything else  you've sent to Verizon Cloud.
    If this doesn't work for you, then where does it break down?  Do you get any error messages?

Maybe you are looking for