how to connect j2me program with mysql using servlet?

Ads
 

how to connect j2me program with mysql using servlet?

my program of j2me

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

public class Demo extends MIDlet implements CommandListener { private String username; private String url = "http://localhost:8082/mobileDemo/DBConnection"; private Display display; private Command exit = new Command("EXIT", Command.EXIT, 1);; private Command login = new Command("Login", Command.SCREEN, 1); // private TextField tb; private Form menu; private TextField tb1; private TextField tb2; DBConn db;

  public Demo() throws Exception {
        display = Display.getDisplay(this);

  }

  public void startApp() {
        displayMenu();
  }

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

        tb1 = new TextField("Please input Mobile No: ","",30,TextField.ANY);

        tb2 = new TextField("Please input password: ","",30,TextField.PASSWORD);

        //menu.append(tb);
        menu.append(tb1);
        menu.append(tb2);
        menu.addCommand(exit);
        menu.addCommand(login);
        menu.setCommandListener(this);
        display.setCurrent(menu);
  }

  public void pauseApp() {}

  public void destroyApp(boolean unconditional) {}

  public void commandAction(Command command, Displayable screen) {
        if (command == exit) {
              destroyApp(false);
              notifyDestroyed();
        } else if (command == login) {
              db = new DBConn(this);
              db.start();
              db.connectDb(tb1.getString(),tb2.getString());
        }
  }

  public class DBConn implements Runnable {
        Demo midlet;
        private Display display;
       // String db;
        String user;
        String pwd;
        public DBConn(Demo 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-1.0, Configuration/CLDC-1.0");
                    c.setRequestProperty("Content-Language","en-US");
                    c.setRequestMethod(HttpConnection.POST);
                    DataOutputStream os = (DataOutputStream)c.openDataOutputStream();


                    os.writeUTF(user.trim());
                    os.writeUTF(pwd.trim());
                    os.flush();
                    os.close();

                    // Get the response from the servlet page.
                    DataInputStream is =(DataInputStream)c.openDataInputStream();
                    //is = c.openInputStream();
                    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());
              }
        }
        /* This method takes input from user like db,user and pwd and pass to servlet */
        public void connectDb(String user,String pwd) {

              this.user = user;
              this.pwd = pwd;
        }

        /* Display Error On screen*/
        private void showAlert(String err) {
              Alert a = new Alert("");
              a.setString(err);
              a.setTimeout(Alert.FOREVER);
              display.setCurrent(a);
        }
  };

}

servlet program is: import java.io.*; import java.util.*; import javax.sql.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*;

    public class DBConnection extends HttpServlet {
    public void init() { }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    DataInputStream in = new DataInputStream((InputStream)request.getInputStream());

    String user = in.readUTF();
    String pwd = in.readUTF();
    response.setContentType("text/plain");
    PrintWriter out = response.getWriter();
    String userid=connect(user.toLowerCase().trim(), pwd.toLowerCase().trim());
     if (userid == null)
    {
    response.sendError(response.SC_BAD_REQUEST, "Unable to locate user.");            
     return;
        }
    out.println(userid);
     out.close();
     in.close();
        out.flush();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
    }
    private String connect(String user,String pwd){

        String db="mobileapp";
        String username="root";
        String password="vertrigo";
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {


        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+db,username,password);

        stmt = con.createStatement();
        rs = stmt.executeQuery("SELECT * FROM emp where username='user' and password='pwd'");
        // displaying records
        if(rs.next()){
        return rs.getString(2);
        }
        else { return null; }

    } catch (Exception e) { 

     return e.toString(); }


  }

    }

and xml of servlet is :

DBConnection DBConnection

DBConnection /DBConnection

please help me its urgent for me

View Answers

Ads









Related Tutorials/Questions & Answers:
how to connect to database in php using mysql
How to connect mysql with jsp
Advertisements
how to delete a jar file from mobile by using j2me program.
How to connect to MySQL in JSP?
not able to connect to mysql using.. jdbc
How to write calculator in J2ME program?
how to connect program to database
need help....how to connect and disconnect multiple databases(databases created in mysql) using java and my sql
Servlets mysql connectivity
How to access (MySQL)database from J2ME?
how to connect client to server using Sockets
how to connect SQL Server 2005 using php
how to connect mySql drivers into eclipse(configuration)?
how to connect jsp to mysql - Java Beginners
servlet7
servlet4
servlet5
servlet3
j2me program
how to create a reminder app using threads in Servlets?
servlet2
j2me mysql connectivity
servlet6
servlet6
Connect a linux machine from linux using java program without password
How to connect to MySQL from command prompt?
Mysql connect
how to fetch image from mysql using jsp
J2me app with servlets
How to parse the attributes from XML using KXML parser in j2me
How To Connect MS ACCESS 2003 Database in C Program with Graphics.

Ads