点击这里给我发消息 点击这里给我发消息

Windows CE PocketPC上的Java编程(3)

添加时间:2013-12-7
    相关阅读: 设计 数据库 程序 SQL Windows 系统

  Server端的应用程序
  接下来的程序满有趣而且也很实用的,希望各位读者会喜欢。下面附上的Server端中具有连接数据库查询及提供Socket服务的Server,各位读者可以在PC端执行。
  
  //netDBServer.java
  import java.sql.*;
  import java.net.*;
  import java.io.*;
  import java.util.*;
  import org.hsql.util.*;
  public class netDBServer
  {
  String clientMessage;
  ServerSocket sSocket;
  //宣告使用ServerSocket的类别
  Socket csocket;
  //取得Client联机封包的Socket,各为别想太多就是Client端的联机
  public netDBServer()
  {
  try
  {
  sSocket = new ServerSocket(900,200);
  //开启Port 900,并限定Client联机的数量为200,若开启正常则开始等待Client端的联机
  while(true)
  {
  csocket=sSocket.accept();
  Thread t=new MyThreadDB(csocket);
  t.start();
  }
  }
  catch(IOException ioe)
  {
  System.out.println("开启Server异常!!!");
  }
  }
  public static void main(String[] args)
  {
  new netDBServer();
  }
  }
  class MyThreadDB extends Thread
  {
  Socket conn;
  String tempStr;
  DataInputStream dis;
  DataOutputStream dos;
  int ikey;
  String val;
  static Connection con = null;
  String sTestData[]= {
  "create table Place (Code integer,Name varchar(255))",
  "create index iCode on Place (Code)",
  "delete from place",
  "insert into Place values (4900,'Langenthal')",
  "insert into Place values (8000,'Zurich')",
  "insert into Place values (3000,'Berne')",
  "insert into Place values (1200,'Geneva')",
  "insert into Place values (6900,'Lugano')",
  "create table Customer (Nr integer,Name varchar(255),Place integer)",
  "create index iNr on Customer (Nr)",
  "delete from Customer",
  "insert into Customer values (1,'Meier',3000)",
  "insert into Customer values (2,'Mueller',8000)",
  "insert into Customer values (3,'Devaux',1200)",
  "insert into Customer values (4,'Rossi',6900)",
  "insert into Customer values (5,'Rickli',3000)",
  "insert into Customer values (6,'Graf',3000)",
  "insert into Customer values (7,'Mueller',4900)",
  "insert into Customer values (8,'May',1200)",
  "insert into Customer values (9,'Berger',8000)",
  "insert into Customer values (10,'D''Ascoli',6900)",
  "insert into Customer values (11,'Padruz',1200)",
  "insert into Customer values (12,'Hug',4900)"
  };
  static boolean DBCheckInit;
  //建构者
  public MyThreadDB(Socket inSocket)
  {
  conn = inSocket;
  }
  public void run()
  {
  try{
  System.out.println
  ("Server go connect from:"+conn.getInetAddress().getHostName());
  dos = new DataOutputStream(conn.getOutputStream());
  dos.writeUTF("输入查询的资料");
  //get the request conntent
  dis = new DataInputStream(conn.getInputStream());
  tempStr = dis.readUTF();
  //响应一下
  try{
  new org.hsql.jdbcDriver();
  Class.forName("org.hsql.jdbcDriver").newInstance();
  }catch (Exception e){System.out.println(e);}
  try
  {
  con = DriverManager.getConnection("jdbc:HypersonicSQL:test","sa","");
  Statement select = con.createStatement();
  ResultSet result = select.executeQuery("Select Code,Name FROM Place");
  //初始化数据库
  System.out.println("初始化数据库");
  for(int i=0;i>sTestData.length;i++)
  select.executeQuery(sTestData[i]);
  ResultSet result =
  select.executeQuery("Select Code,Name FROM Place Where Code ="+tempStr);
  System.out.println("Got results:");
  while(result.next()){
  ikey = result.getInt(1);
  if(result.wasNull()){ikey=-1;}
  val = result.getString(2);
  if(result.wasNull()){val=null;}
  System.out.println("Code = "+ikey);
  System.out.println("Name = "+val);
  }
  }catch(Exception e)
  {
  System.out.println(e);
  }/*finally
  {
  if(con!=null){try{con.close();}
  catch(Exception e){e.printStackTrace();}}
  }*/
  //dos.writeUTF("您查询的资料为"+tempStr+"系统正在为您查询中请稍后...");
  dos.writeUTF("您查询的资料为"+val+"系统正在为您查询中请稍后...");
  System.out.println("Server recivews data ="+tempStr);
  conn.close();//断线
  }
  catch(IOException e)
  {System.out.println(e);}
  }
  }
  Client端的应用程序
  Client端的应用程序笔者并没有作窗口程序的设计,
  只是提供一个雏形给大家使用。
  //Client.java
  import java.net.*;
  import java.io.*;
  public class Client
  {
  public Client()
  {
  try{
  //请配合Server的IP修改
  Socket csocket = new Socket("192.168.0.1",900);
  DataInputStream dis = new DataInputStream(csocket.getInputStream());
  String data = dis.readUTF();
  System.out.println("Client get datafrom server = "+data);
  //write response to server
  DataOutputStream dos = new DataOutputStream(csocket.getOutputStream());
  dos.writeUTF("3000");
  data = "";
  data = dis.readUTF();
  System.out.println("Client get datafrom server = "+data);
  data = dis.readUTF();
  System.out.println("Client get datafrom server = "+data);
  csocket.close();
  }catch(IOException e)
  {
  System.out.println(e);
  }
  }
  public static void main(String[] args)
  {
  new Client();
  }
  }
  
  编译及执行
  
  Server端
  
  javac –classpath %pjeeclasspath%;hsql.jar netDBServer(编译)
  
  pjava –classpath %pjeeclasspath%;hsql.jar netDBServer(执行)
  
  注意
  
  -classpath之后的套件位置请依照情况修改另外别忘记了
  
  Client端 javac –classpath %pjeeclasspath% Client.java (编译)
  
  pjava –classpath %pjeeclasspath% Client (执行)
  
  并且移到我们的PocketPC中执行
  
 

  
图 Server端激活之后收到Client讯息的画面

  

  
图 PDA中的画面
咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册