Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JDBC how to connect j2me program with mysql using servlet?
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


mukesh
how to connect j2me program with mysql using servlet?
0 Answer(s)      3 years and 10 months ago
Posted in : JDBC


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


Advertisement
View Answers

Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com