×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    4

    Access database in MIDlet

    hie, i am new to J2Me and am currently doing a project on it.
    my question is how do we actually access the databases in MIDlet.
    for example, i have a servlet with codes running as below.

    import java.io.*;

    import java.util.*;

    import javax.servlet.*;

    import javax.servlet.http.*;


    import com.oreilly.servlet.CacheHttpServlet;



    public class Guestbook extends CacheHttpServlet {



    private Vector entries = new Vector();
    // User entry list
    private long lastModified = 0;
    // Time last entry was added


    // Display the current entries, then ask for a new entry


    public void doGet(HttpServletRequest req, HttpServletResponse res)

    throws ServletException, IOException {

    res.setContentType("text/html");

    PrintWriter out = res.getWriter();


    printHeader(out);

    printForm(out);
    printMessages(out);

    printFooter(out);

    }


    // Add a new entry, then dispatch back to doGet()


    public void doPost(HttpServletRequest req, HttpServletResponse res)

    throws ServletException, IOException {

    handleForm(req, res);

    doGet(req, res);

    }



    private void printHeader(PrintWriter out)
    throws ServletException {

    out.println("<HTML><HEAD><TITLE>Guestbook</TITLE></HEAD>");

    out.println("<BODY>");

    }



    private void printForm(PrintWriter out) throws ServletException {

    out.println("<FORM METHOD=POST>");

    // posts to itself


    out.println("<B>Please submit your feedback:</B><BR>");

    out.println("Your name: <INPUT TYPE=TEXT NAME=name><BR>");

    out.println("Your email: <INPUT TYPE=TEXT NAME=email><BR>");

    out.println("Comment:
    <INPUT TYPE=TEXT SIZE=50 NAME=comment><BR>");

    out.println("<INPUT TYPE=SUBMIT VALUE=\"Send Feedback\"><BR>");

    out.println("</FORM>");

    out.println("<HR>");

    }



    private void printMessages(PrintWriter out)
    throws ServletException {


    String name, email, comment;


    Enumeration e = entries.elements();

    while (e.hasMoreElements()) {

    GuestbookEntry entry = (GuestbookEntry) e.nextElement();

    name = entry.name;

    if (name == null) name = "Unknown user";

    email = entry.email;

    if (name == null) email = "Unknown email";

    comment = entry.comment;

    if (comment == null) comment = "No comment";

    out.println("<DL>");

    out.println("<DT><B>" + name + "</B> (" + email + ") says");

    out.println("<DD><PRE>" + comment + "</PRE>");

    out.println("</DL>");


    // Sleep for half a second to simulate a slow data source

    try { Thread.sleep(500);
    } catch (InterruptedException ignored) { }

    }

    }


    private void printFooter(PrintWriter out) throws ServletException {

    out.println("</BODY>");

    }


    private void handleForm(HttpServletRequest req,

    HttpServletResponse res) {

    GuestbookEntry entry = new GuestbookEntry();


    entry.name = req.getParameter("name");

    entry.email = req.getParameter("email");

    entry.comment = req.getParameter("comment");


    entries.addElement(entry);


    // Make note we have a new last modified time

    lastModified = System.currentTimeMillis();

    }



    public long getLastModified(HttpServletRequest req) {

    return lastModified;

    }

    }


    class GuestbookEntry {

    public String name;

    public String email;

    public String comment;

    }

    this code is extracted from O'reilly Java Servlet Programming.
    my problem is how to access the database created in this code and display them in the phone using J2ME?
    thanks...

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    7
    Hi,

    If you want to create and access a database in MIDlet, please consider RMS in MIDP.

    Some cell phones have the native Contact Database and Calendar Database which can be accessed by JavaPhone API. However no every phone supports JavaPhone API or JSR 75. Nokia 6630 supports JavaPhone API.

    If you want to run the database on server side, you can create a HttpConnection in the MIDlet to access the database through the servlet, JSP, ASP or other, which is running on the server.

    BR.

    JD_J2ME

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    4
    Hi, thanks for that.
    i am trying to do a midlet which can access the database from the servlet. i wish to update the database every minute from the servlet (in a FORM type) and the midlet will be able to download or retrieve the updated data from the database and display it in the screen of the phone.
    do you have any idea how to do this?
    thanks

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    7
    MIDP API does not support Servlet. I guess that you have the servlet running on server side to access database. Then you can use a HTTP connection in the MIDlet to send requests and get responses to and from the servlet which accesses data in the database.

    Please refer to the MIDP specification for how to use HTTP to
    retrieve the data from the database on server side and display it in the screen of the phone, available from:
    java.sun.com/products/midp

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    4
    sorry to trouble again.
    about the server to access the database instead of the MIDlet, is there available example or tutorial on how to do that?

    Or will you be able to post some code on how to do this? i not sure how to write the servlet to enter data in a FORM to a database, and how to write the servlet to access the same databases ?
    thanks a lot...

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    7
    You can use JDBC in the servlet to access Database.
    Or if you are using J2EE server, you can servlet + EJB to access database.
    You can find all kinds of free J2EE and JDBC tutorials in the Internet. The tutorials have many samples.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    4
    Thanks for the information..
    it really helps a lot...

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    Re: Access database in MIDlet

    l made 1 servlet pgm.where l hav to save dis...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×