Le 01:53 12/03/99 +0200, Oguz Baktir a �crit:
>Hi!!
>Is there anyone who succefully retrieved info from Access 97 databases. If
>so, can you tell me the step by step setting of the connection. How did you
>set the jdbc odbc connection?


Oguz,

The following code works.
It DOES NOT mean that its a GOOD way to do this (I'm learning servlets) for
example, perhaps its better to open the DB connection in the init() method.
This code looks in an Access 97 database (DSN name : BIC) where are stored
press releases and for the ten most recent ones gets the title and the date and
display them in a table.

Hope this help

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.math.*;
import java.io.*;
import java.net.*;
import java.util.*;


public class Accueil extends HttpServlet
{
    Connection con;

    public void init (ServletConfig config) throws ServletException
    {
        super.init(config);
    }
    //public void doPost(HttpServletRequest req, HttpServletResponse res)
    //        throws ServletException, IOException
    public void service(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException
    {

        //register a driver
        try
        {
                DriverManager.registerDriver (new
sun.jdbc.odbc.JdbcOdbcDriver());
        }
        catch(SQLException e)
        {
           log( "Probleme avec DriverMgr: " + e + '\t' );
        }
        //open a connection
        try
        {
          con = DriverManager.getConnection("jdbc:odbc:BIC", "","");
        }
        catch(SQLException e)
        {
           log( "Erreur Connexion: " + e + '\t' );
        }
        try
        {

        Statement stmt = con.createStatement();
        int columns = 0;//how many columns are returned in the ResultSet

        String sql = "SELECT * FROM Articles_News ORDER BY DateSaisie DESC ;";
        stmt.execute(sql);
        ResultSet rs1 = stmt.getResultSet();
        ResultSetMetaData rsmd = rs1.getMetaData();
        columns = rsmd.getColumnCount();

        res.setContentType("text/html");
        res.setDateHeader( "Expires", new java.util.Date().getTime() );
        PrintWriter out = res.getWriter();
        out.println(
        "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN//\">"+
        "<html>"+
       "<head>"+
        "<meta HTTP-EQUIV=\"expires\" content=\"0\">"+

        [... LOT OF HTML HERE....]
         );

        if(columns > 0)
        {

        out.println(

        [...LOT OF HTML HERE ...]

        );
        stmt.execute(sql);
        ResultSet rs = stmt.getResultSet();
        for(int i = 0; i<10; i++){
        if(rs.next())
        {
         //get the info here
        String strTitre       = rs.getString("Titre");
      String strDateSaisie   = rs.getString("DateSaisie");
       out.println(

        [...LOT OF HTML HERE: The data extracted from the database...]
        );
       }
       }
        out.println(
        [...LOT OF HTML HERE...]
        );
        }
        else
        {
        out.println(
        [...LOT OF HTML HERE...]
        );
        }

        out.println(
        [... LOT OF HTML HERE...]
        );
        out.flush();
        stmt.close();
        con.close();

        }
        catch(SQLException e)
        {
           //out.println
           log( "Erreur SQL: " + e + '\t' );
        }
   }
}

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to