BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 05-07-2007, 05:40 PM   #1
AbrahamH
New Member
 
Join Date: May 2007
Model: 7290
PIN: N/A
Carrier: x
Posts: 1
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
fbrimm
Thumbs Must Hurt
 
Join Date: Aug 2005
Model: 8830
Carrier: Verizon
Posts: 144
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  
Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


Vintage RadioShack Model 33–4050 Analog Sound Level Meter Tested Working Used picture

Vintage RadioShack Model 33–4050 Analog Sound Level Meter Tested Working Used

$24.99



Vintage Countex II Coin Tray MMF Industries Bank Sorter Coin Counter picture

Vintage Countex II Coin Tray MMF Industries Bank Sorter Coin Counter

$10.99



Vintage Anchor USA Strapping Tool Made Strong 8

Vintage Anchor USA Strapping Tool Made Strong 8" Anchor Steel Collectible

$24.00



vintage mini label maker with tape label mate untested Dennison  picture

vintage mini label maker with tape label mate untested Dennison

$11.50



Vintage HEATHKIT Model V-7A Vacuum Tube Voltmeter, with Leads, Powers Up picture

Vintage HEATHKIT Model V-7A Vacuum Tube Voltmeter, with Leads, Powers Up

$35.00



Vintage Craftsman DC Outside Micrometer About 1 inch Opening USA Made picture

Vintage Craftsman DC Outside Micrometer About 1 inch Opening USA Made

$9.99







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