Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help with this problem

Status
Not open for further replies.

4345487

Programmer
Dec 31, 2002
132
US
I have this class that returns a Resultset can some one explain or post a small sample of how I can print the results in a JSP page.

Thanks in advance.

package test;

import java.sql.*;
import java.util.*;

public class GetData
{
String error;
Connection con;

public GetData()
{
}

public void connect() throws ClassNotFoundException,
SQLException,
Exception
{
try
{
/* load the driver and connect to the database */
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
con = DriverManager.getConnection("jdbc.odbc.NorthwindJava", "xxxxx", "xxxxxx");
}
catch (ClassNotFoundException cnfe)
{
/* display a error to the user -- can find driver*/
error = "ClassNotFoundException: Could not locate DB driver.";
throw new ClassNotFoundException(error);
}
catch (SQLException cnfe)
{
/* display a error to the user -- can't connect to DB */
error = "SQLException: Could not connect to database.";
throw new SQLException(error);
}
catch (Exception e)
{
/* display a message with an unknown error */
error = "Exception: An unknown error occurred while connecting to database.";
throw new Exception(error);
}
}

public void disconnect() throws SQLException
{
try
{
if ( con != null )
{
/* close the database connection */
con.close();
}
}
catch (SQLException sqle)
{
error = ("SQLException: Unable to close the database connection.");
throw new SQLException(error);
}
}

//public ResultSet GetData(String fp, String week_no)
public ResultSet GetData()
throws SQLException, Exception
{
ResultSet rs = null;
if (con != null)
{
try
{
/* get the data from the Database */
PreparedStatement getTblData;
getTblData= con.prepareStatement
("SELECT FirstName, LastName, Title, Address from Employees order by LastName, FirstName");

rs = getTblData.executeQuery();
}
catch (SQLException sqle)
{
error = "SQLException: Update failed, possible duplicate entry.";
throw new SQLException(error);
}
}
else
{
error = "Exception: Connection to the database was lost.";
throw new Exception(error);
}
return rs;
}
}
 
int columns = (rs.getMetaData()).getColumnCount();
String data = "";
while (rs.next()) {
data = "";
for (int i = 1; i <= columns; i++) {
data += (rs.getString(i) +&quot; &quot;);
}
out.println(data);
}
 
Sedj thank you I'm new at java so le me ask another question if you don't mind.

I have the above program GetData.java and compiled into GetData.class. If now I create a JSP page how can I reference or include the class into the JSP page so I can restrieve the data from the Resultset

Thanks
 
Several methods ::

- using the JSP/JavaBean way ::

<jsp:useBean id=&quot;mybean&quot; scope=&quot;session&quot; class=&quot;MyBean&quot; />
<jsp:getProperty name=&quot;mybean&quot; property=&quot;someProperty&quot;/>

- or just in plain scriptlets ::

<% MyClass my = new MyClass();
out.println(&quot;This is my data :: &quot; + my.getMyData();
%>

For your JSP to see the beans/classes they must be within the WEB-INF/classes directory, or jarred up and put in WEB-INF/lib
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top