×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    15

    Database Connection from mobile not happening

    Hi Everybody,

    I'm very new to J2ME, and I'm writing a sample code to retrieve the values from the database and display it on my mobile screen. I'm able to extract the data from the DB in an emulator. But when I try in the mobile I'm neither getting any error nor am I able to create in to the database. I have tried various mobiles which supports MIDP 2.0, but was not successful.
    This is my midlet code:

    package hello;

    import java.io.*;
    import java.util.*;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.io.*;

    public class HelloMIDlet extends MIDlet implements CommandListener {
    private StreamConnection streamConnection = null;
    private InputStream inputStream = null;


    private String url =
    "http://192.168.39.19:8084/ServletforTest/getConnection";

    private Display display;
    private Command exit = new Command("EXIT", Command.EXIT, 1);;
    private Command connect = new Command("Connect", Command.SCREEN, 1);
    private TextField tb;
    private Form menu;
    private TextField tb1;
    private TextField tb2;
    DB db;

    private Command exitCommand;
    private Form form;
    private StringItem stringItem;

    public HelloMIDlet() {
    display = Display.getDisplay(this);
    }

    private void initialize() {
    }

    public void startMIDlet() {
    // write pre-action user code here
    switchDisplayable(null, getForm());
    // write post-action user code here
    }

    public void resumeMIDlet() {
    }

    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {

    Display display = getDisplay();
    if (alert == null) {
    display.setCurrent(nextDisplayable);
    } else {
    display.setCurrent(alert, nextDisplayable);
    }

    }

    public void commandAction(Command command, Displayable displayable) {

    if (displayable == form) {
    if (command == exitCommand) {
    exitMIDlet();
    }
    }

    if (command == exit) {
    destroyApp(false);
    notifyDestroyed();
    } else if (command == connect) {
    db = new DB(this);
    db.start();
    }
    }

    public Command getExitCommand() {
    if (exitCommand == null) {
    exitCommand = new Command("Exit", Command.EXIT, 0);
    }
    return exitCommand;
    }

    public Form getForm() {
    if (form == null) {
    form = new Form("Welcome", new Item[] { getStringItem() });
    form.addCommand(getExitCommand());
    form.setCommandListener(this);
    }
    return form;
    }

    public StringItem getStringItem() {
    if (stringItem == null) {
    stringItem = new StringItem("Hello", "Hello, World!");
    }
    return stringItem;
    }

    public Display getDisplay () {
    return Display.getDisplay(this);
    }


    public void exitMIDlet() {
    switchDisplayable (null, null);
    destroyApp(true);
    notifyDestroyed();
    }


    public void startApp() {
    displayMenu();
    }

    public void displayMenu() {
    menu = new Form("Connect");

    menu.addCommand(exit);
    menu.addCommand(connect);
    menu.setCommandListener(this);
    display.setCurrent(menu);
    }

    public void pauseApp() {
    }
    public void destroyApp(boolean unconditional) {
    }

    public class DB implements Runnable {
    HelloMIDlet midlet;
    private Display display;
    String db;
    String user;
    String pwd;
    public DB( HelloMIDlet midlet) {
    this.midlet = midlet;
    display = Display.getDisplay(midlet);
    }

    public void start() {
    Thread t = new Thread(this);
    t.start();
    }

    public void run() {

    StringBuffer sb = new StringBuffer();
    try {
    HttpConnection c = (HttpConnection)Connector.open(url);
    c.setRequestProperty("User-Agent","Profile/MIDP-2.0, Configuration/CLDC-1.0");
    c.setRequestProperty("Content-Language","en-US");
    c.setRequestMethod(HttpConnection.POST);


    DataOutputStream os = (DataOutputStream)c.openDataOutputStream();
    os.flush();
    os.close();

    InputStream is =c.openInputStream();
    int len = (int)c.getLength();
    int ch;
    sb = new StringBuffer();
    while ((ch = is.read()) != -1) {
    sb.append((char)ch);
    }
    showAlert(sb.toString());
    is.close();
    c.close();
    } catch (Exception e) {
    showAlert(e.getMessage());
    }
    }

    private void showAlert(String err) {
    Alert a = new Alert("");
    a.setString(err);
    a.setTimeout(Alert.FOREVER);
    display.setCurrent(a);
    }
    };
    }


    This is my servlet code:

    package servlet;

    import java.io.*;
    import java.text.*;
    import java.util.*;
    import javax.servlet.*;
    import java.sql.*;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public class getConnection extends HttpServlet {
    private Connection conn;

    public void init() {
    }


    // @Override
    public void doGet(HttpServletRequest request,
    HttpServletResponse response) throws ServletException,
    IOException {

    doPost(request,response);
    }


    //@Override
    public void doPost(HttpServletRequest request,
    HttpServletResponse response) throws ServletException,
    IOException {
    PrintWriter out = response.getWriter();
    System.out.println("This is inside ");


    DataInputStream in = new DataInputStream(
    (InputStream)request.getInputStream());

    String db ="system";
    String user ="Stridus_demo";
    String pwd = "Stridus_demo";
    Statement stmt=null;
    ResultSet rs=null;
    String ur = "";



    String message ="jdbcracle:thin:@122.166.39.27:1521:"+db+","+user+","+pwd;
    try {

    connect(db.toLowerCase().trim(),user.toLowerCase().trim(),
    pwd.toLowerCase().trim());

    stmt= conn.createStatement();

    rs=stmt.executeQuery("select name from MOBILETESTGARUDA");

    while(rs.next()){
    ur = rs.getString(1)+" & " + ur;
    }

    response.setContentType("text/html");
    response.setContentLength(ur.length());
    out.println(ur);


    } catch (Throwable t) {
    message += "200 " + t.toString();
    }
    finally
    {
    try {

    out.close();
    out.flush();
    rs.close();
    stmt.close();
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }


    private void connect(String db, String user,String pwd) throws Exception {

    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    conn = DriverManager.getConnection("jdbcracle:thin:@122.166.39.27:1521:"+db,user,pwd);
    }
    }

    As i have said this is working absolutely fine in the emulator, but not in the actual device.
    Is there a problem in the code itself or I'm not having a proper mobile to execute this code or is there any particular settings that has to be changed in mobile to execute this particular code.

    Thanks and Regards
    Garudadwajan.R

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Database Connection from mobile not happening

    192.168.39.19 looks like the address on your local network. Do you use a different address when you put this on a phone?

    Cheers,
    Graham.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    15

    Re: Database Connection from mobile not happening

    Oh ya..sorry about that...I didn't change the IP while uploading the code now...

    I'm using external IP when I'm uploading the .jad file.

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Database Connection from mobile not happening

    OK. Do you have any other Java application that connects to the network? Are you sure the device's network settings are correct? Try downloading http://mini.opera.com, see if that application works. See if you can access your server's URL using Opera Mini.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    15

    Re: Database Connection from mobile not happening

    Yes!! I have done that too!!!! In the browser's url I typed the address specified in the midlet with external IP and I'm able to output the value of the DB in my screen!!!!!

    The problem comes when I try to connect to the servlet from my midlet.

    Another info I'd like to share is...

    I'm using netbeans for my project. Both my servlet and my midlet are in seperate "projects". So before I send my jar and jad files to the mobile, I'm also deploying the servlet.

    That has to be done everytime or once done is good enough?

  6. #6
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Database Connection from mobile not happening

    Which browser? Install Opera Mini and try that, not the device's internal browser. The browser may use different settings to Java apps.

    What happens? Does the application lock, or is there an exception?

    Cheers,
    Graham.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    15

    Re: Database Connection from mobile not happening

    Hi Graham,

    I'm not at all using any browser here. Only for cross checking the code I typed the url in the browser and it is working absolutely fine.

    I'm installing the app directly and as soon as I run my app, it should display me the value directly from the DB. I'm not using any browser as a medium. The output I'm trying to portray is just a normal display type.

  8. #8
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Database Connection from mobile not happening

    I realize that. But, please do install Opera Mini and try the URL in that. You need to find out if the phone is configured correctly.

    Graham.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    15

    Re: Database Connection from mobile not happening

    Hi Graham,

    I tried with opera mini...itz working in that browser too..

  10. #10
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Database Connection from mobile not happening

    OK. So, when you run your application, what happens?

    Also: why do you POST, and open and close an OutputStream, when you don't appear to write any data?

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    15

    Re: Database Connection from mobile not happening

    Absolutely nothing!!!!

    First it asks for sending of HTTP information. Den it'll just be blank.
    Nothing happens.

    But if I try to connect it while it is happening I get some info like TCP open.

  12. #12
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Database Connection from mobile not happening

    OK. Next, try this:

    PHP Code:
    import java.io.*;
    import javax.microedition.io.*;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;

    public class 
    HttpReader extends MIDlet implements CommandListenerRunnable {

        private static final 
    Command cmdGET  = new Command("GET"Command.SCREEN1);
        private static final 
    Command cmdEXIT = new Command("Exit"Command.EXIT, 1);

        
    /// PUT YOUR URL HERE
        
    private static final String DOCUMENT_URL "http://";

        private 
    Form form;
        private 
    StringItem status;
        
        public 
    void startApp() {
            
    Display disp Display.getDisplay(this);

            
    form = new Form("HttpReader");
            
    form.addCommand(cmdEXIT);
            
    form.addCommand(cmdGET);
            
    form.setCommandListener(this);

            
    status = new StringItem(null"Ready");
            
    form.append(status);

            
    disp.setCurrent(form);
        }

        public 
    void pauseApp() {
        }

        public 
    void destroyApp(boolean bUnconditional) {
        }

        private 
    void setStatus(String s) {
            
    status.setText(s);
        }

        
    // implementation: CommandListener
        
        
    public void commandAction(Command cmdDisplayable disp) {
            if (
    cmd == cmdEXIT) {
                
    destroyApp(false);
                
    notifyDestroyed();
            } else if (
    cmd == cmdGET) {
                
    form.removeCommand(cmdGET);
                
    form.removeCommand(cmdEXIT);
                
                (new 
    Thread(this)).start();
            }
        }

        
    // implementation: Runnable

        
    public void run() {
            try {
                
    setStatus("Connecting...");
                
    HttpConnection con =(HttpConnectionConnector.open(DOCUMENT_URL);

                try {
                    
    con.setRequestMethod(HttpConnection.GET);
                    
    int len = (int) con.getLength();

                    if (
    len >= 0) {
                        
    byte[] ao = new byte[len];
                        
    DataInputStream din con.openDataInputStream();
                        try {
                            
    din.readFully(ao);
                        } 
    finally {
                            
    din.close();
                        }

                        
    form.append(new String(ao));
                    } else {
                        
    setStatus("no length");
                    }
                } 
    finally {
                    
    con.close();
                }
            } catch (
    Exception e) {
                
    setStatus("Error: " e);
            } 
    finally {
                
    form.addCommand(cmdEXIT);
            }
            
    setStatus("Complete.");
        }


  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    15

    Re: Database Connection from mobile not happening

    Now I'm getting a null pointer exception with this code...
    It says there is some error in startApp..

    This is what he error says:

    startApp threw an Exception
    java.lang.NullPointerException
    java.lang.NullPointerException
    at javax.microedition.lcdui.Displayable.addCommand(Displayable.java:300)
    at hello.HelloMIDlet.startApp(+29)
    at javax.microedition.midlet.MIDletProxy.startApp(MIDletProxy.java:43)
    at com.sun.midp.midlet.Scheduler.schedule(Scheduler.java:374)
    at com.sun.midp.main.Main.runLocalClass(Main.java:466)
    at com.sun.midp.main.Main.main(Main.java:120)

  14. #14
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Database Connection from mobile not happening

    That's because you've mixed bits of my code with bits of yours.

  15. #15
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: Database Connection from mobile not happening

    Hi,
    I guess you have mixed the commands..and either of the command is null,
    please check the same,
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

Similar Threads

  1. Replies: 3
    Last Post: 2008-09-09, 04:53
  2. WIndows Mobile connection to E51 not happening, help
    By robertlaurali in forum Bluetooth Technology
    Replies: 2
    Last Post: 2008-09-03, 09:25
  3. Starting Point for Database Connection App
    By jayshine811 in forum Symbian
    Replies: 2
    Last Post: 2008-06-07, 22:34
  4. Replies: 0
    Last Post: 2007-07-15, 23:06
  5. Mobile Podcasting - will it happen?
    By mobile monte in forum News and Announcements
    Replies: 0
    Last Post: 2006-09-22, 23:45

Posting Permissions

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