Why control is not going to the bean?
[nobr]Hi all,
I am having some problem while accessing a bean. Here is the program i am trying to do. I have two .jsp page and one bean class.
First jsp page is Login.jsp
<html>
<head>
<title>Login Page</title>
</head>
<body>
<center>
<form method=post action=SQLTool.jsp>
<table>
<tr>
<td>User Name : </td>
<td><input type=text name=userName></td>
</tr>
<tr>
<td>Password : </td>
<td><input type=password name=password></td>
</tr>
<tr>
<td><input type=reset></td>
<td><input type=submit value="Login"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
And the second jsp page is SQLTool.jsp
<jsp:useBean id="theBean" class="myBeans.SQLToolBean">
<%
try{
Class.forName("COM.ibm.db2.jdbc.net.DB2Driver");
}catch (Exception e){
out.println(e.toString());
}
%>
</jsp:useBean>
<jsp:setProperty name="theBean" property="userName"/>
<jsp:setProperty name="theBean" property="password"/>
<jsp:setProperty name="theBean" property="conURL" value="jdbc:db2://192.168.7.170:6799/sample"/>
<jsp:setProperty name="theBean" property="sql"/>
<html>
<head>
<title>SQL Tool</title>
</head>
<body>
<br><h2>SQL Tool</h2><br>
Please type your SQL statement in the following box.
<br><br>
<form method=post>
<input type=hidden name=userName value="<jsp:getProperty name="theBean" property="userName"/>">
<input type=hidden name=password value="<jsp:getProperty name="theBean" property="password"/>">
<textarea name=sqlCmdArea cols=80 rows=8>
<jsp:getProperty name="theBean" property="sql"/>
</textarea>
<br>
<input type=submit>
</form>
<br><hr><br>
<%= theBean.getResult() %>
</body>
</html>
And here is my bean class SQLToolBean.java
package myBeans;
import java.sql.*;
publicclass SQLToolBean{
private String sql ="";
private String userName ="";
private String password ="";
private String conURL ="";
publicvoid setSql(String sql){
if(sql !=null)
this.sql = sql;
}
public String getSql(){
return sql;
}
publicvoid setUserName(String name){
if(name !=null)
this.userName = name;
}
public String getUserName(){
return userName;
}
publicvoid setPassword(String password){
if(password !=null)
this.password = password;
}
public String getPassword(){
return password;
}
publicvoid setConURL(String url){
if(url !=null)
this.conURL = url;
}
public String getResult(){
System.out.println("++++++++++++++++++++++++++++Inside the getResult() of the bean");
if(sql ==null || sql.equals(""))
return"";
//System.out.println("++++++++++++++++++++++++++++Inside the getResult() of the bean");
StringBuffer result =new StringBuffer(1024);
try{
Connection con = DriverManager.getConnection(conURL, userName, password);
Statement stat = con.createStatement();
if(sql.toUpperCase().startsWith("SELECT")){
result.append("<TABLE BORDER=1>");
ResultSet rs = stat.executeQuery(sql);
ResultSetMetaData metaData = rs.getMetaData();
//Write table headings
int columnCount = metaData.getColumnCount();
result.append("<TR>");
for(int i = 0; i <= columnCount; i++){
result.append("<TD><B>" + metaData.getColumnName(i) +"</B></TD>\n");
}
result.append("</TR>");
while(rs.next()){
result.append("<TR>");
for(int i = 0; i <= columnCount; i++){
result.append("<TD>" + rs.getString(i) +"</TD>");
}
result.append("</TR>");
}
rs.close();
result.append("</TABLE>");
}else{
int i = stat.executeUpdate(sql);
result.append("Records(s) affected: " + i);
}
stat.close();
con.close();
//result.append("</TABLE>");
}catch(SQLException e){
result.append("<B>Error</B>");
result.append("<BR>");
result.append(e.toString());
}catch(Exception e){
result.append("<B>Error</B>");
result.append("<BR>");
result.append(e.toString());
}
System.out.println("++++++++++++++++++++++++++++At the end of the getResult() of the bean");
String printMe = result.toString();
if(printMe =="")
System.out.println(printMe);
else
System.out.println("No data available");
return printMe;
}
}
The problem is <%= theBean.getResult() %>
. Why i couldn't call the getResult method? What i am missing here?
Thanks for your attention and help.[/nobr]

