Step 1:
Create a J2ME mobile application project in Netbeans
step 2:
Create midlet naming home.java
step 3:
Copy the following code:
/** To change this template, choose Tools | Templates* and open the template in the editor.*/
package hello;
import javax.microedition.io.Connector;import javax.microedition.io.HttpConnection;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import java.io.*;
/*** @author Dinesh
*/public class home extends MIDlet implements CommandListener {
private Command check;TextField user,pass;StringItem label;Form s;public home(){user=new TextField("user","",25,TextField.ANY);pass=new TextField("pass","",25,TextField.PASSWORD);check =new Command("CHECK",Command.OK,1);label=new StringItem("","Press Check!!");s=new Form("Database Connection");s.append(user);s.append(pass);s.append(label);s.addCommand(check);s.setCommandListener(this);
}public void startApp() {
Display.getDisplay(this).setCurrent(s);}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void commandAction(Command c, Displayable d) {
if(c.equals(check)){String st="";String userr=user.getString();String passs=pass.getString();try{String url="http://localhost:8080/micro-web/mservlet?user="+userr+"&pass="+passs;HttpConnection cc=(HttpConnection)Connector.open(url);DataInputStream in=(DataInputStream)cc.openDataInputStream();int ch=0;while((ch=in.read())!=-1){st=st+(char)ch;}String str=st.toString();if(str.equals("suc")){label.setText("You are Authenticated!!");}else{label.setText("You are not Authenticated!!");}}catch(Exception e){System.out.println("Error :"+e);}}}}
step 4:
create a web-application project naming 'micro-web'
step 5:
create a servlet naming 'mservlet.java'
step 6:
copy the following code:
/** To change this template, choose Tools | Templates* and open the template in the editor.*/
package servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;
public class mservlet extends HttpServlet {
@Override
public void init() { }
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException,ServletException {PrintWriter out = response.getWriter();
String user=request.getParameter("user");
String pass=request.getParameter("pass");
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection("jdbc:odbc:iyn","","");
Statement st=connection.createStatement();
String query="select * from login where user='"+user+"'";ResultSet res=st.executeQuery(query);while(res.next()){if(res.getString("pass").equals(pass)){out.print("suc");}else{out.print("fail");}}}catch(Exception e){out.println("Error :"+e);}}
}
note: change the data source name (here it is given as 'iyn')and create new data source and database by
a)controlpanel-> DataSource(ODBC)->SystemDSN
b)click add button
c)select the microsoft access driver(*.mdb)
d)type a data source name in the corresponding text box
d)click the create button to create a database.
e)In the database create a table naming 'login' with username and password fields.
step 7: Run the servlet(in order to start the web server)
step 8: Run the mobile application... and enjoy the result..
No comments:
Post a Comment