Solved

Achieiving the equivelent of PHPs mysql_result command in JSP

Posted on 2004-08-10
5
233 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Owning a franchise can be the dream of a lifetime. It provides a chance for economic growth. You can be as successful as you want.  To make your franchise successful, you need to market it successfully. Here are six of the best marketing strategies …
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now