/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

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

/**
 * @author Wallo
 */
public class testMySQL extends MIDlet implements CommandListener 
{
    private String user ;
    private String url = "http://localhost:8084/WebApplication2/getConnection/";
    private DB db;
    
    private Display display;
    Command cmdExit = new Command("Exit", Command.EXIT, 0);
    Command cmdConnect = new Command("Connect", Command.SCREEN, 0);
    
    private TextField tb;
    private Form menu;
    private TextField tb1;
    private TextField tb2;
    
    public testMySQL() throws Exception
    {
        display = Display.getDisplay(this);
    }
    
    public void startApp() 
    {
        displayMenu();
    }
    
    public void displayMenu()
    {
        menu = new Form("My form to Connect");
        tb = new TextField("Please enter DB name: ", "", 30, TextField.ANY);
        tb1 = new TextField("Please enter user name: ", "", 30, TextField.ANY);
        tb2 = new TextField("Please enter password: ", "", 30, TextField.PASSWORD);
        
        menu.append(tb);
        menu.append(tb1);
        menu.append(tb2);
        menu.addCommand(cmdExit);
        menu.addCommand(cmdConnect);
        menu.setCommandListener(this);
        display.setCurrent(menu);
    }

    public void pauseApp() 
    {
    }

    public void destroyApp(boolean unconditional) 
    {
    }
    
    public void commandAction(Command c ,Displayable s)
    {
        if(s == menu)
        {
            if(c == cmdExit)
            {
                destroyApp(true);
                notifyDestroyed();
            }
            else if(c == cmdConnect)
            {
                db = new DB(this);
                db.start();
                db.connectDb(tb.getString(), tb1.getString(), tb2.getString());
            }
        }
    }
    
    public class DB implements Runnable
    {
        testMySQL midlet;
        private Display display;
        private String dbName;
        private String user;
        private String password;

        private DB(testMySQL midlet) 
        {
            this.midlet = midlet;
            display = Display.getDisplay(midlet);
        }

        private 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-2.0");
                c.setRequestProperty("Content-Language","en-US");
                c.setRequestMethod(HttpConnection.POST);
                
                DataOutputStream os = (DataOutputStream)c.openDataOutputStream();
                os.writeUTF(dbName.trim());
                os.writeUTF(user.trim());
                os.writeUTF(password.trim());
                os.flush();
                os.close();
                
                
                DataInputStream is = (DataInputStream)c.openDataInputStream();
                sb = new StringBuffer();
                int ch;
                while((ch = is.read()) != -1)
                {
                    sb.append((char)ch);
                }
                showAlert(sb.toString());
                is.close();
                c.close();
            }
            catch(Exception ex)
            {
                showAlert("errrrrrrrrrror");
            }
        }
        
        private void connectDb(String dbNmae, String user, String password) 
        {
            this.dbName = dbNmae;
            this.user = user;
            this.password = password;
        }

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