J2ME通過(guò)Servlet訪問(wèn)數(shù)據(jù)庫(kù)的幾大步驟
你知道J2ME訪問(wèn)數(shù)據(jù)庫(kù)的方式嗎,這里向大家描述一下J2ME通過(guò)Servlet訪問(wèn)數(shù)據(jù)庫(kù)步驟,希望對(duì)你的學(xué)習(xí)有所幫助。
J2ME通過(guò)Servlet訪問(wèn)數(shù)據(jù)庫(kù)步驟
1.配置Tomcat服務(wù)器
準(zhǔn)備Tomcat5.5服務(wù)器和JDK1.6,配置環(huán)境變量CLASSPATH、Path、JAVA_HOME、CATALINA_HOME。
2.配置數(shù)據(jù)源
配置數(shù)據(jù)源mydata,指向數(shù)據(jù)庫(kù)6D1。
數(shù)據(jù)庫(kù)6D1中數(shù)據(jù)表users,其結(jié)構(gòu)為(idchar(4),namevarchar(20)),users表中有數(shù)據(jù)記錄若干。
訪問(wèn)數(shù)據(jù)庫(kù)的用戶名為sa,密碼為空。
3.部署Servlet
在Tomcat5.5\webapps下創(chuàng)建目錄6D1,在6D1下創(chuàng)建目錄WEB-INF,在WEB-INF下創(chuàng)建classes目錄,將returnMsg.java文件保存在classes目錄下,其內(nèi)容如下所示:
- //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");
- //設(shè)置返回的類(lèi)型
- ServletOutputStreamout=res.getOutputStream();//得到輸出流
- DataOutputStreamdos=newDataOutputStream(out);
- dos.writeUTF("這是測(cè)試結(jié)果");
- dos.writeUTF("下面是數(shù)據(jù)信息");
- Connectionconn=null;
- ResultSetrs=null;
- Statementstmt=null;
- try
- {
- Stringdrname="sun.jdbc.odbc.JdbcOdbcDriver";
- //這些是連接數(shù)據(jù)庫(kù)的驅(qū)動(dòng)
- Class.forName(drname);
- Stringurl="jdbc:odbc:mydata";
- conn=DriverManager.getConnection(url,"sa","");
- stmt=conn.createStatement();
- rs=stmt.executeQuery("select*fromusers");
- //查詢數(shù)據(jù)庫(kù)表表名為user
- while(rs.next())
- {
- dos.writeUTF("\n用戶ID:"+rs.getString("id"));
- //得到字段ID和字段name內(nèi)容
- dos.writeUTF("\n用戶名:"+rs.getString("name"));
- }
- rs.close();
- stmt.close();
- conn.close();
- }
- catch(Exceptione)
- {}
- }
- }
在WEB-INF目錄下新建web.xml文件,其內(nèi)容如下所示:
- <?xmlversionxmlversion="1.0"encoding="ISO-8859-1"?>
- <web-appxmlnsweb-appxmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2eeh
- ttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
- version="2.4">
- <servlet>
- <servlet-name>returnMsg</servlet-name>
- <servlet-class>returnMsg</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>returnMsg</servlet-name>
- <url-pattern>/returnMsg</url-pattern>
- </servlet-mapping>
- </web-app>
編譯該Servlet。#p#
續(xù)前J2ME通過(guò)Servlet訪問(wèn)數(shù)據(jù)庫(kù):
4.編寫(xiě)MIDlet應(yīng)用
打開(kāi)WirelessToolKit2.5.2,新建項(xiàng)目,項(xiàng)目名字為6D1,MIDlet類(lèi)名為NetMain。
進(jìn)入j2mewtk\2.5.2\apps\6D1\src目錄,在該目錄下創(chuàng)建兩個(gè)java文件,一個(gè)為NetMain.java,一個(gè)為SendMsg.java。
NetMain.java文件的內(nèi)容如下所示:
- //NetMain.java
- importjavax.microedition.midlet.*;
- importjavax.microedition.lcdui.*;
- //繼承MIDlet實(shí)現(xiàn)CommandListener接口
- publicclassNetMainextendsMIDletimplementsCommandListener
- {
- privateDisplaydis;
- SendMsgsm;
- privateCommandsd=newCommand("連接",Command.OK,1);
- //發(fā)送數(shù)據(jù)的按鈕
- publicNetMain()
- {dis=Display.getDisplay(this);//得到顯示對(duì)象
- }
- publicvoidstartApp()
- {
- Formf=newForm("聯(lián)網(wǎng)測(cè)試");//顯示在屏幕的Form對(duì)象
- f.append("發(fā)送數(shù)據(jù)");
- f.addCommand(sd);
- f.setCommandListener(this);//設(shè)置按鈕監(jiān)聽(tīng)
- dis.setCurrent(f);
- }
- publicvoidpauseApp()
- {}
- publicvoiddestroyApp(booleanun)
- {}
- publicvoidexit()
- {destroyApp(false);
- notifyDestroyed();
- }
- publicvoidcommandAction(Commandc,Displayabled)
- {if(c==sd)
- {sm=newSendMsg(this);//調(diào)用sendMsg類(lèi),將本類(lèi)作為參數(shù)傳入
- dis.setCurrent(sm);//顯示sendMsg類(lèi)
- }}}
【編輯推薦】
- J2ME手機(jī)RSS閱讀器通過(guò)NetBeans 4.0也可創(chuàng)建
- 深入探究cookie技術(shù)在J2ME平臺(tái)的應(yīng)用與實(shí)現(xiàn)
- 探究J2ME中cookie庫(kù)的管理
- MotorolaJ2ME開(kāi)發(fā)時(shí)需要注意的幾個(gè)細(xì)節(jié)
- Java2平臺(tái)J2SE、J2EE、J2ME三大版本的區(qū)別