动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > J2EE/J2ME教程
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ J2EE/J2ME教程 ]的信息

本月文章推荐
.Spring入门.
.J2ME网络程序中移动资费页面的处.
.简单图形验证码的识别.
.菜鸟学jsp(一).
.基于MIDP1.0实现通信录.
.浅析J2EE应用中的时间值字段的数.
.演示Session Listener的使用方法.
.你想把XML转成PDF?喔,使用FOP.
.BlueTooth探索系列(二)---发现.
.解决日期选择问题,一劳永逸(使.
.J2ME再现华容道1.
.扫描屏幕和颜色渐变的效果.
.J2ME中文问题的解决方案.
.TOMCAT3.1的安装与配置.
.如何用IIS来实现OTA下载.
.使用Netbeans IDE集成Motorola J.
.MIDP终端模拟之二:高级终端模拟.
.J2EE中用EntityBean和JDO的优缺点.
.jboss4+ejb3下使用JAAS.
.JBOSSAOP学习笔记-依赖注入.

联网并进行RMS永久存储的演示代码

发表日期:2007-12-23 |


Stock.Java
public class Stock {
   private static String name, time, price;

   public static void parse(String data) {
     int index = data.indexOf('"');   
     name = data.substring(++index, (index = data.indexOf('"', index)));
     index +=3;
     time = data.substring(index, (index = data.indexOf('-', index))-1);
     index +=5;
     price = data.substring(index, (index = data.indexOf('<', index)));
   }

   public static String getName(String record) {
     parse(record);
     return(name);
   }
 
   public static String getPrice(String record) {
     parse(record);
     return(price);
   }

}

StockDB.java
import javax.microedition.rms.*;
import java.util.Enumeration;
import java.util.Vector;
import java.io.*;

public class StockDB {

   RecordStore recordStore = null;
      
   public StockDB() {}

   public StockDB(String fileName) {
      try {
        recordStore = RecordStore.openRecordStore(fileName, true);
      } catch(RecordStoreException rse) {
        rse.printStackTrace();
      }
   }

   public void close() throws RecordStoreNotOpenException,
                               RecordStoreException {
        if (recordStore.getNumRecords() == 0) {
            String fileName = recordStore.getName();
            recordStore.closeRecordStore();
            recordStore.deleteRecordStore(fileName);
        } else {
            recordStore.closeRecordStore();
        }
    }


   public synchronized void addNewStock(String record) {
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 DataOutputStream outputStream = new DataOutputStream(baos);
 try {
     outputStream.writeUTF(record);
 }
 catch (IOException ioe) {
     System.out.println(ioe);
     ioe.printStackTrace();
 }
 byte[] b = baos.toByteArray();
 try {
     recordStore.addRecord(b, 0, b.length);
 }
 catch (RecordStoreException rse) {
     System.out.println(rse);
     rse.printStackTrace();
 }
    }


    public synchronized RecordEnumeration enumerate() throws RecordStoreNotOpenException {
       return recordStore.enumerateRecords(null, null, false);
    }


}

QuotesMIDlet.java

import javax.microedition.rms.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.*;
import java.util.Vector;

public class QuotesMIDlet extends MIDlet implements CommandListener {
    Display display = null;
    List menu = null; // main menu
    List choose = null;
    TextBox input = null;
    Ticker ticker = new Ticker("Database Application");
    String quoteServer = "http://quote.yahoo.com/d/quotes.csv?s=";
    String quoteFormat = "&f=slc1wop";

    static final Command backCommand = new Command("Back", Command.BACK, 0);
    static final Command mainMenUCommand = new Command("Main", Command.SCREEN, 1);
    static final Command saveCommand = new Command("Save", Command.OK, 2);
    static final Command exitCommand = new Command("Exit", Command.STOP, 3);
    String currentMenu = null;

    // Stock data
    String name, date, price;

    StockDB db = null;

    public QuotesMIDlet() {
    }

    public void startApp() throws MIDletStateChangeException {
      display = Display.getDisplay(this);
      // open a db stock file
      try {
        db = new StockDB("stocks");
      } catch(Exception e) {}
      menu = new List("Stocks Database", Choice.IMPLICIT);
      menu.append("List Stocks", null);
      menu.append("Add A New Stock", null);
      menu.addCommand(exitCommand);
      menu.setCommandListener(this);
      menu.setTicker(ticker);

      mainMenu();
    }

    public void pauseApp() {
      display = null;
      choose = null;
      menu = null;
      ticker = null;
      
      try {
        db.close();
        db = null;
      } catch(Exception e) {}
    }


    public void destroyApp(boolean unconditional) {
      try {
        db.close();
      } catch(Exception e) {}
      notifyDestroyed();
    }

    void mainMenu() {
      display.setCurrent(menu);
      currentMenu = "Main";
    }

    public String tickerString() {
       StringBuffer ticks = null;
       try {
          RecordEnumeration enum = db.enumerate();
          ticks = new StringBuffer();
          while(enum.hasNextElement()) {
            String stock1 = new String(enum.nextRecord());
            ticks.append(Stock.getName(stock1));
            ticks.append(" @ ");
            ticks.append(Stock.getPrice(stock1));
            ticks.append("    ");
          }
       } catch(Exception ex) {}
       return (ticks.toString());
    }

    public void addStock() {
      input = new TextBox("Enter a Stock Name:", "", 5, TextField.ANY);
      input.setTicker(ticker);
      input.addCommand(saveCommand);
      input.addCommand(backCommand);
      input.setCommandListener(this);
      input.setString("");
      display.setCurrent(input);
      currentMenu = "Add";
    }

    public String getQuote(String input) throws IOException, NumberFormatException {
      String url = quoteServer + input + quoteFormat;
      StreamConnection c = (StreamConnection)Connector.open(url, Connector.READ_WRITE);
      InputStream is = c.openInputStream();
      StringBuffer sb = new StringBuffer();
      int ch;
      while((ch = is.read()) != -1) {
        sb.append((char)ch);
      }
      return(sb.toString());
    }
  
    public void listStocks() {
        choose = new List("Choose Stocks", Choice.MULTIPLE);
        choose.setTicker(new Ticker(tickerString()));
        choose.addCommand(backCommand);
        choose.setCommandListener(this);
      try {
         RecordEnumeration re = db.enumerate();
         while(re.hasNextElement()) {
           String theStock = new String(re.nextRecord());
           choose.append(Stock.getName(theStock)+" @ " + Stock.getPrice(theStock), null);
         }
      } catch(Exception ex) {}
      display.setCurrent(choose);
      currentMenu = "List";
   }
 
 
   public void commandAction(Command c, Displayable d) {
      String label = c.getLabel();
      if (label.equals("Exit")) {
         destroyApp(true);
      } else if (label.equals("Save")) {
          if(currentMenu.equals("Add")) {
              // add it to database
            try {
              String userInput = input.getString();
              String pr = getQuote(userInput);
              db.addNewStock(pr);
              ticker.setString(tickerString());
             
            } catch(IOException e) {
            } catch(NumberFormatException se) {
            }
            mainMenu();
          }


      } else if (label.equals("Back")) {
          if(currentMenu.equals("List")) {
            // go back to menu
            mainMenu();
          } else if(currentMenu.equals("Add")) {
            // go back to menu
            mainMenu();
          }

      } else {
         List down = (List)display.getCurrent();
         switch(down.getSelectedIndex()) {
           case 0: listStocks();break;
           case 1: addStock();break;
         }
           
      }
  }
}

(出处:http://www.knowsky.com)


上一篇:Post和Get 的问题解决 人气:818
下一篇:MIDP 1.0 HttpConnection类的robust封装 人气:750
浏览全部J2EE/J2ME的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-11-21 AutoIndex v2.2.4 多国语言版
2008-11-21 ASBLOG v2.5 bulid 081118
2008-11-21 phpwebsite v1.60
2008-11-21 DreamArticle 文章管理系统 v3.0
2008-11-21 DreamArticle 文章管理系统 v3.0
2008-11-21 Piwik ( PHP统计系统,可以和GOOG
2008-11-21 CMS001 v2.2 Beta
2008-11-21 magento开源电子商务平台 v1.1.7
2008-11-21 开良马克思影视下载插件 v1.1
2008-11-21 傲游(Maxthon) 2.1.5 正式版
2008-11-21 Skype v3.8.0.188 Final
2008-11-21 AirPlay OpenAlpha 2008.11.20
2008-11-21 屏幕文字抓取工具 DWMouse1.3.510
2008-11-21 Vista一键还原(Vista Ghost)1.
2008-11-21 SP Photo Fix照片修改1.2破解版
2008-11-21 QQ腾讯聊天工具 v1.2正式版
2008-11-21 FlightTrack航班信息v1.0破解版
2008-11-21 RealPiano仿真钢琴1.0破解版
  发表评论
姓 名: 验证码:
内 容:
站长工具:网站收录查询 | Google PR查询 | ALEXA排名查询 | CSS在线编辑器 | 广告代码 | js/vbs加密 | md5加密 | 进制转换 | UTF-8 转换工具 | Html转换js | Html转换asp | Html转换php | Html转换perl
实用工具:汉字翻译拼音 | 拼音字典 | 符号对照表 | 个税计算 | 实时汇率查询换算 | 经典小工具 | 汉字简繁转换 | 普通单位换算 | 公制单位换算 | 生辰老黄历 | 国内电话区号 | 国家代码与域名缩写 | 文字加密解密 | 健康查询 | 万年历 | 汉字横竖排版 | 手机号码查询 | 计算器 | ip搜索
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2008 www.knowsky.com All rights reserved | 网络实名:动态网站制作指南 | 沪ICP备05001343号
ホームページ制作 不動産検索システム 求人情報
防水工事·改修工事 フットサル大会 探偵
SEO対策 中国語教室 ホームページ作成