wood burning stoves 2.0
The moose likes JDBC and Relational Databases 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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Cannot display data from database" Watch "Cannot display data from database" New topic

Cannot display data from database


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

[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

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...

I agree. Here's the link: http://aspose.com/file-tools
subject: Cannot display data from database
It's not a secret anymore!