×

Discussion Board

Results 1 to 12 of 12
  1. #1
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    108

    Smile Retrieving information from a DB

    Hello all

    I want to have my emulator retrieve information from a MySQL database on another machine. What protocol should I use for this..how should I start?
    (if there is any step by step guide available, please do share)

    Many thanks
    =)
    Last edited by hm991; 2010-03-21 at 06:46.

  2. #2
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,085

    Re: Retrieving information from a DB

    Write a server side script(servlet/JSP/PHP) that can retrieve information from MySQL database and make it available to the mobile device over HTTP connection.

    Now on the mobile client side, write a small application can send the request to get the required data from server ovedr HTTP connection.

    refer to example: http://wiki.forum.nokia.com/index.ph...est_in_Java_ME

    thanks,
    ~Amitabh

  3. #3
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    108

    Smile Re: Retrieving information from a DB using Tomcat

    thanks Amitabh, I'm trying to work with that piece of code..I'm trying to use Tomcat with it..

  4. #4
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    108

    Re: Retrieving information from a DB

    in the 'try' block in the function 'sendGetRequest()', I've added 2 System.out.println() statements:

    public String sendGetRequest(String urlstring)
    { HttpConnection hc = null;
    DataInputStream dis = null;
    String message = "";
    try
    {
    hc = (HttpConnection) Connector.open(urlstring);
    System.out.println("in try");//-->>
    // establishing input stream from the connection
    dis = new DataInputStream(hc.openInputStream());
    System.out.println("in try2");//-->>
    // reading the response from web server character by character
    int ch;
    while...

    when I run the midlet, it gives an error(exception handler), and it only displays "in try", and not "in try2", meaning there's something wrong with the inputstream. I can't figure out what though

  5. #5
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,085

    Re: Retrieving information from a DB

    Refer to API doc for WTK, httpconnection in IO package.

    PHP Code:
     void getViaHttpConnection(String urlthrows IOException {
             
    HttpConnection c null;
             
    InputStream is null;
             
    int rc;

             try {
                 
    = (HttpConnection)Connector.open(url);

                 
    // Getting the response code will open the connection,
                 // send the request, and read the HTTP response headers.
                 // The headers are stored until requested.
                 
    rc c.getResponseCode();
                 if (
    rc != HttpConnection.HTTP_OK) {
                     throw new 
    IOException("HTTP response code: " rc);
                 }

                 
    is c.openInputStream();

                 
    // Get the ContentType
                 
    String type c.getType();

                 
    // Get the length and process the data
                 
    int len = (int)c.getLength();
                 if (
    len 0) {
                     
    int actual 0;
                     
    int bytesread ;
                     
    byte[] data = new byte[len];
                     while ((
    bytesread != len) && (actual != -1)) {
                        
    actual is.read(databytesreadlen bytesread);
                        
    bytesread += actual;
                     }
                 } else {
                     
    int ch;
                     while ((
    ch is.read()) != -1) {
                         ...
                     }
                 }
             } catch (
    ClassCastException e) {
                 throw new 
    IllegalArgumentException("Not an HTTP URL");
             } 
    finally {
                 if (
    is != null)
                     
    is.close();
                 if (
    != null)
                     
    c.close();
             }
         } 
    This type of code should work.

    thanks,
    `Amitabh

  6. #6
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    108

    Re: Retrieving information from a DB

    thanks!

    I now have a midlet, and a class, within a mobile application project. The class is not accepting the following imports, because of which the program refuses to run:

    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;


    what could the problem possibly be? any ideas?

  7. #7
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,085

    Re: Retrieving information from a DB

    Within a mobile application project, you cannot import all JDK packages please restrict to J2me java packages only. Ref. to API doc of SUM WTK.
    We dont have these below in MIPD/CLDC implementation of Java and J2me KVM does'nt support it.
    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    thanks,
    ~Amitabh

  8. #8
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    108

    Re: Retrieving information from a DB

    thanks i thought so too..

    In the article available here: http://www.vogella.de/articles/JavaME/article.html, I believe they have created a midlet and a servlet inside a web application project(I mean that's what they would probably have done if they were using NetBeans instead of the Wireless Toolkit)

    I am unable to create a midlet in a Web Application, and there's no 'servlet' option in a Mobile Application, unless 'Java ME Web Service Client', or, 'Java ME Client to Web Application' refer to creating a 'servlet'.

  9. #9
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,085

    Re: Retrieving information from a DB

    This Article say's you develop Midlet app in WTK and Servlet in Eclipse.
    You cant make both in WTK.

    In Eclipse or Netbeans, you can make them as seperate projects with seperate depencies on JDK (for core Java Servlet)& WTK (for MIDlet)

    thanks,
    ~Amitabh

  10. #10
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    108

    Re: Retrieving information from a DB

    thanks..presently I'm trying to figure out how to link the 2 together

  11. #11
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,085

    Re: Retrieving information from a DB

    No to link them together, the midlet will be installed on a device(emulator) and servlet will be hosted on some public server(localhost/IIS for local testing).

    Then the midlet will call this server using GCF on some public IP or local host.

    thanks,
    ~Amitabh

  12. #12
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    108

    Re: Retrieving information from a DB

    wow that's a lot clearer!
    any ideas on how to connect Mysql with Tomcat? because I've spent quite a few hours searching for it but no luck..

Similar Threads

  1. Retrieving SIM information
    By kedarm in forum Symbian
    Replies: 9
    Last Post: 2009-12-28, 07:27
  2. Data Structures - storing information
    By BarataPT in forum Mobile Java General
    Replies: 1
    Last Post: 2009-12-22, 19:42
  3. AT commands - sending ringtone using PDU
    By liangzhuge in forum Smart Messaging
    Replies: 39
    Last Post: 2007-04-06, 10:18
  4. Prototype SDK 2.0 setup problem
    By jackmcardle in forum Mobile Java Tools & SDKs
    Replies: 4
    Last Post: 2007-01-09, 16:21
  5. intalled application version information
    By petxolobo in forum Symbian
    Replies: 3
    Last Post: 2006-10-12, 06:40

Posting Permissions

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