手机访问SQL数据库.doc_第1页
手机访问SQL数据库.doc_第2页
手机访问SQL数据库.doc_第3页
手机访问SQL数据库.doc_第4页
手机访问SQL数据库.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

J2me c/s结构,访问数据库第一步:配制好j2me客户端。1、 装好JDK。2、 装好Wireless Toolkit 2.5.2手机模拟软件。第二步:配制好serverlet服务器端。1、 装好Tomcat,并配制好。第三步:写手机端代码 NetMain.java类,手机程序的主类;import javax.microedition.midlet.MIDlet;import javax.microedition.lcdui.*;import javax.microedition.midlet.MIDletStateChangeException;public class NetMain extends MIDlet implements CommandListener private Display dis; sendMsg sm; private Command sd = new Command(连接,Command.OK,1);/发送数据的按钮 private Command ex = new Command(退出,Command.EXIT,1);/public NetMain() dis = Display.getDisplay(this);/ TODO Auto-generated constructor stubprotected void destroyApp(boolean arg0) throws MIDletStateChangeException / TODO Auto-generated method stub protected void pauseApp() / TODO Auto-generated method stubprotected void startApp() throws MIDletStateChangeException Form f = new Form(联网测试);/显示在屏幕的Form对象 f.append(发送数据); f.addCommand(sd); f.addCommand(ex); f.setCommandListener(this);/设置按钮监听 dis.setCurrent(f);/ TODO Auto-generated method stubpublic void commandAction(Command c,Displayable d) if(c = sd) sm = new sendMsg(this);/调用sendMsg类,将本类作为参数传入 dis.setCurrent(sm);/显示sendMsg类 else if(c=ex) try destroyApp(false); catch (MIDletStateChangeException e) / TODO Auto-generated catch blocke.printStackTrace(); notifyDestroyed(); public void exit()Form f = new Form(联网测试);/显示在屏幕的Form对象 f.append(发送数据); f.addCommand(sd); f.addCommand(ex); f.setCommandListener(this);/设置按钮监听dis.setCurrent(f);/ TODO Auto-generated method stub 发送请求得到数据类sendMsg.javaimport javax.microedition.lcdui.*;import javax.microedition.io.*;import java.io.*;public class sendMsg extends Form implements CommandListener,Runnable String url = :8000/test/returnMsg;/ 定义联网用的URL String returnMsg=; /获得返回字符串 Command exit=new Command(返回,Command.OK,1);/退出程序的按钮 Thread t;/定义联网用线程 NetMain nm;/NetMain的实例 public sendMsg(NetMain nm)/构造函数,将NetMain的实例传入 super(返回结果);/定义Form的标题 this.nm = nm; t = new Thread(this); t.start();/启动线程 public void run() try returnMsg = sendHttpGet(url);/将url传到sendHttpGet方法里做参数,并返回服务器结果的字符串赋值给returnMsg /System.out.println(returnMsg); append(returnMsg);/显示出返回结果 addCommand(exit); setCommandListener(this); catch(Exception e) System.out.println(e); public String sendHttpGet(String url) throws IOException HttpConnection hconn = null; /定义HTTP连接的对象 DataInputStream dis = null; /定义数据输入流 StringBuffer msg = new StringBuffer() ; String st= null; ;/创建一个StringBuffer存放收到的字符串 / String s=; try hconn = (HttpConnection)Connector.open(:8000/test/returnMsg);/得到连接对象 dis = new DataInputStream(hconn.openInputStream(); st=(dis.readUTF().toString(); /用得到的对象的数据流做参数 再作为参数构成数据输入流 while(!st.equals(end) /判断当数据流不为空的时候 msg.append(st); st=(dis.readUTF().toString(); /System.out.println(msg.toString(); /用readUTF()方法构造字符串 放在StringBuffer里 catch(Exception e) System.out.println(e); finally if(hconn != null)/关闭连接 hconn.close(); if(dis != null) dis.close(); /System.out.println(msg.toString(); return msg.toString();/将得到的StringBuffer 通过toString()方法,生成新的String并返回 public void show() public void commandAction(Command c,Displayable d) if(c = exit) nm.exit();/退出程序 下面我们来设计服务器端的部分。主要就是写一个servlet程序。下面是程序代码import java.sql.*;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class returnMsg extends HttpServletpublic void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException res.setContentType(text/html; charset=gb2312);/设置返回的类型 ServletOutputStream out = res.getOutputStream();/得到输出流 DataOutputStream dos = new DataOutputStream(out); dos.writeUTF(这是测试结果); dos.flush(); dos.writeUTF(下面是数据信息); dos.flush(); try Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); Connection con = DriverManager.getConnection(jdbc:odbc:ksoa,sa,*); Statement sql=con.createStatement(); ResultSet rs=sql.executeQuery(select fdbs ,sum(sshje) as sshje from retmast where rq=2011-11-15 group by fdbs); while(rs.next() dos.writeUTF(n); dos.writeUTF(分店:+rs.getString(fdbs);/得到字段ID 和字段name 内容 dos.writeUTF( 总金额:+rs.getString(sshje); catch (ClassNotFoundException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (SQLException e) e.printStackTrace(); dos.writeUTF(end);首

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论