• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cannot display data from database

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I wrote a very simple JSP code (weblogic server, mssql server 7.0). The problem is that i want to get value from the databse into a variable and display it on the page.
sample code is :
<%<br /> Connection conn = null;<br /> try {<br /> Driver myDriver = (Driver)<br /> Class.forName("weblogic.jdbc.pool.Driver").newInstance();<br /> conn = myDriver.connect("jdbc:weblogic ool:sqlServerPool", null);<br /> Statement stmt = null;<br /> ResultSet rs = null;<br /> stmt = conn.createStatement();<br /> if ("find".equalsIgnoreCase(request.getParameter("action")))<br /> {<br /> String s_stmt = "select pm_name from project_manager where upper(pm_name) like '%allistair%'";<br /> rs = stmt.executeQuery(s_stmt);<br /> s_name = rs.getString("pm_name");<br /> }<br /> } catch(SQLException e)<br /> {<br /> System.out.println("Error in sql query :");<br /> e.printStackTrace();<br /> }<br /> %>
........some html code......
<td>*Name</td>
<td><input type="text" name="name" value="<%= s_name %>"> </td>
......some html code.....
<td><input type="text" name="findmanager"></td>
<td><input type="submit" name="find" value="Find" onClick="this.form.action.value='find';"></td>
.....more code..........
The problem i am facing is that I am not able to display the value of pm_name in s_name. The sql runs fine and returns a value. In fact if i check the sql in a hidden variable after displaying s_name, I can see the full sql.
Can somebody help me out ? This was my first JSP code (in my life) which i developed for my project but has really bogged me down becoz i am not able to display even a basic thing.
Thanks
Poonam
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, your message totally screwed up my browser window. Very impressive.
Since I can't really see what you posted, I will only be able to give generalities. If you want to set a variable from a ResultSet object, you do is just as you would in any Java application with JDBC. For example, to set an integer value i
<%i = rs.getInt(1) ; %>

Cheers,
Robert
[This message has been edited by Robert Brunner (edited August 16, 2001).]
[This message has been edited by Robert Brunner (edited August 16, 2001).]
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I got this sorted out:

(Gotta be really careful about pasting html tags)
The first thing I'd point out is that you have to call rs.next() before you can try to read any data from it. The ResultSet is a cursor-type object (lie Iterators and Enumerators) and starts out as looking BEFORE the first element.
Try adding if(rs.next()) before trying to read the string...

Dave.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic