Using a DataSource from WebLogic in a JSP

By: Bruce W. Perry Emailed: 1645 times Printed: 2119 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Example below imports the necessary classes at the top of the code using the page directive and its import attribute. Otherwise, this JSP accomplishes everything that the servlet of the prior recipe does, including the display of nearly identical output in the web browser.

Using a JSP scriptlet to access a WebLogic DataSource
<%@ page import="java.util.Hashtable,java.sql.*,javax.naming.*,javax.sql.*" %>

<html>
<head><title>Database Query in WebLogic</title></head>
<body>
<h2>Querying a database with a JSP in WebLogic</h2>

<%
    Context env = null;

    DataSource pool = null;
         
    Hashtable ht = new Hashtable( );

    ht.put(Context.INITIAL_CONTEXT_FACTORY,
      "weblogic.jndi.WLInitialContextFactory");

    ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
    
    env = new InitialContext(ht);

    //Lookup this DataSouce at the top level of the WebLogic JNDI tree
    pool = (DataSource) env.lookup ("oracle-8i-athletes"); 

    String sql = "select * from athlete";

    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    ResultSetMetaData rsm = null; %>
        
 <table border='1'><tr>

  <% 
     try{   
             //get a java.sql.Connection from the pool
             conn = pool.getConnection( );

             stmt = conn.createStatement( );//create a java.sql.Statement
      
             //execute a SQL statement,generating  a ResultSet
             rs = stmt.executeQuery(sql); 

             rsm = rs.getMetaData( );

             int colCount =  rsm.getColumnCount( );
            
             //print column names
             for (int i = 1; i <=colCount; ++i) { %>
                
               <th><%=rsm.getColumnName(i)%> </th>

          <%  } %>
            
           </tr>
          
          <% while( rs.next( )){ %>
                
                <tr>
                
            <%     for (int i = 1;  i <=colCount; ++i) { %>
                <td>    <%= rs.getString(i) %> </td>
            <%}//for %>  
                </tr>
          <%} //while
           
  } catch (Exception e) {
            
           throw new JspException(e.getMessage( ));
            
  } finally {
            
      try{
                
          stmt.close( );
          conn.close( );
                
      } catch (SQLException sqle){ }
            
  }                 %>

</body>
</html>

After making sure that you have properly configured the connection pool and DataSource in the WebLogic console, view this JSP's output by copying it to WebLogic's default web application, then request a URL in your browser that looks like this one: http://localhost:7001/sqlWeblogic.jsp.


JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

How to connect Oracle java cloud service database(https) connection? could you pls help me??

what code changes should be done in the above code


View Tutorial          By: prakash at 2015-08-27 16:19:04

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Bruce W. Perry
Embedding MP3 audio file in a JSP
Embedding a QuickTime movie in a JSP
Getting HTTP Request Headers in a JSP
Using a DataSource from WebLogic in a JSP
Creating a JavaBean to Connect with Google API
Deploying an Individual JSP on Tomcat
Writing your first JSP page

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
JSP CheckBox Example
JSP Program for display Date
JSP Example to connect to MS SQL database and retrieve records
Sending Email using JSP
What are the different scopes in JSP?
JSP Alert Example
Comparison operators in JSP
Uploading a file to a server using JSP
Uploading an Image to a Database using JSP
Automatically Refreshing a JSP
The JSP Program running first Time.
Embedding java codes in jsp sciptlets
Protecting your website with a login page
Form Object
A simple program using EL in JSP
Most Emailed Articles (in JSP)
Steps to get a Free SSL certificate for your Tomcat
What is JSP?
Retrieving a Portion of a String
Tags using in jsp
Tag libraries
Closing Windows
Radio Object
Frame Object
Math Object
Disabling Scriptlets in JSP using web.xml
Getting HTTP Request Headers in a JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
What is JSP?
Click to Activate and Use this control
Built-in Object in Javascript