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;
}
}
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;
}
}