import java.net.*;import java.io.*;import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;
import java.sql.SQLException;import java.sql.Statement;
public class ClassFileName extends HttpServlet {
static String type ="";
static Connection con = null;
static Statement stmt = null;
static ResultSet rs = null;
static String code="RetrievalNumber546" ;
static double[][] dRaws=null;
static double[][] dResults=null;
static int fieldCount=2;
static String _r = "";
static int iSignificant2=0;
static void getSQLData(){
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF8","root", "root");
stmt = con.createStatement();
rs = stmt.executeQuery("select Tcode, UNIX_TIMESTAMP(date)*1000,field1,field2, field3, field4,field5,field6 from TableName where Tcode="+code+" ;");
while (rs.next()){String id=rs.getString(1);}rs.last(); int rowCount=rs.getRow();
dRaws = new double[rowCount][];
rs.beforeFirst();
int iRecno=0;
while (rs.next()){
dRaws[iRecno] = new double [6+10];
for (int j=2; j<=7; j++){
try{dRaws[iRecno][j-2]=(double) rs.getDouble(j); }catch (NullPointerException e) {};
}
++iRecno;
}
con.close();
} catch (Exception e) {e.printStackTrace();}
}
static double round(double _d,int _i){
double r=0;
switch (_i)
{
case 1: r=Math.round(_d*10)/10.0; break;
case 2: r=Math.round(_d*100)/100.00; break;
case 3: r=Math.round(_d*1000)/1000.000; break;
case 4: r=Math.round(_d*10000)/10000.0000; break;
case 5: r=Math.round(_d*100000)/100000.00000; break;
case 6: r=Math.round(_d*1000000)/1000000.000000; break;
default : r=Math.round(_d);
}
return r;
}
static void makeJSon(int Sign){
String delimiter=",";
int i=dResults.length;
for(int j=0;j<i; j++){
_r=_r+"["; // Begin of Record
long _L=(long) dResults[j][0];
_r=_r+_L+",";
if (j+1<Sign){_r=_r+""+",";} else {
Double _d2=Math.round(dResults[j][1]*10000)/10000.000;
_r=_r+Double.toString(_d2)+",";}
if (fieldCount>=6){
//_r=_r+((j<iSignificant2?)="":Double.toString(dResults[j][2]))+",";
_r=_r+Double.toString(dResults[j][2])+",";
_r=_r+Double.toString(dResults[j][3])+",";
_r=_r+Double.toString(dResults[j][4])+",";
_L=(long) dResults[j][5];_r=_r+_L+",";
}
for (int k=7; k<fieldCount; k++){
if (j<iSignificant2){_r=_r+",";} else {
_r=_r+Double.toString(dResults[j][k])+",";
}
}
_r=_r+(j+1) ;
if (j>=i-1) {delimiter="";}
_r=_r+"]"+delimiter+"\n";
}
}
/***************************
Main Program
***************************/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/javascript");
response.setCharacterEncoding("UTF-8");
code = request.getParameter("code");
type = request.getParameter("Type");
String callback = request.getParameter("callback");
String value="1",value_2="0",value_3="0",value_4="0",value_5="0";
value=request.getParameter("value");
int iValue=0,iValue2=0, iValue3=0,iValue4=0, iValue5=0;
if (value!=null){
String asValues[] = value.split(",");
if (asValues.length>=1)value=asValues[0];
if (asValues.length>=2)value_2=asValues[1];
if (asValues.length>=3)value_3=asValues[2];
if (asValues.length>=4)value_2=asValues[3];
if (asValues.length>=5)value_3=asValues[4];
iValue= Integer.parseInt(value);
iValue2= Integer.parseInt(value_2);
iValue3= Integer.parseInt(value_3);
iValue4= Integer.parseInt(value_4);
iValue5= Integer.parseInt(value_5);
}
int iSignificant=0;
//int iSignificant2=0;
String ExtraSign=",,MACD,MACDH,";
if (ExtraSign.indexOf(","+type+",")>0)
{iSignificant= iValue+iValue3-1;}
else {iSignificant= iValue;}
if (type.equals("MACDG")){iSignificant=0;iSignificant2=iValue+iValue3-1;;}
String _return = "",result="",newString ="";
_r=callback+"([\n";
getSQLData();
/***************************
create Reference Data
***************************/
if (type.equals("TEMPLATE") || type.equals("STEMPLATE")){getMA(iValue);}
else if (type.equals("TEMPLATE")){calTemplate(iValue,iValue2,iValue3);}
else if (type.equals("TEMPLATE")){calTemplate(iValue,iValue2,iValue3);}
else if (type.equals("TEMPLATE")){calTemplate(iValue,iValue2,iValue3);}
makeJSon(iSignificant);
_r=_r+"]);";
StringBuilder sb = new StringBuilder(); sb.append(_r); _r="";
PrintWriter out = response.getWriter();
out.print(sb.toString());
out.flush();
dRaws=null;
dResults=null;
}
}
沒有留言:
張貼留言