BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 05-07-2007, 05:40 PM   #1 (permalink)
New Member
 
Join Date: May 2007
Model: 7290
PIN: N/A
Carrier: x
Posts: 1
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Ho to create an application with database connection

Please Login to Remove!

Hi:
I'm new in BB development I've created some sample application in JDE but i'd like to create one with database connection, I just want to do a simple application execute some select query(quite simple) Mostly im intesest in the connection process i have no idea how to connect a mobile application with a database
Thank You
Offline  
Old 05-11-2007, 08:48 AM   #2 (permalink)
Thumbs Must Hurt
 
Join Date: Aug 2005
Model: 8830
Carrier: Verizon
Posts: 144
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello AbrahamH,

The BB doesn't really connect directly to a database. It will require either a web service or a web page on the server that the BB will access for its database access.

If you can use a web service, then the MDS Studio should be fairly easy to get something working.

If you need a web page, then your BB app will have to send/receive data via an http connection. Search around for http connection and there is lots of good examples out there.

I have used a web page to retrieve data from the database that then "writes" the data in XML format.

Here's some sample code to get you started.

Code:
    public static void readdb(String _param) {
        String XMLvalues = "";

        // Pass parameter to url
        String url = "http://yourserver.com/yourpage.aspx?param="+_param+";deviceside=false";
            
        // Load data from server
        XMLvalues = getHttp(url);
            
        // Process data returned....



    }

    public static String getHttp(String url) {
        String XMLvalues = "";
        
        HttpConnection h = null;
        InputStream dis = null;
        OutputStream dos = null;
        int response;
        boolean keepGoing = true;
        boolean _error = false;
        int retry = 3;
        
        try {
            h = (HttpConnection)Connector.open(url);
            
            // Set the request method and headers
            h.setRequestMethod(HttpConnection.POST);
            h.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
            h.setRequestProperty("Content-Language", "en-US");            

            dis = h.openInputStream();
    
            while (keepGoing) {
                int status = h.getResponseCode();
                switch (status) {
                    case (HttpConnection.HTTP_OK):
                        // Connection is 200 OK.
                        // Ready to send data.
                        keepGoing = false;
                        break;
                        
                    case (HttpConnection.HTTP_BAD_REQUEST):
                        // Bad Request, try again
                        retry--;
                        if (retry > 0) {
                            h.close();
                        
                            // Open a new connection.
                            h = (HttpConnection)Connector.open(url);
                            
                            // Set the request method and headers
                            h.setRequestMethod(HttpConnection.POST);
                            h.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
                            h.setRequestProperty("Content-Language", "en-US");            
                
                            dis = h.openInputStream();
                        } else {
                            _error = true;
                            keepGoing = false;
                        }
                        break;
                            
                    case (HttpConnection.HTTP_NOT_FOUND):
                        // File not found
                        _error = true;
                        keepGoing = false;
                        break;
                        
                    default:
                        // The connection failed for some other reason.
                        _error = true;
                        keepGoing = false;
                        break;
                }
            }

            // Read input if no errors
            if (!_error) {
                final StringBuffer sb = new StringBuffer();
                int ch;
                
                String type = h.getType();

                while ((ch = dis.read()) != -1) {
                    if (ch > 0x7F) ch = 0x20;
                    sb.append((char) ch);
                }
                XMLvalues = sb.toString();
            }
            
        } catch (IOException ioe) {
            System.out.println(ioe.toString());
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (Exception e) {
                }
            }
            if (dis != null) {
                try {
                    dis.close();
                } catch (Exception e) {
                }
            }
            if (h != null) {
                try {
                    h.close();
                } catch (Exception e) {
                }
            }
        }
    
        // Return results
        return XMLvalues;
    }
fbrimm
Offline  




Copyright 2004-2016 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.