Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp with beans not handling

 
Shehzad Khan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone
please help!!
i have tried a lot to do everything but couldn't succeed in calling data values from beans class which is calling the values from database
whole code of ConnectionBean.java and conference.jsp is attached.
please please anybody help me in running this code
the error that usually comes is "cannot create bean of ConnectionBean class"
and the root cause is "cannot find connection bean class"
i want to tell that i have tomcat server and the path for both .java and .jsp is as follows:
d:\jakarta-tomcat-4.0-b5\webapps\examples\web-inf\classes\babaji\ch_7\ConnectionBean.java
d:\jakarta-tomcat-4.0-b5\webapps\examples\jsp\conference.jsp

//conference.jsp starts from here
<%@ page import="java.sql.*,babaji.ch_7.*"%>
<jsp:useBean id="connection" class="ConnectionBean" scope="session"/>
<html>
<body>
<center>
<font size="+2" face="arial" ><b>Conference Registration</b></font>
<form action="shuttle.jsp" method="post">
<table border="1" bgcolor="tan" width="50%" align="center">
<tr><td><table border="0" bgcolor="white" cellspacing="0" width="100%">
<tr bgcolor="tan">
<th> </th><th>City</th><th>Tickets Remaining</th></tr>
<%
String sql = "select * from conferences";
ResultSet results = connection.executeQuery(sql);
while(results.next())
{
if(results.getInt("seats") > 0)
{
%>
<td>
<input type="radio" name="show" value="<%= results.getString("ID") %>"</td>
</td>
<%
}
%>
<td><%= results.getString("city") %> </td>
<td align="center"><%= results.getString("seats") %>
</td>
</tr>
<%
}
%>
</table>
</td></tr></table>
<p>
<input type="submit" value="Next Generation (choose shuttle)">
</form>
</center>
</body>
</html>
//conference.jsp ends here

//ConnectionBean.java starts from here
package babaji.ch_7;
import java.sql.*;
import javax.servlet.http.*;
public class ConnectionBean implements HttpSessionBindingListener
{
private Connection con;
private Statement stat;
private static final String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
private static final String dbURL = "jdbc dbc roject4";
public ConnectionBean()
{
try
{
Class.forName(driver);
con = DriverManager.getConnection(dbURL);
stat = con.createStatement();
}
catch(ClassNotFoundException e)
{}
catch(SQLException e)
{}
}
public void commit() throws SQLException
{
con.commit();
}
public void rollback() throws SQLException
{
con.rollback();
}
public void setAutoCommit(boolean autoCommit) throws SQLException
{
con.setAutoCommit(autoCommit);
}
public ResultSet executeQuery(String sql) throws SQLException
{
return stat.executeQuery(sql);
}
public int executeUpdate(String sql) throws SQLException
{
return stat.executeUpdate(sql);
}
public void valueBound(HttpSessionBindingEvent event)
{
System.err.println("ConnectionBean: in the valueBound method");
try
{
if(con == null || con.isClosed())
{
con = DriverManager.getConnection(dbURL);
stat = con.createStatement();
}
}
catch(SQLException e)
{
con = null;
}
}
public void valueUnbound(HttpSessionBindingEvent event)
{
try
{
con.close();
}
catch(SQLException e)
{}
finally
{
con = null;
}
}
protected void finalize()
{
try
{
con.close();
}
catch(SQLException e)
{}
}
}
//ConnectionBean.java ends here

please everybody, i am anxiously waiting for the positive reply; as i have heard a lot about goodness of java ranch, so referring here today for the first time
i am scjp certified and want to take scwcd exam soon
ok take care
bye
from Shehzad Khan
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is an idiosyncrasy of JSP that you MUST give a complete package for the bean class in the useBean tag. SO - instead of
<jsp:useBean id="connection" class="ConnectionBean" scope="session"/>
You must use:
<jsp:useBean id="connection" class="babaji.ch_7.ConnectionBean" scope="session"/>
It has to do with the way JSP creates the bean, using the class string and ignoring the import attribute.
Bill
 
Shehzad Khan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone
thanks sir bill, for your valuable information
it really works and ConnectionBean class is picked up gracefully by conference1.jsp. but there is now another problem that emerged. now the error "java.sql.SQLException: no data found" arised.
what to do? please help. my database name is ppp.mdb and it is placed in d:\jakarta-tomcat-4.0-b5\examples\jsp\ppp.mdb
in here, i have two tables with the name a)conference and b)shuttles
in the conference table, i have four field which i am writing in the following order: field_name:field_type
1)ID:integer
2)city:String
3)airport:String
4)seats:integer
the ConnectionBean class code is the same that i have posted previously
however i have changed conference1.jsp a bit.
so the code of conference1.jsp is pasted here.
//conference1.jsp starts from here
<%@ page import="java.sql.*"%>
<jsp:useBean id="connection" class="babaji.ch_7.ConnectionBean" scope="session" />
<html>
<body>
<center>
<font size="+2" face="arial" ><b>Conference Registration</b></font>
<form action="shuttle.jsp" method="post">
<table border="1" bgcolor="tan" width="50%" align="center">
<tr><td><table border="0" bgcolor="white" cellspacing="0" width="100%">
<tr bgcolor="tan">
<th> </th><th>City</th><th>Tickets Remaining</th></tr>
<%
String sql = "select * from conference";
ResultSet results = connection.executeQuery(sql);
while(results.next())
{
if(results.getInt("seats") > 0)
{
%>
<td>
<input type="radio" name="show" value="<%= results.getInt("ID") %>"</td>
</td>
<%
}
%>
<td><%= results.getString("city") %> </td>
<td align="center"><%= results.getInt("seats") %>
</td>
</tr>
<%
}
%>
</table>
</td></tr></table>
<p>
<input type="submit" value="Next Generation (choose shuttle)">
</form>
</center>
</body>
</html>
//conference1.jsp ends here

please help and let me know the solution for this error
thanks, wating anxiously for the reply
bye
take care
from Shehzad Khan
:roll:
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Shezi"

Your name doesn't agree with the javaranch guidelines. Please take a moment and re-register after reviewing the guidelines at http://www.javaranch.com/name.jsp
thanks for your cooperation.
- satya
 
Shehzad Khan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear everyone, i am mailing my previous mail again. please someone tell me why i am having trouble in getting values from access database. or please let me know any link if possible or solution to me as to what i can do.
where i am wrong?
thanks, take care
from sk!!
hello everyone
thanks sir bill, for your valuable information
it really works and ConnectionBean class is picked up gracefully by conference1.jsp. but there is now another problem that emerged. now the error "java.sql.SQLException: no data found" arised.
what to do? please help. my database name is ppp.mdb and it is placed in d:\jakarta-tomcat-4.0-b5\examples\jsp\ppp.mdb
in here, i have two tables with the name a)conference and b)shuttles
in the conference table, i have four field which i am writing in the following order: field_name:field_type
1)ID:integer
2)city:String
3)airport:String
4)seats:integer
the ConnectionBean class code is the same that i have posted previously
however i have changed conference1.jsp a bit.
so the code of conference1.jsp is pasted here.
//conference1.jsp starts from here
<%@ page import="java.sql.*"%>
<jsp:useBean id="connection" class="babaji.ch_7.ConnectionBean" scope="session" />
<html>
<body>
<center>
<font size="+2" face="arial" ><b>Conference Registration</b></font>
<form action="shuttle.jsp" method="post">
<table border="1" bgcolor="tan" width="50%" align="center">
<tr><td><table border="0" bgcolor="white" cellspacing="0" width="100%">
<tr bgcolor="tan">
<th> </th><th>City</th><th>Tickets Remaining</th></tr>
<%
String sql = "select * from conference";
ResultSet results = connection.executeQuery(sql);
while(results.next())
{
if(results.getInt("seats") > 0)
{
%>
<td>
<input type="radio" name="show" value="<%= results.getInt("ID") %>"</td>
</td>
<%
}
%>
<td><%= results.getString("city") %> </td>
<td align="center"><%= results.getInt("seats") %>
</td>
</tr>
<%
}
%>
</table>
</td></tr></table>
<p>
<input type="submit" value="Next Generation (choose shuttle)">
</form>
</center>
</body>
</html>
//conference1.jsp ends here

please help and let me know the solution for this error
thanks, wating anxiously for the reply
bye
take care
from Shehzad Khan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic