Solved

Achieiving the equivelent of PHPs mysql_result command in JSP

Posted on 2004-08-10
5
239 Views
Last Modified: 2012-05-05
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
Comment
Question by:ogryn
  • 3
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 250 total points
ID: 11762439
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
 

Author Comment

by:ogryn
ID: 11762553
Does the 1 in the follwing statement, equate to which row of the database you are reading from?

stmt.setString( 1, param ) ;

0
 
LVL 35

Expert Comment

by:TimYates
ID: 11762618
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
 

Author Comment

by:ogryn
ID: 11762675
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
 
LVL 35

Expert Comment

by:TimYates
ID: 11762815
Hope it works :-)

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

Should help too :-)

Good Luck!

Tim
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you are looking at this article, you have most likely been hit by some version of ransomware and are trying to find out if there is anything you can do, or what way you should react - READ ON!
Giving access to ESXi shell console is always an issue for IT departments to other Teams, or Projects. We need to find a way so that teams can use ESXTOP for their POCs, or tests without giving them the access to ESXi host shell console with a root …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question