Using J2ME to establish database connection with ASP

Source: Internet
Author: User
Tags iis access database

J2ME is the use of httpconnection to establish an HTTP connection, and then obtain data, ASP is also using the HTTP protocol, so you can use J2ME to establish a connection with the ASP to access the database. ASP is Microsoft's server-side dynamic page technology that can run server-side programs and return results on request, which also provides many useful features for MIDP. Because the MIDP client processing capacity is not strong, some processing tasks can be placed on the server side, using ASP to complete some of the tasks it can not complete, such as the operation of the database. Here is a simple example of how MIDP interacts with ASP to complete database operations.

(1) Ensure that the IIS Web server is working properly.

(2) Establish a test database: Use Access database, generate a database file saved to D:\J2ME\J2METEST\IIS\DATEBASE, file name is J2metest.mdb. Then create a new database table-message, The table you create contains the following fields: ID (AutoNumber), message (text), IP (text), date (date).

(3) Create ASP Program

The following is the ASP program source code, file name is connectasp.asp, save to D:\J2ME\J2METEST\IIS\ASP.


Connectasp.asp
<% @LANGUAGE =vbscript%>
<%
Strdblocation=server.mappath ("/database/j2metest.mdb");
Strconnectionstring= "Provider=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") =message;
RS ("IP") =ip;
RS ("date") = (new Date ()). getVarDate ();
Rs. Update ();
Rs. Close ();
Cnn. Close ();
Response.Write ("The Message writed successfully.\n");
%>


Then enter the following URL in the browser:Http://localhost/asp/ConnectASP.asp?message=Hello+J2ME!

If the operation succeeds, the browser outputs the statement "the message weited successfully." And then opens the database file to view the table message and a record has been successfully written. The ID is 1,message to Hello J2me,ip for 127.0.0.1,date is the current date.

(4) Writing MIDlet program

Next, you write a MIDP program that creates an HTTP connection in the MIDlet, with the URI pointing to the ASP file with the parameters attached.

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

public class Connectasp extends midlet{
public void startApp () {
try{
Testasp (); Test ASP Connection
}catch (IOException 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.openinputstream ();
int ch;
while ((Ch=in.read ()!=-1) {
System.out.print ((char) ch);
}
In.close ();
Conn.close ();
}catch (Connectionnotfoundexception e) {
System.out.println ("Http could not to be opened");
}
}

public void Pauseapp () {}
public void Destroyapp (Boolean unconditional) {}
}


Compiling and running this midlet, the console outputs the statement "the message writed successfully." And then opens the database to view the table message and a record has been successfully written.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.