Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot display data from database

 
poonam
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
Robert Brunner
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • 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).]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic