Achieiving the equivelent of PHPs mysql_result command in JSP

Hello,

How would I go about getting the same result as PHP's:

$query = mysql_query("SELECT * FROM RUNNINGINSTANCE WHERE UNITLINK = $param");
$instancename1 = mysql_result($query,0,"name");

would in JSP?

Thanks
ogrynAsked:
Who is Participating?
 
TimYatesConnect With a Mentor Commented:
Assuming you have a db connection called "connection", and param is stored in a String variable "param";

-------------------

<%@ page import="java.sql.*" %>
<%
    PreparedStatement stmt =  null ;
    ResultSet rslt = null ;
    try
    {
        PreparedStatement stmt = connection.prepareStatement( "SELECT * FROM RUNNINGINSTANCE WHERE UNITLINK = ?" ) ;
        stmt.setString( 1, param ) ;
        ResultSet rslt = stmt.executeQuery() ;
        if( rslt.next() )
        {
            String instanceName = rslt.getString( "name" ) ;
        }
    }
    catch( SQLException ex )
    {
        out.print( ex.toString() ) ;
    }
    finally
    {
        try { if( rslt != null ) rslt.close() ; } catch( SQLException ex ) {}
        try { if( stmt != null ) stmt.close() ; } catch( SQLException ex ) {}
    }
%>
0
 
ogrynAuthor Commented:
Does the 1 in the follwing statement, equate to which row of the database you are reading from?

stmt.setString( 1, param ) ;

0
 
TimYatesCommented:
No, it sets the first ? in the prepares statement :-)

rslt.next() gets the next (first in this case) row of the resultset ready, then you can read the "name" field out of it
0
 
ogrynAuthor Commented:
ahh, thanks. I'm a really newbie at Java stuff. I've been using JSTL, but I need to step it up a gear. I'll give that a go. Thanks
0
 
TimYatesCommented:
Hope it works :-)

http://java.sun.com/developer/qow/archive/73/index.html

Should help too :-)

Good Luck!

Tim
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.