aspose file tools*
The moose likes JSP and the fly likes How to use ConnectionPool in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to use ConnectionPool in jsp" Watch "How to use ConnectionPool in jsp" New topic
Author

How to use ConnectionPool in jsp

krussi rong
Ranch Hand

Joined: Jan 30, 2002
Posts: 62
Hi,
I download a connectionPool, and I want to set
it's scope to application.so every apllication
can get connection from it,I put it on a
application scope bean.It works fine now,also
I can get connection like this:
DataManager dataManager;
dataManager = (DataManager)pageContext.getAttribute("dbCon",pageContext.APPLICATION_SCOPE);
java.sql.Connection con = dataManager.getCon();
the problem is how to keep
a con to a session scope,so every page
can use it? I tried in this way,but it didn't work.
session.setAttribute("dbcon",con);
java.sql.connection con = session.getAttribute
("con");
I don't know why
thanks
Krussi
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
What is your error message?


Seam Framework: http://www.amazon.com/exec/obidos/ASIN/0137129394/mobileenterpr-20/
Ringful: http://www.ringful.com/
prabhat kumar
Ranch Hand

Joined: Apr 11, 2001
Posts: 114
You have to something like this
Connection con = (Connection)session.getAttribute("con");

hopw this will solve ur problem
Alan Williamson
Greenhorn

Joined: Nov 01, 2001
Posts: 14
BTW - For production you will wish to have the database connection in a servlet or bean for security reasons and design. :-)
krussi rong
Ranch Hand

Joined: Jan 30, 2002
Posts: 62
thanks
I tried something about session,I should cast it,
now it works.
But How can I save a application instance. I hope
all my application can use it.
I did like this:
a.jsp
<jsp:useBean id="dataManager" scope="application" class="com.wirelesspay.database.DataManager" />
.......
b.jsp
<%
DataManager dataManager;
dataManager = (DataManager)pageContext.getAttribute("dbCon",pageContext.APPLICATION_SCOPE);
java.sql.Connection con = dataManager.getCon();
%>
.......
the problem is when a.jsp and b.jsp in the same
session, it works. otherwise it doesn't work.
I have no idea, I want every application file
which need connection to database use the same
connection pool,not just the jsp. It seems this
way just for jsp, then what about a noraml class to get the connection.
thanks very much!
Krussi
krussi rong
Ranch Hand

Joined: Jan 30, 2002
Posts: 62
sorry I made a mistake here.
It works fine now.
I should use the same id in a.jsp and b.jsp.
I want every application file which need connection to database use the same
connection pool,not just the jsp. It seems this
way just for jsp, then what about a noraml class to get the connection.
thanks very much!
Alan Williamson
Greenhorn

Joined: Nov 01, 2001
Posts: 14
This is a sample from a class using Oracle 9iAS connection pool.
import java.util.*;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
/**
* This class provides the DB Access.
* @see ConnectionPool
*/
public class DBAccess {
private static String theDataSource = "jdbc/OracleCoreDS";
private static DataSource myDataSource = null;
private static java.sql.Connection myConn = null;
private static String queryStatus="good";
public DBAccess()
{
}
public static Connection getConnection()
{
try {
InitialContext ctx = new InitialContext();
myDataSource = (DataSource)ctx.lookup(theDataSource);
myConn = myDataSource.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return myConn;
}
public static void closeConnection(Connection conn)
{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* This method executes the query passed to it
* @returns the resultset
*/
public static ResultSet executeQuery(String qr)
{
queryStatus = "good";
ResultSet rs = null;
try {
InitialContext ctx = new InitialContext();
myDataSource = (DataSource)ctx.lookup(theDataSource);
myConn = myDataSource.getConnection();
Statement st = myConn.createStatement();
rs = st.executeQuery(qr);
}
catch(Exception e)
{
e.printStackTrace();
queryStatus = e.toString();
}
//System.out.println("Query is : " +qr);
return rs;
}
krussi rong
Ranch Hand

Joined: Jan 30, 2002
Posts: 62
Thanks Alan Williamson very much!
In my way just only jsp can get connection, in
your way just normal class can get connection. so
I am wonder how can I have a module, not only
the jsp but also the normal class can get the
connection.
Thanks
Krussi
Alan Williamson
Greenhorn

Joined: Nov 01, 2001
Posts: 14
I use a servlet that is a controller. The JSP with do a post to this servlet. The servlet will process the request and fetch the data. There is a class called DBAccess. The servlet will use it like:
ResultSet rs = null;
Connection myConn = null;
try
{
DBAccess dba = new DBAccess();
myConn = DBAccess.getConnection();
rs = DBAccess.executeQuery(query1, myConn);
}
catch (Exception exp)
{
exp.printStackTrace() ;
}
Alan Williamson
Greenhorn

Joined: Nov 01, 2001
Posts: 14
I hit the reply button by accident before I was done.
The SQL statement will create a recordset. I then will go through the recordset and load it into an vector. The vector will be placed in a session variable and the controller will redirect to the result JSP page. That page will get the session variable data and load to the page. This is MVC design model.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use ConnectionPool in jsp