hello,
I am getting a null output as a resultset from my database. I want to suppress that error and show it as a empty string when i throw the result on page.
all ideas are welcome, preferably with some amount of codes..
Prasath
Discussions
Web tier: servlets, JSP, Web frameworks: Supressing a null which is received from the database
-
Supressing a null which is received from the database (2 messages)
- Posted by: Prasath Balakrishnan
- Posted on: March 03 2001 02:08 EST
Threaded Messages (2)
- Supressing a null which is received from the database by Rick Grashel on March 01 2001 18:34 EST
- Supressing a null which is received from the database by Prasath Balakrishnan on March 02 2001 02:28 EST
-
Supressing a null which is received from the database[ Go to top ]
- Posted by: Rick Grashel
- Posted on: March 01 2001 18:34 EST
- in response to Prasath Balakrishnan
Prasath,
The best way to do what you are talking about is to create a 'wrapper' for the JDBC result set and override the get/set methods.
Something like the following:
public class CustomResultSet
{
private ResultSet resultSet = null;
public CustomResultSet(ResultSet _resultSet)
{
resultSet = _resultSet;
}
public String getString(int _column) throws SQLException
{
String resultString = resultSet.getString(_column);
if (resultString == null)
{
resultString = "";
}
return resultString;
}
// Make sure to override all of the other get/set methods
// that you need also.
} -
Supressing a null which is received from the database[ Go to top ]
- Posted by: Prasath Balakrishnan
- Posted on: March 02 2001 02:28 EST
- in response to Rick Grashel
Rick,
Here is my code. can u tell me where to insert the methods as it is still causing a null pointer exception during the run time.
The variable tn needs to be taken for the problem i mentioned.It is out of my third resultset
package orgis_servlets;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.lang.String;
/*
* This class is a UserAdministration Servlet
* Author : Prasath Balakrishnan
* Created: 02/21/2001
*/
public class UserAdmin extends HttpServlet
{
// Create the Variables as required.
private ConnectionPool connectionPool;
private Statement stmt = null;
private Connection con = null;
private static final String SELECT_NAMEPASSWORD =
"SELECT USER_PROFILE.USER_ID,USER_FNAME,USER_LNAME,USER_TN,LOC_DESC,USER_ROLE.ROLE_ID,ROLE_SHORT_NM FROM USER_PROFILE,USER_LOCATION,USER_ROLE,ORGIS_ROLE WHERE USER_PROFILE.USER_ID =";
private static final String AND1 = " AND USER_PROFILE.USER_LOC_ID =";
private static final String AND2 = " USER_LOCATION.USER_LOC_ID";
private static final String AND3 = " AND USER_PROFILE.USER_ID =";
private static final String AND4 = " USER_ROLE.USER_ID";
private static final String AND5 = " AND USER_ROLE.ROLE_ID =";
private static final String AND6 = " ORGIS_ROLE.ROLE_ID ORDER BY USER_ROLE.ROLE_ID";
private static final String QUOTE ="'";
private static final String SELECT = "SELECT USER_LOC_ID, LOC_DESC FROM USER_LOCATION";
private static final String SELECT1 = "SELECT ROLE_ID, ROLE_SHORT_NM FROM ORGIS_ROLE";
String sbcid,sbcid1;
String password;
String fname;
String lname;
String tn;
String ldesc;
String role;
boolean hasData = false;
String urole;
String uloc;
String userid1;
String userlocid1;
String luserid ="";
String rid;
Vector vector1 = new Vector();
Vector vector2 = new Vector();
Vector vector3 = new Vector();
Vector vector4 = new Vector();
Vector vector5 = new Vector();
int roleid,userlocid;
String status1 ="UPDATE";
String status2 ="INSERT";
char ch1,ch2,ch3,ch4,ch5;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
try
{
connectionPool = new ConnectionPool(initialConnections(),
maxConnections(),
true);
}
catch (Exception e) {
System.err.println( "Error while making the pool!" );
connectionPool = null;
}
}
/*
*
* @param req the servlet request
* @param res the servlet response
* @exception IOException If an I/O error has occurred
*/
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html");
/*get the Inputs and convert the inputs into UpperCase if necessary*/
sbcid = req.getParameter("sbcuid");
sbcid1=sbcid.toUpperCase();
HttpSession session = req.getSession(true);
System.out.println("session is active for a maximum time of : " + session.getMaxInactiveInterval());
System.out.println("Session ID Value in UserAdminServlet is " + session.getId());
System.out.println(" Is this a new Session " + session.isNew());
LogonInfo userInfo = (LogonInfo) session.getValue("logonInfo");
System.out.println("UserAdmin.session.LogonInfo.userid=" + userInfo.getUserId());
luserid = userInfo.getUserId();
// For Error Message the Title and header..
String title = "User Administration Error";
String header = "User Administration Error";
String button1[] = {"button","Back","window.history.back()"};
String button2[] = {"button","ORGIS Home","/orgis_jsp/OrgisHome.html" };
//Checking for the SBCUID character by character.
if(sbcid.length() != 6)
{
req.setAttribute("javax.servlet.jsp.jspException", new Exception("Invalid SBCUID."));
req.setAttribute("title", title);
callPage("/orgis_jsp/Error.jsp", req, res);
}
else if (sbcid.length() == 0)
{
req.setAttribute("javax.servlet.jsp.jspException", new Exception("Enter a value in all required fields."));
req.setAttribute("title", title);
callPage("/orgis_jsp/Error.jsp", req, res);
}
else if (checkuser1(ch1))
{
req.setAttribute("javax.servlet.jsp.jspException", new Exception("Invalid SBCUID."));
req.setAttribute("title", title);
callPage("/orgis_jsp/Error.jsp", req, res);
}
else if (checkuser2(ch2))
{
req.setAttribute("javax.servlet.jsp.jspException", new Exception("Invalid SBCUID."));
req.setAttribute("title", title);
callPage("/orgis_jsp/Error.jsp", req, res);
}
else if (checkuser3(ch3))
{
req.setAttribute("javax.servlet.jsp.jspException", new Exception("Invalid SBCUID."));
req.setAttribute("title", title);
callPage("/orgis_jsp/Error.jsp", req, res);
}
else if (checkuser4(ch4))
{
req.setAttribute("javax.servlet.jsp.jspException", new Exception("Invalid SBCUID."));
req.setAttribute("title", title);
callPage("/orgis_jsp/Error.jsp", req, res);
}
else if (checkuser5(ch5))
{
req.setAttribute("javax.servlet.jsp.jspException", new Exception("Invalid SBCUID."));
req.setAttribute("title", title);
callPage("/orgis_jsp/Error.jsp", req, res);
}
else
{
//**Making the connection
ResultSet rs = null ;
ResultSet rs1 = null;
ResultSet rs2 = null;
hasData = false;
try
{
con = connectionPool.getConnection();
stmt = con.createStatement();
// query for selecting the Location details Combo Box.
vector1.clear();
vector5.clear();
String query1 =(SELECT);
rs1=stmt.executeQuery(query1);
while(rs1.next())
{
hasData = true;
userlocid = rs1.getInt(1);
userlocid1 = userlocid +"";
uloc = rs1.getString(2);
vector1.add(uloc);
vector5.add(userlocid1);
}
rs1.close();
//query for selecting the User role List Box.
hasData= false;
vector2.clear();
String query2 =(SELECT1);
rs2=stmt.executeQuery(query2);
while(rs2.next())
{
hasData = true;
urole = rs2.getString(2);
vector2.add(urole);
}
rs2.close();
// query for selecting the UserProfile if any for the SBCUID.
hasData= false;
vector3.clear();
vector4.clear();
String userid="";
String query = (SELECT_NAMEPASSWORD + QUOTE + sbcid1 + QUOTE + AND1 + AND2 + AND3 + AND4 + AND5 + AND6);
rs = stmt.executeQuery (query);
while(rs.next())
{
hasData = true;
userid = rs.getString(1);
fname = rs.getString(2);
lname = rs.getString(3);
tn = rs.getString(4);
ldesc = rs.getString(5);
roleid = rs.getInt(6);
rid = roleid +"";
role = rs.getString(7);
vector3.add(role);
vector4.add(rid);
}
/* Check whether the user exists & if so the role of the user */
if((userid).equals (sbcid1))
{
//req.setAttribute("luserid",luserid);
req.setAttribute("uloc",uloc);
req.setAttribute("userid",userid);
req.setAttribute("fname",fname);
req.setAttribute("lname",lname);
req.setAttribute("tn",tn);
req.setAttribute("ldesc",ldesc);
req.setAttribute("vector1",vector1);
req.setAttribute("vector2",vector2);
req.setAttribute("vector3",vector3);
req.setAttribute("vector4",vector4);
req.setAttribute("vector5",vector5);
req.setAttribute("status1",status1);
callPage("/orgis_jsp/UserDetail1.jsp", req, res);
}
// if the userid is a new SBCUID do this
else if ((rs == null) || (hasData == false))
{
userid1 = sbcid1;
req.setAttribute("userid1",sbcid1);
req.setAttribute("vector1",vector1);
req.setAttribute("vector2",vector2);
req.setAttribute("vector4",vector4);
req.setAttribute("vector5",vector5);
req.setAttribute("status2",status2);
//req.setAttribute("luserid",luserid);
callPage("/orgis_jsp/UserDetail2.jsp", req, res);
}
rs.close();
stmt.close();
}
catch(SQLException e)
{
req.setAttribute("javax.servlet.jsp.jspException", new Exception("SQL Error Occurred.Please correct it."));
req.setAttribute("title", title);
callPage("/orgis_jsp/Error.jsp", req, res);
}
/* Closing the database connection*/
finally
{
try
{
if (con != null) con.close();
}
catch(SQLException e)
{
req.setAttribute("javax.servlet.jsp.jspException", new Exception("Database Connection not closed properly."));
req.setAttribute("title", title);
callPage("/orgis_jsp/Error.jsp", req, res);
}
}// End of Finally
}/* end of else after checkuser method*/
}/* end of doPost */
// Method to call the JSP
private void callPage(String url, HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.include(req,res);
}
// Method to check the SBCUID input format
protected boolean checkuser1(char ch1)
{
if (sbcid.length() != 0)
{
ch1 = sbcid.charAt(0);
if(!Character.isLetter(ch1))
{
return true;
}
else
return false;
}
return false;
}
// Method to check the SBCUID input format
protected boolean checkuser2(char ch2)
{
if (sbcid.length() != 0)
{
ch2 = sbcid.charAt(1);
if(!Character.isLetter(ch2))
{
return true;
}
else
return false;
}
return false;
}
// Method to check the SBCUID input format
protected boolean checkuser3(char ch3)
{
if (sbcid.length() != 0)
{
ch3 = sbcid.charAt(2);
if(!Character.isDigit(ch3))
{
return true;
}
else
return false;
}
return false;
}
// Method to check the SBCUID input format
protected boolean checkuser4(char ch4)
{
if (sbcid.length() != 0)
{
ch4 = sbcid.charAt(3);
if(!Character.isDigit(ch4))
{
return true;
}
else
return false;
}
return false;
}
// Method to check the SBCUID input format
protected boolean checkuser5(char ch5)
{
if (sbcid.length() != 0)
{
ch5 = sbcid.charAt(4);
if(!Character.isDigit(ch5))
{
return true;
}
else
return false;
}
return false;
}
// Override this in subclass to change number of initial connections.
protected int initialConnections()
{
return(3);
}
// Override this in subclass to change maximum number of connections.
protected int maxConnections()
{
return(20);
}
}/* end of class*/