sample code is the best documentation.  this code uses sun's JDBC-ODBC
bridge which has some quirkyness when working with my MS-SQL7:
        1. ResultSet fields can only be read once
        2. ResultSet fields must be read left to right

<%@ page import="
java.sql.*
" %>

<%
Connection conn = null;
String URL   = "JDBC:ODBC:<ODBC DSN name goes here />";

try {
 out.println("<!-- Load Sun's JDBC driver... -->");
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 out.println("<!-- DriverManager.setLogStream... -->");
        DriverManager.setLogStream(System.out);
 out.println("<!-- Open connection to the database... -->");
        conn = DriverManager.getConnection(URL);
}
catch (SQLException sqle) {
        out.println("Sorry, the database is not available.");
        out.println("Exception: " + sqle);
}
catch (Exception e) {
        out.println("Exception occured: " + e);
}

        Statement stmt          = null;
        ResultSet rs            = null;
        String query            = "exec sp_getGameToday";
        int numCols                     = -2;
        int question_number     = 0;
        String prompt           = "";
        String[] answer         = {"", "", "", "", "", ""};
        String[] letter         = {"", "a", "b", "c", "d", "e"};
        String correct_answer   = "b";
        String qname            = "";

        try {
                stmt = conn.createStatement();
                rs = stmt.executeQuery(query);
                ResultSetMetaData rsmd = rs.getMetaData();
                numCols = rsmd.getColumnCount();

                while (rs.next()) {
                        qname   = "qid_" + rs.getString("qid");
                        prompt  = rs.getString("prompt");
                        for (int i=1; i <= 5; i++) {
                                answer[i] = "";
                                answer[i] =

String.valueOf(rs.getString("answer"+i)).trim();
                                if (answer[i].equals("null"))
                                        answer[i] = "";
                        }
                        correct_answer  = rs.getString("correct_answers");
%>
                <p><font face="arial, helvetica, sans-serif"
size="-1"><%=++question_number%>. <%=prompt%>
<%
                        for (int i=1; i <= 5; i++) {
                                if (answer[i].length() > 0) {
%>
                <br>&nbsp;&nbsp;<input
                        type=radio
                        name="<%=qname%>"
                        value="<%=letter[i]%>"
<%
                                        if (
String.valueOf(request.getParameter(qname)).equals(letter[i]) )
                                                out.println(" checked");
                                        out.println("\t\t>");
                                        out.println(answer[i]);
                                        if (
String.valueOf(request.getParameter(qname)).equals(letter[i]) )
                                                if (
String.valueOf(request.getParameter(qname)).equals(correct_answer) )
                                                        out.println("
(<b>CORRECT</b>)");
                                                else
                                                        out.println("
(<b>Wrong</b>)");
                                }// if (answer[i].length() > 0)
                        } // for
                } // while
%>
                <p><font face="arial, helvetica, sans-serif"
size="-1"><input type=submit value="Sumbit"></font></p>
                <p>&nbsp;</p>
<%
        } // try
        catch (SQLException se) {
                out.println(se);
        }
        catch (Exception e) {
                out.println(e);
        }
        finally {
                try {
                        rs.close();
                        stmt.close();
                        conn.close();
                }
                catch (Exception e) {
                        out.print("<b>There was an error closing the
database connection</b>");
                        out.print("<br>Exception: " +e);
                }
        }
%>

    __o
  _ \<,_
 (_)/ (_)
~~~~~~~~~~~

> -----Original Message-----
> From: Pepe Ortiz [mailto:[EMAIL PROTECTED]]
> Sent: Friday, March 24, 2000 8:26 AM
> To: [EMAIL PROTECTED]
> Subject: HOW TO: Connect to a MS SQL7 DB with JSP
>
>
> Hi there, I'm new at JSP.
>
> I'm looking for a sample code that can connect to a MSSQL7 DB
> through a JSP
> page in order to do queries and display results. Can anyone
> share a similar
> code so I can see how its done.
>
> Thanks!
> -Pepe Ortiz
>
>
>
> =============================================================
> Jose "Pepe" Ortiz
> Virtual, Inc.
> Phone: 787.620.5000 / Fax: 787.620.5001 / Pager: 787.498.2534
>
> ==============================================================
> =============
> To unsubscribe: mailto [EMAIL PROTECTED] with body:
> "signoff JSP-INTEREST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to