myconnection.java

来自「这是一个可以在手机客户端运行的选课系统 这个是服务器端」· Java 代码 · 共 179 行

JAVA
179
字号
/*
 * 创建日期 2005-3-11
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.*;

/**
 * @author huihui
 * 
 * TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public class MyConnection implements Runnable {
	
	int result=-10;

	boolean sendflag = false;

	boolean getflag = false;

	String request = null; //请求的URL

	int next_wrong=0;
	int next_right=0;

	String content; //提示信息:“密码验证”、“选课”、“修改密码”

	MainMIDlet mainmidlet = null;
	Thread thread = new Thread(this);
	
	public MyConnection(MainMIDlet mainmidlet, String request, String content,int next_right,int next_wrong) {
		this.mainmidlet = mainmidlet;
		this.content = content;
		this.request = request;
		this.next_wrong = next_wrong;
		this.next_right=next_right;
	}
	public void setSend() {
		sendflag = true; //有信息待发送
	}
	public void setGet() {
		getflag = true; //有信息待接收
	}
	public void ConnectToServer() throws IOException {
		//byte[] buffer = null;
		try {
			//loading
			this.mainmidlet.imagescreen.show(3,"连接服务器……",next_wrong);		//3:load			
			HttpConnection connection = (HttpConnection) Connector.open(request, Connector.READ_WRITE, true);
			if (sendflag) {
				OutputStream os = null;				
				os = connection.openOutputStream();				
				DataOutputStream dos = new DataOutputStream(os);
				dos.writeInt(this.mainmidlet.choosed); 					//选了几门课
				for (int i = 0; i < this.mainmidlet.choosed; i++) {
					dos.writeInt(this.mainmidlet.courseChoosedid[i]);
				}
				dos.flush();
				dos.close();
				os.close();
			}			
			InputStream is = null;
			is = connection.openInputStream(); //打开输入流
			DataInputStream dis = new DataInputStream(is);
			result = dis.readInt();		// -1代表用户名不存在 -2代表密码错误 -3代表与数据库连接错误	(三个代表)
			System.out.println(result);
			if(result>1024){	//意外情况
				result=0;
			}		
			/*
			if(result==-5){
				int fullNum=dis.readInt();
				System.out.println(fullNum);
				for(int i=0;i<fullNum;i++)
					System.out.println(dis.readInt());
			}*/
			if (getflag) {
				//System.out.println("读取课程!");
				//带缓冲读取
				/*
				 * int actual = 0; int bytesread = 0; buffer = new byte[len];
				 *  while ((bytesread != len) && (actual != -1)) { actual =
				 * is.read(buffer, bytesread, len - bytesread); bytesread +=
				 * actual; //parse.parse(dis,dh); }
				 */
				if (result >= 0) { //>=0正常//					
					if (next_right == 2 ){ 		//选课列表						
						this.mainmidlet.courseNum = result;
						result = 1;
						this.mainmidlet.allcourseid = new int[this.mainmidlet.courseNum];
						this.mainmidlet.allcourse = new String[this.mainmidlet.courseNum];				
						for (int i = 0; i < this.mainmidlet.courseNum; i++) {	
							this.mainmidlet.allcourseid[i] = dis.readInt();
							this.mainmidlet.allcourse[i] = dis.readUTF();
							//System.out.println(this.mainmidlet.allcourse[i]);
						}						
					} else if (next_right==3) { //查询
						//System.out.println("result="+result);
						this.mainmidlet.choosed = result;
						this.mainmidlet.courseChoosed=new String[this.mainmidlet.choosed];	//申请字符串数组空间
						result = 1;
						for (int i = 0; i < this.mainmidlet.choosed; i++) {
							this.mainmidlet.courseChoosedid[i] = dis.readInt();
							this.mainmidlet.courseChoosed[i]  = new String(dis.readUTF());
						}
					}
				}				
			}
			
			dis.close();
			is.close();
			connection.close();
			//根据servelet返回信息判断。 // -1代表用户名不存在 -2代表密码错误 -3代表与数据库连接错误 -4代表更新数据库错误
			if (result == 1) {
				if (next_right==2) {
					this.mainmidlet.ShowChoose(); //直接进入选课界面					
				} else if (next_right == 3) {					
					this.mainmidlet.ShowQuery(); //直接进入查询结果界面					
				} else {
					//successful
					this.mainmidlet.imagescreen.show(1,content + "成功",next_right);
					this.mainmidlet.display.setCurrent(this.mainmidlet.imagescreen);
				}
			} else {
				switch (result) { //判断错误代码
				case -5:
					content = "课程人数已满";
					break;
				case -4:
					content = "更新数据库发生错误";
					break;
				case -3:
					content = "与数据库连接错误";
					break;
				case -2:
					content = "密码错误";
					break;
				case -1:
					content = "用户名不存在";
					break;
				default:
					//System.out.println(content);
					break;
				}
				//密码错误需返回登录界面
				this.mainmidlet.imagescreen.show(2,"错误: " + content, next_wrong);
				this.mainmidlet.display.setCurrent(this.mainmidlet.imagescreen);
			}
		} catch (IOException e) {
			e.printStackTrace();			
			//connection failed
			this.mainmidlet.imagescreen.show(2,"连接服务器失败", next_wrong);
			//应返回登录框。
		}
	}
	public void start() {
		try {
			thread.start();
		} catch (Exception error) {
			error.printStackTrace();
		}
	}

	public void run() {
		try {
			ConnectToServer();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?