Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the steps for J2ME to access the database through Servlet?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how J2ME accesses the database through Servlet. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Do you know how J2ME accesses the database? here is a description of how J2ME accesses the database through Servlet. I hope it will be helpful to your study.

Steps for J2ME to access the database through Servlet

1. Configure the Tomcat server

Prepare the Tomcat5.5 server and JDK1.6 and configure the environment variables CLASSPATH, Path, JAVA_HOME, CATALINA_HOME.

two。 Configure the data source

Configure the data source mydata to point to the database 6D1.

The structure of the data table users in the database 6D1 is (idchar (4), namevarchar (20)). There are several data records in the users table.

The user name to access the database is sa and the password is empty.

3. Deploy Servlet

Create the directory 6D1 under Tomcat5.5\ webapps, create the directory WEB-INF under 6D1, create the classes directory under WEB-INF, and save the returnMsg.java file under the classes directory as follows:

/ / returnMsg.java importjava.sql.*; importjavax.sql.*; importjava.io.*; importjavax.servlet.*; importjavax.servlet.http.*; publicclassreturnMsgextendsHttpServlet {publicvoiddoGet (HttpServletRequestreq,HttpServletResponseres) throwsServletException,IOException {res.setContentType ("text/html;charset=gb2312"); / / set the returned type ServletOutputStreamout=res.getOutputStream (); / / get the output stream DataOutputStreamdos=newDataOutputStream (out); dos.writeUTF ("this is the test result") Dos.writeUTF ("data information below"); Connectionconn=null; ResultSetrs=null; Statementstmt=null; try {Stringdrname= "sun.jdbc.odbc.JdbcOdbcDriver"; / / these are the drivers that connect to the database Class.forName (drname); Stringurl= "jdbc:odbc:mydata"; conn=DriverManager.getConnection (url, "sa", "); stmt=conn.createStatement (); rs=stmt.executeQuery (" select*fromusers ") / / query database table name user while (rs.next ()) {dos.writeUTF ("\ nuser ID:" + rs.getString ("id")); / / get field ID and field name content dos.writeUTF ("\ nusername:" + rs.getString ("name"));} rs.close (); stmt.close (); conn.close ();} catch (Exceptione) {}}

Create a new web.xml file in the WEB-INF directory with the following contents:

ReturnMsg / returnMsg

Compile the Servlet.

Before continuing, J2ME accesses the database through Servlet:

4. Write MIDlet applications

Open WirelessToolKit2.5.2 and create a new project with the name of 6D1 MIDlet class called NetMain.

Go to the j2mewtk\ 2.5.2\ apps\ 6D1\ src directory and create two java files, one for NetMain.java and one for SendMsg.java.

The contents of the NetMain.java file are as follows:

/ / NetMain.java importjavax.microedition.midlet.*; importjavax.microedition.lcdui.*; / / inherit MIDlet to implement CommandListener interface publicclassNetMainextendsMIDletimplementsCommandListener {privateDisplaydis; SendMsgsm; privateCommandsd=newCommand ("connection", Command.OK,1); / / Button to send data publicNetMain () {dis=Display.getDisplay (this); / / get display object} publicvoidstartApp () {Formf=newForm ("networking Test") / / Form object f.append ("send data") displayed on the screen; f.addCommand (sd); f.setCommandListener (this); / / set button to listen on dis.setCurrent (f);} publicvoidpauseApp () {} publicvoiddestroyApp (booleanun) {} publicvoidexit () {destroyApp (false); notifyDestroyed ();} publicvoidcommandAction (Commandc,Displayabled) {if (c==sd) {sm=newSendMsg (this) / / call the sendMsg class and pass this class as a parameter to dis.setCurrent (sm); / / display the sendMsg class} the above is how J2ME accesses the database through Servlet. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report