> -----Original Message-----
> From: A mailing list about Java Server Pages specification
> and reference
> [mailto:[EMAIL PROTECTED]]On Behalf Of Guilherme - PerConsult
> Sent: 06 December 1999 17:32
> To: [EMAIL PROTECTED]
> Subject: using ResultSet with JSP?
>
>
> Hi all...
>
> I have to make a query on a DB, and I made this code:
>
> =============
>
> <html>
> <body>
>
> <p>gui 1</p>
>
> <%@ page import =
> "java.io.*,java.util.*,java.net.*,java.sql.*,javax.servlet.*,j
> avax.servlet.http.*"%>
>
> <%!
>
> String url = "jdbc:odbc:teste";
> Connection con;
> Statement stmt;
>
>
> public void jspInit()
> {
> System.out.print("starting conection");
> //super.init(servletConfig);
> try
> {
> Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
> con = DriverManager.getConnection(url,"","");
> stmt = con.createStatement();
> }
> catch(SQLException sql)
> {
> System.out.println("init error");
> }
> catch(ClassNotFoundException fnf)
> {
> System.out.println("init error");
> }
> System.out.println("ready!");
> }
> public void jspDestroy()
> {
> System.out.print("closing conection");
> try
> {
> stmt.close();
> con.close();
> }
> catch(SQLException sql)
> {
> System.out.println("init error");
> }
> System.out.println("ready!");
> }
> %>
>
> <p>gui3</p>
>
> <%
> String pesquisa = "select * from pessoa where nome = 'gui2'";
> stmt.executeQuery(pesquisa);
> %>
>
> <p>fim</p>
>
> </body>
> </html>
>
> ==================
>
> On Control Panel I set my database as "teste" (the bridge). I
> have some
> data on the table "pessoa" to make the query also..
>
> Well.... the code is okay. But I don`t know how to print the
> result of
> this query on the screen.... in the JSP file...
>
> Can anybody help me???
executeQuery() returns a ResultSet object which allows you to iterate
through each row of the results of the query. You access the values of the
columns using getXXX() methods - eg getString( "BOB" ) gets the BOB column
as string.
eg in your example:
<%
String pesquisa = "select * from pessoa where nome = 'gui2'";
ResultSet result = stmt.executeQuery(pesquisa);
while( result.next() ) {
out.println( "BOB=" + result.getString( "BOB" ) + "<BR>" );
}
%>
This will display all the values (if any) of the BOB column, found in your
query.
(replace "BOB" with a column name in your table).
See the API docs for java.sql for more information.
Hope that helps,
Steve
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html