Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Location
    Pennsylvania
    Posts
    306
    Rep Power
    10

    java.sql.SQLException: Cursor position not valid error


    HI all, I have been working on creating applications using DAO's. I am finally starting to get the hang of it. However I am running into this error:

    java.sql.SQLException: Cursor position not valid

    here is the full stack trace:

    Code:
    java.sql.SQLException: Cursor position not valid.
    	at com.ibm.as400.access.JDError.throwSQLException(JDError.java:382)
    	at com.ibm.as400.access.JDError.throwSQLException(JDError.java:359)
    	at com.ibm.as400.access.AS400JDBCResultSet.getValue(AS400JDBCResultSet.java:3589)
    	at com.ibm.as400.access.AS400JDBCResultSet.getString(AS400JDBCResultSet.java:3230)
    	at org.apache.tomcat.dbcp.dbcp.DelegatingResultSet.getString(DelegatingResultSet.java:174)
    	at leftMenu.LeftDAO.logo(LeftDAO.java:80)
    	at leftMenu.Left.doGet(Left.java:42)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    	at org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:419)
    	at org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:133)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    	at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:673)
    	at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:464)
    	at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:399)
    	at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
    	at BgLeftMnu.doGet(BgLeftMnu.java:58)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
    	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:738)
    	at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526)
    	at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
    	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
    	at java.lang.Thread.run(Unknown Source)
    So Line 80 of LeftDAO is:
    Code:
    try
    		{
    			conn = ds.getConnection();
    			CallableStatement cs = conn.prepareCall(LOGO);
    
    			ResultSet rs = cs.executeQuery();
    
    			String logo = rs.getString(1); LINE 80
    
    			System.out.println(logo);
    
    			rs.close();
    			return logo;
    		}
    line 42 of Left is:
    Code:
    String bgLogo = dao.logo(); LINE 42
    System.out.println("bgLogo" + bgLogo);
    request.setAttribute("bgLogo", bgLogo);
    and finally line 58 of BgLeftMnu:
    Code:
    RequestDispatcher rd = request.getRequestDispatcher("/servlet/Left");
    rd.forward(request, response);
    Anyone have any suggestions as to how I can clear this up?

    I have another question when this is solved, but I cannot get to it until this problem is resolved.

    Thanks for the help everyone!!
  2. #2
  3. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    PHP Code:
    ResultSet rs cs.executeQuery();
    if (
    rs.next()) { // <-----
        
    String logo rs.getString(1); 
        
    // do stuff with logo...

    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  4. #3
  5. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    Explanation: you have to advance the cursor to the first record of the result set. The call to next() will return false if the query returns no rows.

    The Java Tutorial - Trail: JDBC Database Access
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Location
    Pennsylvania
    Posts
    306
    Rep Power
    10
    alright I didnt realize I had to advance to the first result set if there was only one result being returned(why I thought that I will never know)...but I guess it makes sense.

    Thanks I will go fix this then come back and ask my second question!
  8. #5
  9. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    alright I didnt realize I had to advance to the first result set if there was only one result being returned(why I thought that I will never know)...but I guess it makes sense.
    It's because the cursor actually starts out before the first row (if there is at least one row returned). If the cursor started out on the first row, it'd be a quite a situation if your query didn't return any rows...
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Location
    Pennsylvania
    Posts
    306
    Rep Power
    10
    ok that is all set and works great. I think I have a good model set up for this particular application...I will find out soon though. What I need to do with this part of my applications is very similar to what I did a while ago(I posted about creating a drop down menu that would be populated at runtime with the results of a stored procedure)

    This time however, I do not need to populate a drop down menu, I need to create something like an unordered list. This is the scenario: I am working with a framset. The top frame has a horizontal menu with 4 options. When one of the options is clicked another menu is supposed to appear in the left frame. This is to be a vertical menu.

    I have the horizontal menu created (it was a string returned to me from a stored procedure that I just inserted into the html). When I click on the option I call the required stored procedures and I get the results, but again I am not sure how to take the result set and have it displayed on the html page.

    Each result is just a string of code that needs to be placed in the html to create the unordered list (at least thats what I was thinking, it does not have to be an unordered list, if there is an easier way thats great!)

    ok so that is what I want to do here is the code I will list it all:

    First Left.java:
    Code:
    package leftMenu;
    
    import java.io.*;
    import java.sql.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.sql.*;
    import javax.naming.*;
    import javax.naming.directory.*;
    
    public class Left extends HttpServlet
    {
    	public static final String PATH = "/bgLeft.jsp";
    
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
        	String resultPath = PATH;
    
        	HttpSession session = request.getSession(true);
    
    		String sessId = (String) session.getAttribute("id");
    		String xmq = (String) session.getAttribute("xmq");
    		String ymb = (String) session.getAttribute("ymb");
    
    		try
    		{
    			Context initCtx = new InitialContext();
    			Context envCtx = (Context) initCtx.lookup("java:comp/env");
    			DataSource ds = (DataSource) envCtx.lookup("jdbc/myDataSource");
    
              	LeftDAO dao = new LeftDAO(ds);
    this is the list of menu items that need to be displayed
              	List items = dao.menuItems(sessId, xmq, ymb);
              	System.out.println("items:" + items);
              	request.setAttribute("items", items);
    this is the logo that I will display at the bottom of the frame
              	String bgLogo = dao.logo();
              	System.out.println("bgLogo: " + bgLogo);
              	request.setAttribute("bgLogo", bgLogo);
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    		}
    
    		RequestDispatcher rd = request.getRequestDispatcher(resultPath);
    
    		rd.forward(request, response);
    
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
        	doGet(request, response);
        }
    }
    Now Left.DAO.java
    Code:
    package leftMenu;
    
    import java.io.*;
    import java.sql.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.sql.*;
    import javax.naming.*;
    import javax.naming.directory.*;
    
    public class LeftDAO
    {
    	static final String LEFT = "{call wmsguitst.BGGETLFTM1(?,?,?)}";
    	static final String LOGO = "{call wmsguitst.BGGETLOGO1()}";
    
    	private DataSource ds;
    
    	public LeftDAO(DataSource ds)
    	{
    
        	if (ds == null)
        	{
        		throw new NullPointerException("data source cannot be null");
        	}
        	this.ds = ds;
    	}
     again this it where the menu items are accessed fromthe the stored proceudre
    	public List menuItems(String sessId, String xmq, String ymb) throws SQLException
    	{
    		Connection conn = null;
    
    		try
    		{
    			conn = ds.getConnection();
    			CallableStatement cs = conn.prepareCall(LEFT);
    
    			cs.setString(1, sessId);
    			cs.setString(2, xmq);
    			cs.setString(3, ymb);
    
    			ResultSet rs = cs.executeQuery();
    
    			List items = new ArrayList();
    
    			while(rs.next())
    			{
    				String item = rs.getString(1);
    				items.add(new Item(item));
    			}
    			rs.close();
    			return items;
    		}
    		finally
    		{
    			if (conn != null) conn.close();
    		}
    	}
     and again this stored proceudre returns just a string referencing a logo
    	public String logo() throws SQLException
    	{
    		Connection conn = null;
    
    		try
    		{
    			conn = ds.getConnection();
    			CallableStatement cs = conn.prepareCall(LOGO);
    
    			ResultSet rs = cs.executeQuery();
    
    			String logo = null;
    
    			if(rs.next())
    			{
    				logo = rs.getString(1);
    			}
    
    			rs.close();
    			return logo;
    		}
    		finally
    		{
    			if (conn != null) conn.close();
    		}
    	}
    }
    and finally Item.java
    Code:
    package leftMenu;
    
    import java.io.*;
    import java.sql.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.sql.*;
    import javax.naming.*;
    import javax.naming.directory.*;
    
    public class Item
    {
    	private String item;
    
    	public Item(String item)
    	{
    		this.item = item;
    	}
    
    	public String getItem()
    	{
    		return item;
    	}
    }
    Sorry this was so long...if you need a better explanation of what is going on I can attempt to provide one...

    Thanks for your time!
  12. #7
  13. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Location
    Pennsylvania
    Posts
    306
    Rep Power
    10
    looks like it is time to hit the books again....

    can you just tell me if my design so far is adaquate..I modeled it after what was created for my previous application, I just modified it for this one...

    thanks
  16. #9
  17. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    If it works the way you want it to, it's adequate!
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Location
    Pennsylvania
    Posts
    306
    Rep Power
    10
    HAHA...that is the greatest reply I have ever seen...
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Location
    Pennsylvania
    Posts
    306
    Rep Power
    10
    Do you have any different tutorials, those did not help me much with this specific taks. Maybe any with examples...I was not able to find much on google either....

    Thanks
  22. #12
  23. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    I don't believe that it took you only two hours to complete each of those tutorials or understand the information contained within them. It's going to be tough to find a tutorial that will spell out exactly what you need to do each step of the way - you'll have specific requirements that need to be met, and that's perfectly natural. Rather, the tutorials are designed to provide you with a foundation so you can make choices about how to implement your ideas in Java.

    See if this one helps close any gaps.
    The J2EE Tutorial - JavaServer Pages Technology

    If that doesn't help, there's always Google...
    http://www.google.com/search?q=jsp+jdbc+tutorial
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Location
    Pennsylvania
    Posts
    306
    Rep Power
    10
    alright here is one specific question I have. If I figure this out I should be able to do this I hope...

    ok the way I am going to do this, I want to pass the List items as an attribute so it can be used by the jsp. Can I actually pass it as a list as such:

    Code:
     List items = dao.menuItems(sessId, xmq, ymb);
              	request.setAttribute("items", items);
    in otherwords does that pass the List as a string or as a list. Also if I had an array and I attempted to set it as an attribute does that need anything special or can I just do it like the above example.

    secondly, when attempting to access these in the jsp this is how I was planning on going about it:
    Code:
    string[] listedItems = request.getAttribute("items")
    will that store the items in an array??

    Thats mainly what I am hung up on right now, I know how to run through it when I eventually get to it, but right now I am not sure how to get to it.....

    I hope this is clear enough


    Thanks!
  26. #14
  27. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    in otherwords does that pass the List as a string or as a list.
    It gets passed as a List.
    Also if I had an array and I attempted to set it as an attribute does that need anything special or can I just do it like the above example.
    It's just like the List.
    will that store the items in an array??
    No. You'll need to get the same type of object back. Example:
    PHP Code:
    List items = (List) request.getAttribute("items");
    // or...
    String[] items = (String[]) request.getAttribute("items");
    // depending on what type of reference was set... 
    This is exactly the sort of thing that you'll learn how to do when you read the tutorials. In all seriousness, it seems like a waste of time and bandwidth to repeat information here that's covered in the introductory tutorials and the API. I certainly don't mind helping, but you'll need to improve your research habits if you wish to grow in your programming skills. I don't want to discourage you from asking for help, but you can very often find your own answers by doing a little research and writing a lot of code (that's how I learned, so I'm speaking from experience).

    Good luck!
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Location
    Pennsylvania
    Posts
    306
    Rep Power
    10
    I understand that research is valuable and that learning how to code by trying and trying it until I finally get it can be a valuable learning tool, but what would have taken me hours to read through everything and learn you answered in 3 minutes. I learned everything else by reading, it was just that one thing that I did not see out in the open, I figured instead of digging and digging I would ask because if someone knew that answer, why should I waste my time looking and killing myself...I know that sounds selfish and I am sorry it comes across that way, I am just going for the most effective time efficient solution, and now I know how to do it and thats what I needed.

    So I do read the tutorials, but I also do ask a lot of questions. I do not ask questions to be a pain or to inconvenience anyone, but if the answer is readily available you will have a hard time convincing me that hours and hours of reading for one answer is better than asking.

    Please do not be upset by this response, I just didnt want you to think I come ask for questions and that is it, I have learned more from this forum than I could have ever imagined. I have great respect for everyone in here and do not wish to upset anyone.

    Again, thanks for you time!
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo