Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Why does it not print out the database columns???

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does it not print out the database columns???
I am using a bean to handle database connection, here is the bean code below:
package mybeans;
import java.sql.*;
public class GetDbInfo
{
ResultSet rs;
String PHONE;
public GetDbInfo()
{ try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e)
{
System.out.println( "error: connection to " + "sun.jdbc.odbc.JdbcOdbcDriver");
}
try{
Connection con = DriverManager.getConnection("jdbc dbc:MyDatabase2");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM CONTACTTABLE");
}
catch(Exception e)
{
System.out.println( "error: connection to DriverManager");
}
}
public String getPHONE()
{
try{
rs.next();
PHONE = rs.getString(3);
}
catch(Exception e)
{
System.out.println( "error: SQL query");
}

return PHONE;
}
}
Below is the JSP page that uses the bean

<!-- dbbean.jsp -->
<html>
<head>
<jsp:useBean id="DbBean" scope="session" class="mybeans.GetDbInfo" />
<jsp:setProperty name="DbBean" property="*" />
</head>
<body>
The first three names in the database are:
<ul>
<%
for (int x = 0; x < 3; x++)
{
%>
<li><jsp:getProperty name="DbBean" property="PHONE" /></li>
<%
}
%>
</ul>
</body>
</html>

Now it should print in the browser three values from the database but it doesn't
Why???
Note I am using Micorsoft Access.
Note also that I tried similar data access without a bean and this worked, but it
does not seem to work with beans.
Below is the output:
The first three names in the database are:
.
.
.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there are a number of things that you could be doing differently here. First I will say that getting a ResultSet from a JavaBean to a JSP can be strange at first. Here is how I would do it (This may not be the best way and I have not coded it exactly but you should get the idea):
1. Create a method in your bean that connects the database... so for your purposes it would be...
Connection con
public void connect() {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbcdbc:MyDatabase2");
}
catch(Exception e)
{
System.out.println( "error: connection to " + "sun.jdbc.odbc.JdbcOdbcDriver");
}
try{
}
catch(Exception e)
{
System.out.println( "error: connection to DriverManager");
}
}
2. Then create a method that returns a ResultSet... like so for your example it would be something like...
public String getPHONE()
{
try{
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM CONTACTTABLE");
}
catch(Exception e)
{
System.out.println( "error: SQL query");
}
return rs;
}

The create a JSP that uses that JavaBean and populates the HTML like so ...
<ul>
<%
theBean.connect();
ResultSet rs = theBean.getPHONES();
while(rs.next()) {
%>
<li><%= rs.getString("aDatabaseColumn") %></li>
<%
}
%>
</ul>
Also you should create another method in the bean that disconnects the database connection...
Sorry I did not code it all out perfectly for you but hope that helps some...
 
Zac Roberts
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry in my last post number 2 should be:
public ResultSet getPHONE() instead of
public String getPHONE()
 
northfield Sid
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have done as suggested by Zac, but getting strange error which I cannot decipher.
Here is the new Bean class
/* GetdbInfo2.jsp */
package mybeans;
import java.sql.*;
public class GetDbInfo2
{
private ResultSet rs;
private Connection con;
public GetDbInfo2()
{
}

public void connect()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc dbc:MyDatabase2");
}
catch(Exception e)
{
System.out.println( "error: connection to DriverManager");
}
}

public void disconnect()
{
try{
con.close();
}
catch(Exception e)
{
System.out.println("close db connection error");
}
}

public ResultSet getPHONE()
{
try{
Statement stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM CONTACTTABLE");
}
catch(Exception e)
{
System.out.println( "error: SQL query");
}

return rs;
}
}
<!-- this is the dbbean.jsp -->
<html>
<head>
<jsp:useBean id="dbBean" scope="session" class="mybeans.GetDbInfo2" />
</head>
<%@ page import=java.sql.*; %>
<body>
The first three names in the database are:
<ul>
<% dbBean.connect();
ResultSet rs = dbBean.getPHONE();

while (rs.next() )
{
%>
<li>
<%= rs.getString("PHONE") %>
</li>
<%
}
%>
</ul>
</body>
</html>

Here is the error:
Error: 500
Location: /myJSPs/jsp/Whitehead/chap7/dbbean2.jsp
Internal Servlet Error:
org.apache.jasper.compiler.ParseException: D:\tomcat\jakarta-tomcat-3.3.1\webapps\myJSPs\jsp\Whitehead\chap7\dbbean2.jsp(6,16) Attribute value should be quoted
at org.apache.jasper.compiler.JspReader.parseToken(JspReader.java:487)
at org.apache.jasper.compiler.JspReader.parseAttributeValue(JspReader.java:533)
at org.apache.jasper.compiler.JspReader.parseTagAttributes(JspReader.java:644)
at org.apache.jasper.compiler.Parser$Directive.accept(Parser.java:192)
at org.apache.jasper.compiler.Parser.parse(Parser.java:1077)
at org.apache.jasper.compiler.Parser.parse(Parser.java:1042)
at org.apache.jasper.compiler.Parser.parse(Parser.java:1038)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:209)
at org.apache.tomcat.facade.JasperLiaison.jsp2java(JspInterceptor.java:790)
at org.apache.tomcat.facade.JasperLiaison.processJspFile(JspInterceptor.java:731)
at org.apache.tomcat.facade.JspInterceptor.requestMap(JspInterceptor.java:506)
at org.apache.tomcat.core.ContextManager.processRequest(ContextManager.java:968)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:875)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:833)
at org.apache.tomcat.modules.server.Http10Interceptor.processConnection(Http10Interceptor.java:176)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:494)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:516)
at java.lang.Thread.run(Thread.java:484)
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Northfield,
Person learns from it's mistakes...
check syntex of your code of jsp file at:
<%@ page import=java.sql.*; %>
corrrrrrrrect it as:
<%@ page import="java.sql.*" %>
dear, don't insert semicolon....
Enjoy..
Good Day.
Sandeep Oza
[ September 11, 2002: Message edited by: Sandeep. Oza ]
 
northfield Sid
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx,
>Person learns from it's mistakes...
totally in cognizance
 
You learn how to close your eyes and tell yourself "this just isn't really happening to me." Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic