2022年手机访问SQL数据库整理 .pdf
J2me c/s结构,访问数据库第一步:配制好j2me客户端。1、装好 JDK。2、装好 Wireless T oolkit 2.5.2手机模拟软件。第二步:配制好serverlet服务器端。1、装好 Tomcat,并配制好。第三步:写手机端代码NetMain.java 类,手机程序的主类;import javax.microedition.midlet.MIDlet; import javax.microedition.lcdui.*; import javax.microedition.midlet.MIDletStateChangeException; publicclass 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 stub protectedvoid destroyApp(boolean arg0) throwsMIDletStateChangeException / TODO Auto-generated method stub protectedvoid pauseApp() / TODO Auto-generated method stub 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 7 页 - - - - - - - - - protectedvoid 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 stub publicvoid commandAction(Command c,Displayable d) if(c = sd ) sm = new sendMsg(this); /调用 sendMsg类,将本类作为参数传入dis .setCurrent(sm);/显示 sendMsg类 elseif(c= ex ) try destroyApp(false); catch (MIDletStateChangeException e) / TODO Auto-generated catch blocke.printStackTrace(); notifyDestroyed(); publicvoid 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 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 7 页 - - - - - - - - - 发送请求得到数据类sendMsg.java import javax.microedition.lcdui.*; import javax.microedition.io.*; import java.io.*; publicclass sendMsg extends Form implements CommandListener,Runnable String url = http:/: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();/启动线程 publicvoid 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 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 7 页 - - - - - - - - - HttpConnection hconn = null; /定义 HTTP连接的对象 DataInputStream dis = null; /定义数据输入流 StringBuffer msg = new StringBuffer() ; String st= null; ;/创建一个 StringBuffer存放收到的字符串/ String s=;try hconn = (HttpConnection)Connector.open ( http:/: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并返回 publicvoid show() publicvoid commandAction(Command c,Displayable d) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 7 页 - - - - - - - - - if(c = exit) nm.exit();/退出程序 下面我们来设计服务器端的部分。主要就是写一个servlet 程序。下面是程序代码import java.sql.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class returnMsg extends HttpServlet public 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(下面是数据信息 ); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 7 页 - - - - - - - - - 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 block e.printStackTrace(); catch (SQLException e) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 7 页 - - - - - - - - - e.printStackTrace(); dos.writeUTF(end); 首先把写好的returnMsg.java程序要进行编译,生成.class文件。我们把生成好的class文件放在 Tomcat 目录下的webapps/root/web-inf/classes/ 里面,注意路径一定不要错。然后还要编辑web-inf目录下面的web.xml 文件 . 再里面加入这些信息:- returnMsg returnMsg returnMsg /returnMsg - 注 :每次更改完 class都要更新 TOMCA T 服务。名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 7 页 - - - - - - - - -