• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use ConnectionPool in jsp

 
krussi rong
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your error message?
 
prabhat kumar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to something like this
Connection con = (Connection)session.getAttribute("con");

hopw this will solve ur problem
 
Alan Williamson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic