How do I connect to a Websphere Datasource with a given JNDI name?

前端 未结 6 1484
温柔的废话
温柔的废话 2020-12-07 23:15

I\'m using Websphere Portal 7.0 and creating a portlet with RAD 8.0. My portlet is trying to make a db2 connection to a remote server. I wrote a java program locally to do a

6条回答
  •  眼角桃花
    2020-12-07 23:49

    Find below code to get database connection from your web app server. Just create datasource in app server and use following code to get connection :

    // To Get DataSource
    Context ctx = new InitialContext();
    DataSource ds = (DataSource)ctx.lookup("jdbc/abcd");
    // Get Connection and Statement
    Connection c = ds.getConnection();
    stmt = c.createStatement();
    

    Import naming and sql classes. No need to add any xml file or to edit anything in project.
    That's it..

提交回复
热议问题