Two Laptop Bag*
The moose likes JDBC and the fly likes Cannot display data from database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Cannot display data from database" Watch "Cannot display data from database" New topic
Author

Cannot display data from database

poonam
Greenhorn

Joined: Aug 16, 2001
Posts: 1
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

Joined: Jul 18, 2001
Posts: 49
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

Joined: Mar 06, 2001
Posts: 13459

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot display data from database