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


Voltage Electricity Tester Volt Detector Test Pen AC Non-Contact Sensor 90-1000V picture

Voltage Electricity Tester Volt Detector Test Pen AC Non-Contact Sensor 90-1000V

$15.94



LED AC Electric Voltage Power Detector Sensor Tester Non-Contact Pen 12-1000V picture

LED AC Electric Voltage Power Detector Sensor Tester Non-Contact Pen 12-1000V

$12.49



AVR SX460 Automatic Voltage Volt Regulator Replacement For Stamford Generator picture

AVR SX460 Automatic Voltage Volt Regulator Replacement For Stamford Generator

$17.18



100A AC Meter Ammeter Volt Energy Voltage Power LCD Display Monitor Panel picture

100A AC Meter Ammeter Volt Energy Voltage Power LCD Display Monitor Panel

$17.09



12-1000V Dual Sensitivity Electrical Tester Pen Non-Contact AC Voltage Detector picture

12-1000V Dual Sensitivity Electrical Tester Pen Non-Contact AC Voltage Detector

$13.99



Waterproof CAR Battery Meter DC 12V Voltmeter LED Digital Display Voltage Gauge picture

Waterproof CAR Battery Meter DC 12V Voltmeter LED Digital Display Voltage Gauge

$10.49







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