Use J2ME to establish database connections with ASP

xiaoxiao2021-03-06  101

Use J2ME to establish database connections with ASP

J2ME uses HTTPConnection to establish an HTTP connection, then obtain data, ASP also uses the HTTP protocol, so you can use J2ME to establish a connection to ASP, thereby accessing the database. ASP is Microsoft's server-side dynamic page technology, which can run the server-side program and return the result, which also provides many useful features to MIDP. Since the MIDP client handles are not strong, you can put some processing tasks on the server side, using ASP to complete some tasks that it cannot be completed, such as the database. A simple example is given here, indicating how the MIDP interacts with the ASP to complete the database operation.

(1) Make sure the IIS web server works normally.

(2) Establish a test database: use the Access database to generate a database file to save to D: / j2me / j2metest / IIS / datebase, the file name is j2metest.mdb. Then create a database table -Message, the created table contains the following Field: ID (Auto Number), Message (Text), IP (Text), Date.

(3) Creating an ASP program

The following is the ASP program source code, the file name is Connectasp.asp, saved to D: / J2ME / J2meteSt / IIS / ASP.

  // Connectasp.asp <% @ language = VBScript%> <% strdblocation = server.mappath ("/ database / j2metest.mdb); strconnectionstring =" proviker = microsoft.jet.oledb.4.0; data source = " strDBLocation; var cnn = Server.CreateObject ( "ADODB.Connection"); cnn.Open (strConnectionString); rs = Server.CreateObject ( "ADODB.Recordset"); ip = Request.ServerVariables ( "REMOTE_ADDR"); message = Request.QueryString ("Message"); rs.activeconnection = cnn; rs.cursortype = 1; rs.locktype = 2; rs. Source = "Message"; rs.Open (); rs.addNew (); rs (" ") = Message; rs (" ip ") = IP; RS (" Date ") = (new date ()). Getvardate (); rs.Update (); rs.close (); cnn.close () Response.write ("The Message WriteDfully./N");%>

Then enter the following URL in the browser: http://localhost/asp/connectasp.asp? Message = Hello J2ME!

If the run is successful, the browser outputs a statement "The Message WeiteDfully.", Then open the database file to view the table Message, and a record has been successfully written. ID is 1, Message is Hello J2ME, IP 127.0.0.1, Date is the current date. (4) Write the MIDLET program

Next, write the MIDP program, create an HTTP connection in the MIDLET, and the URI points to this ASP file and attached to the parameter.

//Connectasp.javaImport javax.microedition.midlet. *; Import javax.miceoedition.io. *; Import java.io. *;

Public class connectasp eXtends MIDlet {public void startapp () {Testasp (); test ASP connection} catch (ooexception e) {system.out.println ("error");} notifydestroyed ();}

void testASP () throws IOException {try {String uri = "http: //localhost/asp/ConnectASP.asp message = Hello J2ME?!"; HttpConnection conn = (HttpConnection) Connector.open (uri); InputStream in = conn .openputstream (); int Ch; while ((ch = in.read ()! = - 1) {System.out.Print ((char) ch);} in.close (); conn.close (); Catch (ConnectionNotFoundExcection E) {system.out.println ("http could not be opened";}}

Public void pauseapp () {} public void destroyApp (boolean unconditional) {}}

Compile and run this MIDLET, the console output statement "The Message Write SuccessFully." And then open the database to see the table Message, and a record has been successfully written.

转载请注明原文地址:https://www.9cbs.com/read-123990.html

New Post(0)