Tutorials to .com

Tutorials to .com » Jsp » Database » J2ME visit procedures and Servlet Communication Access database

J2ME visit procedures and Servlet Communication Access database

Print View , by: iSee ,Total views: 19 ,Word Count: 2004 ,Date: Sat, 18 Apr 2009 Time: 7:36 PM

Authors: Wen Bo Time: May 26, 2005

j2me technology is a lot of networking, I have only just contacted this regard, the past few days a number ofdebug procedures in this regard, learned a lot, following on to talk about my learning process.

First of all, this procedure can be simplified into three levels,
1, J2ME client (MIDlet Procedure)
2, server-side servlet (servlet Procedure)
3, database (Access or SQL, etc.)

I passed a small example to achieve the client through the cell phone to send the request, and then the servlet to respond to server-side, servlet through the content of the database query Access, returned to the cell phone off

Client households and displayed.

Something very simple but involves many steps.

development Environment:

Operating System: Windows 2000 professional
JDK version: 1.4.2
WTK version: 2.1
Tomcat version: 5.0

If you do not understand is the JDK WTK tomcat anything, I suggest you look tutorial basic knowledge, I do not show here.

JDK version 1.4.0 or above must be, perhaps you use versions of the low and low and Tomcat versions is not bad. But I tried to use will be error Tomcat5.0 + JDK1.0.3

Unable to start the Tomcat server. The log in the Tomcat, it is written like this, "Unsuported JNI version 65537" so I used a relatively high version of the JDK.

Configured environment variables, not many here say. Test your environment variables. In the cmd window, type java - version to see if the version of JDK. Javac to see if input error, no

JDK proved wrong on the environment variable settings correct.

Then we edit the J2ME client, procedures constitute two categories. I am not a master, so the procedure can only be for the purpose of realization of functions, and what did not reflect the value of optimization.
--------------------------------- NetMain. java -------------- ---------------------------------------
import javax.microedition.midlet .*;
import javax.microedition.lcdui .*;

public class NetMain extends MIDlet implements CommandListener / / inherit MIDlet interface to achieve CommandListener
private display dis;
sendMsg sm;
private Command sd = new Command ( "Connect", Command.OK, 1); / / send data button
public NetMain ()
dis = Display.getDisplay (this); / / object to be displayed

public void startApp ()
Form f = new Form ( "networking ");// test displayed on the screen of the Form Object
f.append ( "send data");
f.addCommand (sd);
f.setCommandListener (this); / / set button monitor
dis.setCurrent (f);

public void pauseApp ()
public void destroyApp (boolean un)
public void exit ()
destroyApp (false);
notifyDestroyed ();
public void commandAction (Command c, Displayable d)
if (c == sd)
sm = new sendMsg (this); / / call sendMsg category, will be imported as a parameter in this category
dis.setCurrent (sm); / / display category sendMsg


-------------------------------------------------- ----------------------------------------
------------------------------------ sendMsg. java ----------- -------------------------------------------
import javax.microedition.lcdui .*;
import javax.microedition.io .*;
import java.io. *;

public class sendMsg extends Form implements CommandListener, Runnable / / inherit Form and the Runnable interface to achieve CommandListener
String url = ""; / / URL used in the definition of networking
String returnMsg = ""; / / get the return string
Command exit = new Command ( "left", Command.EXIT, 1); / / exit button
Thread t; / / definition of networking with thread
NetMain nm; / / NetMain examples

public sendMsg (NetMain nm) / / constructor function will be introduced to examples of NetMain
super ( "Return to Form the definition of the outcome of the title ");//
this.nm = nm;
t = new Thread (this);
t.start ();// start the thread

public void run ()
returnMsg = sendHttpGet (url);

/ / Url passed to doing sendHttpGet method parameters and return the result string server assigned to the returnMsg

/ / System.out.println (returnMsg);
append (returnMsg); / / return the results show that
addCommand (exit);
setCommandListener (this);

) catch (Exception e)
System.out.println (e);

public String sendHttpGet (String url) throws IOException
HttpConnection hconn = null; / / define the object of HTTP connections
DataInputStream dis = null; / / definition of data input stream
StringBuffer msg = new StringBuffer (); / / create a string StringBuffer store received
String s = "";
hconn = (HttpConnection) Connector.open ( "");// http://localhost:8080/returnMsg be connected object
dis = new DataInputStream (hconn.openInputStream ());// been the object of using the data stream as a parameter and then do constitute the parameters of data input stream

while (dis.readUTF ()! = null) / / determine when the data stream is not empty when
msg = msg.append (dis.readUTF ());// with readUTF () method on the string structure in StringBuffer

) catch (Exception e) ()
if (hconn! = null) / / close the connection
hconn.close ();
if (dis! = null)
dis.close ();


return msg.toString ();// will be through the StringBuffer the toString () method, create a new String and return to

public void show ()

public void commandAction (Command c, Displayable d)
if (c == exit)

nm.exit ();// exit procedures

-------------------------------------------------- -------------------------------------------------- ---------------

More than two categories, the Notes should. First of all, is a Form, above, put a button, when press the button when the thread to start networking to link URL, and then returns a string,

Displayed in a new Form in. Very simple is not it, huh, huh.
Now we come to the part of the design of server-side. The main procedure is to write a servlet. The following is a code
----------------------------------------- returnMsg. java ------ -------------------------------------------------- --------------

import java.sql .*;
import javax.sql .*;
import java.io. *;
import javax.servlet .*;
import javax.servlet.http .*;

public class returnMsg extends HttpServlet
public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
res.setContentType ( "text / html; charset = gb2312 ");// set to return to the type of

ServletOutputStream out = res.getOutputStream ();// get the output stream

DataOutputStream dos = new DataOutputStream (out);

dos.writeUTF ( "This is a test");
dos.writeUTF ( "The following is information");
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;


String drname = "sun.jdbc.odbc.JdbcOdbcDriver"; / / These are the drivers connect to database

Class.forName (drname);

String url = "jdbc: odbc: mydata";

conn = DriverManager.getConnection (url);

stmt = conn.createStatement ();
rs = stmt.executeQuery ( "select * from user ");// query the database user named Hank Greenberg

while (rs.next ())
dos.writeUTF ( "User ID:" + rs.getString ( "id "));// be field ID and field name content
dos.writeUTF ( "Username:" + rs.getString ( "name"));
rs.close ();
stmt.close ();
conn.close ();

) catch (Exception e)

-------------------------------------------------- -------------------------------------------------- -----
First of all, the written procedure for returnMsg.java compiler to generate. Class files. We generated a good class file directory on the Tomcat

webapps / root / web-inf / classes / inside, the attention must not be the wrong path.
And then have to edit the web-inf directory of the following web.xml file.
And then adding the information inside:
-------------------------------------------------- ------
<servlet-class> returnMsg </ servlet-class>
</ servlet>

<servlet-name> returnMsg </ servlet-name>
<url-pattern> / returnMsg </ url-pattern>
</ servlet-mapping>
-------------------------------------------------- --
You can put the contents inside the original removed.

Note: If you compile the following CMD file returnMsg.java, the emergence of import javax.servlet .*; the wrong time, remember to fill in the environment variable to add more than

Add in the classpath inside
c: \ Program Files \ Apache software foundation \ Tomcat 5.0 \ common \ lib \ servlet-api.jar is added to fill the servlet api search path.

Then we need to use access to generate a database file, table name is user, there are a few fields were auto-ID number on the line, name just a few names added. And then to

Database as a data source to connect.
Because our system is windows 2000 set up in the beginning --- ---- ----- Administrative Tools control panel --- ODBC data source points "add" attention to the DSN in the system inside the parade

For, and then put your database on a
Path is not written in Chinese characters, it can choose. Data source is the name of mydata

Now we can start the Tomcat server, and then we have just written to test the servlet, we open the IE browser, enter

Let's take a look at the effect ,,,,,,
What? Do nothing there, huh, huh, because we do not have to output anything inside the browser, of course, nothing,,, you can view the original document, if the data inside your

Database information, then the connection is successful. All the normal servlet.

Finally, we open the new WTK project file, and write it in front of us on the scr inside the two categories, and then build and then run point "connections", this time in the simulator, on

Will appear and just inside the original document in IE the same as the text.
Is not very happy. Ha ha!

Of course, in the actual development, not only the realization of such a simple function. I have also continued exploration and study, I hope that friends like you in the learning process of the experience. My QQ: 19197

Jsp And Database Articles

Can't Find What You're Looking For?

Rating: Not yet rated


No comments posted.