• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

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
0
ogryn
Asked:
ogryn
  • 3
  • 2
1 Solution
 
TimYatesCommented:
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now