【问题标题】:How to run java Application from Midlet如何从 Midlet 运行 java 应用程序
【发布时间】:2014-01-30 13:05:31
【问题描述】:

第一,我是 Java 新手。我学到的一切都归功于 Netbeans 和 Internet。 我在用 NetBeans IDE 7.0.1 jdk1.7.0_25 Java_ME_platform_SDK_3.2

项目 1st- InsrtDB

package insrtdb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class InsrtDB {

private static Statement stmt = null;

    private static Connection con = null;
    public static  String mt="0:00:00";
    public static  String sms="Test InsrtDB";

     /**
     * @param args the command line arguments
     */

    public static void main(String[] args) throws ClassNotFoundException, SQLException, Exception {

     // TODO code application logic here
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     con = DriverManager.getConnection("jdbc:odbc:MADb");
     System.out.println("Connected!");
     stmt = con.createStatement();
     int rowsEffected = 0;

     // Example INSERT new record
     rowsEffected = updateData("INSERT INTO Reqst (myTime,SMS) VALUES (' "+mt+" ',' "+sms+" ')");
     System.out.print("Inserted a Row : ");
     con.close();
    }

     public static int updateData(String SQL) throws Exception {
     return stmt.executeUpdate(SQL);
     }
}

输出-InsrtDB(运行) 跑: 连接的! 插入一行:构建成功(总时间:27 秒)

Project 2nd-SndRecvSMS(MIDlet 应用)

private class SmsReceiverThread extends Thread {
public void run() {
try {
       //  Receive Message
       TextMessage txtMsg = (TextMessage) receiveCon.receive();

       // Get the receiving SMS phone number
       String senderTpNo = txtMsg.getAddress();

       // Get the receiving SMS message
       String senderMsg = txtMsg.getPayloadText();

      // Create a TextBox and direct the incomming message to that
      switchDisplayable(null, getTxtBox());

      smt = senderMsg.substring(10);
      msgSnt = senderMsg.substring(0, 10);
      stringItem.setText(smt);
      stringItem1.setText(msgSnt);
      } 
      catch (IOException ex) {
      }
  }

}

我第一次在 JavaMEPhone1 上运行了这个 midlet,然后在 JavaMEPhone2 上运行,我可以成功发送和接收短信。 现在,当在第二个应用程序中,我在资源中添加第一个应用程序作为添加项目 & Clean & Build 我得到 输出:- 构建成功(总时间:8 秒)

现在当我添加代码时以粗体突出显示

private class SmsReceiverThread extends Thread {

public void run() {
try {
     //  Receive Message
     TextMessage txtMsg = (TextMessage) receiveCon.receive();

     // Get the receiving SMS phone number
     String senderTpNo = txtMsg.getAddress();

     // Get the receiving SMS message
     String senderMsg = txtMsg.getPayloadText();

     // Create a TextBox and direct the incomming message to that
     switchDisplayable(null, getTxtBox());

     smt = senderMsg.substring(10);
     msgSnt = senderMsg.substring(0, 10);
     stringItem.setText(smt);
     stringItem1.setText(msgSnt);
     } catch (IOException ex) {
  }

   insrtdb.InsrtDB.mt=smt;
   System.out.println("New value of mt =  "  +insrtdb.InsrtDB.mt);
   insrtdb.InsrtDB.sms=msgSnt;
   System.out.println("New value 0f sms = "  +insrtdb.InsrtDB.sms);
   try {
         insrtdb.InsrtDB.class.getClass().newInstance();
         } catch (IllegalAccessException ex) {
         } catch (InstantiationException ex) {
       }

    }
}

清理并构建并运行。 输出:- 对于 JavaMEPhone1 跑步 123456790 012345678917:12:50 跑: 构建成功(总时间:8 分 14 秒) 输出:- 对于 JavaMEPhone2 收到消息 mt 的新值 = 17:12:50 新值 0f 短信 = 0123456789 运行无构建: 构建成功(总时间:2 分 34 秒)
但已连接! Inserted a Row 行丢失且未插入数据库行。

我错过了什么?

【问题讨论】:

    标签: java sql multithreading netbeans database-connection


    【解决方案1】:

    我一直在寻找基本、简单且可行的解决方案。但首先让我给你2个参考。 1st:-如何在 J2ME 中发送和接收 SMS by anujarosha at http://anujarosha.wordpress.com/2011/07/20/how-to-send-and-receive-sms-in-j2me/ 这是详尽而详细的博客,对此表示赞赏。 2nd:-从移动设备发送短信到计算机-java 移动套接字级别编程由 Sanjeewa Malalgoda 在 http://sanjeewamalalgoda.blogspot.in/2009/09/send-text-message-from-mobile-device-to.html 这是一个非常简单的博客,它的自我解释和同样的赞赏。 这 2 个博客启发了我与互联网上的每一个人分享我的解决方案。 实际上,这是我正在尝试构建的 Demo 的一部分,它有 4 个步骤。 首先:一个移动 midlet 应用程序向 2nd Mobile 发送 SMS。 第二:第二个移动 midlet 应用程序收到此 SMS(如果应用程序正在运行)。第二个移动设备通过 USB 数据线连接到笔记本电脑。 第三:收到短信后,应用程序向服务器套接字发送一个套接字,服务器套接字是一个运行在笔记本电脑上的java应用程序。 第四:收据上的服务器套接字拆分并插入到数据库表中,该表有两个字段,一个是 myTime 和第二个 SMS。 现在我要处理三次。第一个手机时间(发件人时间),第二个手机时间(接收手机时间)和第三个笔记本电脑时间,自然所有 3 个时间都不同步。对我来说,发件人时间是重要因素,因此我必须将其纳入 SMS 本身。同样,SMS 字符串长度可以从 0 到 160 不等;因此,同样的内容也包含在 SMS 本身中。因此字符串短信有3部分:字符串“短信长度”+字符串“短信”+字符串“时间”(hh:mm:ss)。

    我的解决方案是第一个 java 应用程序如下

       /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package aserver;
    import java.net.*; 
    import java.io.*; 
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    /**
     *
     * @author A S Patel
     */
    public class AServer {
    private static ServerSocket serverSocketin; 
    private static Statement stmt = null;
     private static Connection con = null;
    public static  String mt;
     public static  String sms;
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
         try  { 
    serverSocketin = new ServerSocket(8206); 
     DataRiciver dataRiciver=new DataRiciver(); 
     dataRiciver.start(); 
            }catch (IOException e) { 
            } 
          }
    
     static class DataRiciver extends Thread { 
            @Override
    public void run(){ 
    while (true){ 
    try { 
                        Socket incoming = serverSocketin.accept(); 
                        DataInputStream in = new DataInputStream(incoming.getInputStream()); 
                        String data=""; 
    data = in.readLine(); 
    System.out.println(data); 
     String lN=data.substring(0, 3);
                    int n=Integer.parseInt(lN);
    mt=data.substring(n+3);
    sms=data.substring(3, n+3);
    InsertDB();
    in.close(); 
    incoming.close(); 
    Thread.sleep(1000); 
                    }catch (Exception e) { 
                      } 
            }
    } 
        }
    
        public static void InsertDB() throws ClassNotFoundException, SQLException, Exception{
               Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
               con = DriverManager.getConnection("jdbc:odbc:MADb");
               System.out.println("Connected!");
               stmt = con.createStatement();
               int rowsEffected = 0;
               // Example INSERT new record
               rowsEffected = updateData("INSERT INTO Reqst (myTime,SMS) VALUES (' "+mt+" ',' "+sms+" ')");
                System.out.print("Inserted a Row : ");
                con.close();
            }
        public static int updateData(String SQL) throws Exception {
            return stmt.executeUpdate(SQL);
         } 
        }
    

    现在第二个是 Midlet 代码。就像在 Neatbeans 中一样,如果您使用可视 Midlet,流程图将会是!

    现在在新消息表单中选择发送并右键单击并选择转到源

    添加此代码

         } else if (command == send) {                                          
                    // write pre-action user code here
                     Calendar cal = Calendar.getInstance();
                     int hr = cal.get(Calendar.HOUR_OF_DAY);
                    int min = cal.get(Calendar.MINUTE);
                    int snd = cal.get(Calendar.SECOND);
                    String mt = String.valueOf(hr)+":"+String.valueOf(min)+":"+String.valueOf(snd);
                    phnNoStr = phnNoTexField.getString();
                    msgStr =  msgTexField.getString() ;
                    int n=msgStr.length();
                    String lN;
                   if(n<10){lN="00"+ String.valueOf(n);}else
                       if(n<100){lN="0"+ String.valueOf(n);}else
                       {lN= String.valueOf(n);}
                   mySMS=lN+msgStr+mt;
              // write post-action user code here
    new SMSThread(phnNoStr, mySMS).start();
     switchDisplayable(null, getSuccess());
                }                                           
    

    在代码的最后部分,即在 public void destroyApp(boolean unconditional) 之后

    你有常规内部类私有类 SmsReceiverThread 扩展线程

    在后面添加以下代码

                    String senderMsg = txtMsg.getPayloadText();
    
    
         // Create a TextBox and direct the incomming message to that
                    switchDisplayable(null, getTxtBox());
                    String lN=senderMsg.substring(0, 3);
                    int n=Integer.parseInt(lN);
                     smt = senderMsg.substring(n+3);
                    msgSnt = senderMsg.substring(3, n+3);
    stringItem.setText(smt);
    stringItem1.setText(msgSnt);
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException ex) {
                    }
               String data=senderMsg;
               sendData(data);
                } catch (IOException ex) {
                }
      switchDisplayable(null, getSplashScreen());
            }
      }    
     public void sendData(String data) { 
    try { 
    StreamConnection connection = (StreamConnection) Connector.open("socket://localhost:8206"); 
    PrintStream output = new PrintStream(connection.openOutputStream()); 
    data=data +"\n"; 
    output.write(data.getBytes()); 
    output.flush(); 
    output.close(); 
    connection.close();
     } catch (Exception e) { 
            } 
    

    您可以在 Neatbeans 中运行第一个代码而不是 2 个 Midlets 实例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-12
      • 1970-01-01
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      相关资源
      最近更新 更多