高人给解释一下,明天结账,不够再开贴。
package com.landsea.myservlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import sun.jdbc.rowset.*;
import com.landsea.sendclass.SendClass;
import com.landsea.cachedrowsetwrapper.*;
import java.net.*;
import com.landsea.myexception.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class MyServlet extends HttpServlet {
//private static final String CONTENT_TYPE = "text/html; charset=GBK";
private static final String CONTENT_TYPE = "application/octet-stream";
//Initialize global variables
/******************初始化数据库的驱动程序********************/
static
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("success for name");
}
catch(ClassNotFoundException cnfe)
{
System.err.println(cnfe.getMessage());
cnfe.printStackTrace();
}
}
/******************初始化数据库的驱动程序********************/
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
///**********************************
/************************************************************/
System.out.println("成功调用Servlet");
String inLawOrNot = null;
inLawOrNot = request.getParameter("DataQuery");
System.out.println("调用参数是:" + inLawOrNot);
if(inLawOrNot == null) return;
try
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("success for name");
}
catch(ClassNotFoundException cnfe)
{
System.err.println(cnfe.getMessage());
cnfe.printStackTrace();
}
Connection dbcon = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://192.168.102.81:1433;DatabaseName=CBJYW;user=sa;Password=");
System.out.println("连接数据库成功");
ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
SendClass sendClass = (SendClass)ois.readObject();
ois.close();
//***********************************
System.out.println(sendClass.getQueryStatement());
System.out.println("成功接受数据对象");
Statement sqlStatement = dbcon.createStatement();
String dbsql = sendClass.getQueryStatement();
ResultSet dbResultSet = sqlStatement.executeQuery(dbsql);
CachedRowSet crs = new CachedRowSet();//?
crs.populate(dbResultSet);
// 关闭一些使用完的对象
dbResultSet.close();
sqlStatement.close();
dbcon.close();
//定义CachedRowSetWrapper类来传送给applet端
CachedWrapper cw = new CachedWrapper();
SetCachedWrapper(cw,crs);
crs.close();
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
oos.writeObject(cw);
oos.close();
System.out.println("成功传送对象给Applet");
}
catch(MyException me)
{
System.out.println(me.getMessage());
}
catch(SQLException sqle)
{
System.out.println("success fail");
System.out.println(sqle.getMessage());
sqle.printStackTrace();
}
catch(ClassNotFoundException cnfe)
{
System.err.println(cnfe.getMessage());
cnfe.printStackTrace();
}
}
//必须保证CachedRowSet 传入前要保证不是空
public void SetCachedWrapper(CachedWrapper cw,CachedRowSet crs) throws SQLException{
try{
while(crs.next()){
cw.setAbsoluteDepth(crs.getString("绝对深度"));
cw.setLongitude(crs.getString("经度"));
cw.setLatitude(crs.getString("纬度"));
cw.setWindDirect(crs.getString("风向"));
cw.setHeading(crs.getString("船首风向"));
cw.setRelativeWaterVelocity(crs.getString("相对水速度"));
cw.setRelativeGroundVelocity(crs.getString("相对地速度"));
cw.setResetCourse(crs.getString("复位里程"));
cw.setTotalCourse( crs.getString("总里程"));
cw.setAutoPilotUnits(crs.getString("自动舵单位"));
}
}
catch(SQLException sqle)
{
System.err.println(sqle.getMessage());
sqle.printStackTrace();
throw sqle;
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}